Skip to content

Commit 6ef0a03

Browse files
author
xingzhaozhu
committed
bugfix: correct generate search span_name
1 parent 1bb3dcf commit 6ef0a03

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

instrumentation/opentelemetry-instrumentation-elasticsearch/src/opentelemetry/instrumentation/elasticsearch/__init__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ def _uninstrument(self, **kwargs):
150150

151151
_regex_doc_url = re.compile(r"/_doc/([^/]+)")
152152

153+
# search api https://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html
154+
_regex_search_url = re.compile(f"/([^/]+)/_search[/]?")
155+
153156

154157
def _wrap_perform_request(
155158
tracer, span_name_prefix, request_hook=None, response_hook=None
@@ -168,6 +171,7 @@ def wrapper(wrapped, _, args, kwargs):
168171

169172
op_name = span_name_prefix + (url or method or _DEFAULT_OP_NAME)
170173
doc_id = None
174+
search_target = None
171175
if url:
172176
# 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()
173177
# A limitation of the regex is that only the '_doc' mapping type is supported. Mapping types are deprecated since Elasticsearch 7
@@ -184,6 +188,11 @@ def wrapper(wrapped, _, args, kwargs):
184188
)
185189
# Put the document ID in attributes
186190
doc_id = match.group(1)
191+
match = _regex_search_url.search(url)
192+
if match is not None:
193+
op_name = span_name_prefix + "/<target>/_search"
194+
search_target = match.group(1)
195+
187196
params = kwargs.get("params", {})
188197
body = kwargs.get("body", None)
189198

@@ -209,6 +218,8 @@ def wrapper(wrapped, _, args, kwargs):
209218
attributes["elasticsearch.params"] = str(params)
210219
if doc_id:
211220
attributes["elasticsearch.id"] = doc_id
221+
if search_target:
222+
attributes["elasticsearch.target"] = search_target
212223
for key, value in attributes.items():
213224
span.set_attribute(key, value)
214225

instrumentation/opentelemetry-instrumentation-elasticsearch/tests/test_elasticsearch.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,14 +237,15 @@ def test_dsl_search(self, request_mock):
237237
spans = self.get_finished_spans()
238238
span = spans[0]
239239
self.assertEqual(1, len(spans))
240-
self.assertEqual(span.name, "Elasticsearch/test-index/_search")
240+
self.assertEqual(span.name, "Elasticsearch/<target>/_search")
241241
self.assertIsNotNone(span.end_time)
242242
self.assertEqual(
243243
span.attributes,
244244
{
245245
SpanAttributes.DB_SYSTEM: "elasticsearch",
246246
"elasticsearch.url": "/test-index/_search",
247247
"elasticsearch.method": helpers.dsl_search_method,
248+
"elasticsearch.target": "test-index",
248249
SpanAttributes.DB_STATEMENT: str(
249250
{
250251
"query": {

0 commit comments

Comments
 (0)