bugfix: correct generate search span_name (#1018)

This commit is contained in:
zhutianyu
2022-03-25 01:56:52 +08:00
committed by GitHub
parent 8727bc3374
commit db478789e4
3 changed files with 18 additions and 2 deletions

View File

@ -17,6 +17,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#999])(https://github.com/open-telemetry/opentelemetry-python-contrib/pull/999)
- `opentelemetry-instrumentation-falcon` Falcon: Capture custom request/response headers in span attributes
([#1003])(https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1003)
- `opentelemetry-instrumentation-elasticsearch` no longer creates unique span names by including search target, replaces them with `<target>` and puts the value in attribute `elasticsearch.target`
([#1018](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1018))
## [1.10.0-0.29b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.10.0-0.29b0) - 2022-03-10

View File

@ -150,11 +150,14 @@ class ElasticsearchInstrumentor(BaseInstrumentor):
_regex_doc_url = re.compile(r"/_doc/([^/]+)")
# search api https://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html
_regex_search_url = re.compile(r"/([^/]+)/_search[/]?")
def _wrap_perform_request(
tracer, span_name_prefix, request_hook=None, response_hook=None
):
# pylint: disable=R0912
# pylint: disable=R0912,R0914
def wrapper(wrapped, _, args, kwargs):
method = url = None
try:
@ -167,7 +170,10 @@ def _wrap_perform_request(
)
op_name = span_name_prefix + (url or method or _DEFAULT_OP_NAME)
doc_id = None
search_target = None
if url:
# TODO: This regex-based solution avoids creating an unbounded number of span names, but should be replaced by instrumenting individual Elasticsearch methods instead of Transport.perform_request()
# A limitation of the regex is that only the '_doc' mapping type is supported. Mapping types are deprecated since Elasticsearch 7
@ -184,6 +190,11 @@ def _wrap_perform_request(
)
# Put the document ID in attributes
doc_id = match.group(1)
match = _regex_search_url.search(url)
if match is not None:
op_name = span_name_prefix + "/<target>/_search"
search_target = match.group(1)
params = kwargs.get("params", {})
body = kwargs.get("body", None)
@ -209,6 +220,8 @@ def _wrap_perform_request(
attributes["elasticsearch.params"] = str(params)
if doc_id:
attributes["elasticsearch.id"] = doc_id
if search_target:
attributes["elasticsearch.target"] = search_target
for key, value in attributes.items():
span.set_attribute(key, value)

View File

@ -237,7 +237,7 @@ class TestElasticsearchIntegration(TestBase):
spans = self.get_finished_spans()
span = spans[0]
self.assertEqual(1, len(spans))
self.assertEqual(span.name, "Elasticsearch/test-index/_search")
self.assertEqual(span.name, "Elasticsearch/<target>/_search")
self.assertIsNotNone(span.end_time)
self.assertEqual(
span.attributes,
@ -245,6 +245,7 @@ class TestElasticsearchIntegration(TestBase):
SpanAttributes.DB_SYSTEM: "elasticsearch",
"elasticsearch.url": "/test-index/_search",
"elasticsearch.method": helpers.dsl_search_method,
"elasticsearch.target": "test-index",
SpanAttributes.DB_STATEMENT: str(
{
"query": {