Skip to content

Commit ca082a7

Browse files
xrmxsilverjampquentin
authoredMar 22, 2024
elasticsearch: don't set body as db statement for bulk requests (#2355)
* 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 #2150 Co-authored-by: Jason Mobarak <[email protected]> Co-authored-by: Quentin Pradet <[email protected]> * Update CHANGELOG * Please the linter --------- Co-authored-by: Jason Mobarak <[email protected]> Co-authored-by: Quentin Pradet <[email protected]>
1 parent ada2784 commit ca082a7

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed
 

‎CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2121
([#2151](https://github.com/open-telemetry/opentelemetry-python-contrib/issues/2298))
2222
- Avoid losing repeated HTTP headers
2323
([#2266](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2266))
24+
- `opentelemetry-instrumentation-elasticsearch` Don't send bulk request body as db statement
25+
([#2355](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2355))
2426

2527
## Version 1.23.0/0.44b0 (2024-02-23)
2628

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

+5-3
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[
251+
SpanAttributes.DB_STATEMENT
252+
] = sanitize_body(body)
251253
if params:
252254
attributes["elasticsearch.params"] = str(params)
253255
if doc_id:

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

+34
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151

5252
Article = helpers.Article
5353

54+
# pylint: disable=too-many-public-methods
55+
5456

5557
@mock.patch(
5658
"elasticsearch.connection.http_urllib3.Urllib3HttpConnection.perform_request"
@@ -486,3 +488,35 @@ def test_body_sanitization(self, _):
486488
sanitize_body(json.dumps(sanitization_queries.interval_query)),
487489
str(sanitization_queries.interval_query_sanitized),
488490
)
491+
492+
def test_bulk(self, request_mock):
493+
request_mock.return_value = (1, {}, "")
494+
495+
es = Elasticsearch()
496+
es.bulk(
497+
[
498+
{
499+
"_op_type": "index",
500+
"_index": "sw",
501+
"_doc_type": "_doc",
502+
"_id": 1,
503+
"doc": {"name": "adam"},
504+
},
505+
{
506+
"_op_type": "index",
507+
"_index": "sw",
508+
"_doc_type": "_doc",
509+
"_id": 1,
510+
"doc": {"name": "adam"},
511+
},
512+
]
513+
)
514+
515+
spans_list = self.get_finished_spans()
516+
self.assertEqual(len(spans_list), 1)
517+
span = spans_list[0]
518+
519+
# Check version and name in span's instrumentation info
520+
self.assertEqualSpanInstrumentationInfo(
521+
span, opentelemetry.instrumentation.elasticsearch
522+
)

0 commit comments

Comments
 (0)