Skip to content

Commit 896ea31

Browse files
xingzhaozhuzzhutianyu
xingzhaozhu
authored andcommitted
bugfix: correct generate search span_name
1 parent 1bb3dcf commit 896ea31

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717
([#999])(https://github.com/open-telemetry/opentelemetry-python-contrib/pull/999)
1818
- `opentelemetry-instrumentation-falcon` Falcon: Capture custom request/response headers in span attributes
1919
([#1003])(https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1003)
20+
- `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`
21+
([#1018](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1018))
2022

2123
## [1.10.0-0.29b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.10.0-0.29b0) - 2022-03-10
2224

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

+14-1
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,14 @@ 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(r"/([^/]+)/_search[/]?")
155+
153156

154157
def _wrap_perform_request(
155158
tracer, span_name_prefix, request_hook=None, response_hook=None
156159
):
157-
# pylint: disable=R0912
160+
# pylint: disable=R0912,R0914
158161
def wrapper(wrapped, _, args, kwargs):
159162
method = url = None
160163
try:
@@ -167,7 +170,10 @@ def wrapper(wrapped, _, args, kwargs):
167170
)
168171

169172
op_name = span_name_prefix + (url or method or _DEFAULT_OP_NAME)
173+
170174
doc_id = None
175+
search_target = None
176+
171177
if url:
172178
# 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()
173179
# 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 wrapper(wrapped, _, args, kwargs):
184190
)
185191
# Put the document ID in attributes
186192
doc_id = match.group(1)
193+
match = _regex_search_url.search(url)
194+
if match is not None:
195+
op_name = span_name_prefix + "/<target>/_search"
196+
search_target = match.group(1)
197+
187198
params = kwargs.get("params", {})
188199
body = kwargs.get("body", None)
189200

@@ -209,6 +220,8 @@ def wrapper(wrapped, _, args, kwargs):
209220
attributes["elasticsearch.params"] = str(params)
210221
if doc_id:
211222
attributes["elasticsearch.id"] = doc_id
223+
if search_target:
224+
attributes["elasticsearch.target"] = search_target
212225
for key, value in attributes.items():
213226
span.set_attribute(key, value)
214227

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

+2-1
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)