Skip to content

Commit ab9d4b1

Browse files
xrmxsilverjam
andcommitted
elasticsearch: don't set body as db statement for bulk requests
bulk requests can be too big and diverse to make sense as db statement. Other than that the sanitizer currently only handles dicts so it's crashing. Closes open-telemetry#2150 Co-authored-by: Jason Mobarak <[email protected]>
1 parent dcffb58 commit ab9d4b1

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,11 @@ def wrapper(wrapped, _, args, kwargs):
245245
if method:
246246
attributes["elasticsearch.method"] = method
247247
if body:
248-
attributes[SpanAttributes.DB_STATEMENT] = sanitize_body(
249-
body
250-
)
248+
# Don't set db.statement for bulk requests, as it can be very large
249+
if isinstance(body, dict):
250+
attributes[SpanAttributes.DB_STATEMENT] = sanitize_body(
251+
body
252+
)
251253
if params:
252254
attributes["elasticsearch.params"] = str(params)
253255
if doc_id:

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,3 +486,19 @@ def test_body_sanitization(self, _):
486486
sanitize_body(json.dumps(sanitization_queries.interval_query)),
487487
str(sanitization_queries.interval_query_sanitized),
488488
)
489+
490+
def test_bulk(self, request_mock):
491+
request_mock.return_value = (1, {}, "")
492+
493+
es = Elasticsearch()
494+
es.bulk([dict(_op_type="index", _index="sw", _doc_type="_doc", _id=1, doc={"name": "adam"})] * 2)
495+
496+
spans_list = self.get_finished_spans()
497+
self.assertEqual(len(spans_list), 1)
498+
span = spans_list[0]
499+
500+
# Check version and name in span's instrumentation info
501+
# self.assertEqualSpanInstrumentationInfo(span, opentelemetry.instrumentation.elasticsearch)
502+
self.assertEqualSpanInstrumentationInfo(
503+
span, opentelemetry.instrumentation.elasticsearch
504+
)

0 commit comments

Comments
 (0)