Skip to content

Commit 19fe771

Browse files
authored
Sanitize DB_STATEMENT by default for elasticsearch (#1758)
1 parent 2d4e6c9 commit 19fe771

File tree

4 files changed

+9
-28
lines changed

4 files changed

+9
-28
lines changed

Diff for: CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2020

2121
### Fixed
2222

23+
- Fix elasticsearch db.statement attribute to be sanitized by default
24+
([#1758](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1758))
2325
- Fix `AttributeError` when AWS Lambda handler receives a list event
2426
([#1738](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1738))
2527
- Fix `None does not implement middleware` error when there are no middlewares registered

Diff for: instrumentation/opentelemetry-instrumentation-elasticsearch/src/opentelemetry/instrumentation/elasticsearch/__init__.py

+3-8
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
4545
The instrument() method accepts the following keyword args:
4646
tracer_provider (TracerProvider) - an optional tracer provider
47-
sanitize_query (bool) - an optional query sanitization flag
4847
request_hook (Callable) - a function with extra user-defined logic to be performed before performing the request
4948
this function signature is:
5049
def request_hook(span: Span, method: str, url: str, kwargs)
@@ -138,13 +137,11 @@ def _instrument(self, **kwargs):
138137
tracer = get_tracer(__name__, __version__, tracer_provider)
139138
request_hook = kwargs.get("request_hook")
140139
response_hook = kwargs.get("response_hook")
141-
sanitize_query = kwargs.get("sanitize_query", False)
142140
_wrap(
143141
elasticsearch,
144142
"Transport.perform_request",
145143
_wrap_perform_request(
146144
tracer,
147-
sanitize_query,
148145
self._span_name_prefix,
149146
request_hook,
150147
response_hook,
@@ -163,7 +160,6 @@ def _uninstrument(self, **kwargs):
163160

164161
def _wrap_perform_request(
165162
tracer,
166-
sanitize_query,
167163
span_name_prefix,
168164
request_hook=None,
169165
response_hook=None,
@@ -225,10 +221,9 @@ def wrapper(wrapped, _, args, kwargs):
225221
if method:
226222
attributes["elasticsearch.method"] = method
227223
if body:
228-
statement = str(body)
229-
if sanitize_query:
230-
statement = sanitize_body(body)
231-
attributes[SpanAttributes.DB_STATEMENT] = statement
224+
attributes[SpanAttributes.DB_STATEMENT] = sanitize_body(
225+
body
226+
)
232227
if params:
233228
attributes["elasticsearch.params"] = str(params)
234229
if doc_id:

Diff for: instrumentation/opentelemetry-instrumentation-elasticsearch/src/opentelemetry/instrumentation/elasticsearch/utils.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ def _flatten_dict(d, parent_key=""):
2929
items = []
3030
for k, v in d.items():
3131
new_key = parent_key + "." + k if parent_key else k
32-
if isinstance(v, dict):
32+
# recursive call _flatten_dict for a non-empty dict value
33+
if isinstance(v, dict) and v:
3334
items.extend(_flatten_dict(v, new_key).items())
3435
else:
3536
items.append((new_key, v))

Diff for: instrumentation/opentelemetry-instrumentation-elasticsearch/tests/test_elasticsearch.py

+2-19
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,7 @@ class TestElasticsearchIntegration(TestBase):
5858
"elasticsearch.url": "/test-index/_search",
5959
"elasticsearch.method": helpers.dsl_search_method,
6060
"elasticsearch.target": "test-index",
61-
SpanAttributes.DB_STATEMENT: str(
62-
{"query": {"bool": {"filter": [{"term": {"author": "testing"}}]}}}
63-
),
61+
SpanAttributes.DB_STATEMENT: str({"query": {"bool": {"filter": "?"}}}),
6462
}
6563

6664
create_attributes = {
@@ -264,18 +262,6 @@ def test_dsl_search(self, request_mock):
264262
)
265263

266264
def test_dsl_search_sanitized(self, request_mock):
267-
# Reset instrumentation to use sanitized query (default)
268-
ElasticsearchInstrumentor().uninstrument()
269-
ElasticsearchInstrumentor().instrument(sanitize_query=True)
270-
271-
# update expected attributes to match sanitized query
272-
sanitized_search_attributes = self.search_attributes.copy()
273-
sanitized_search_attributes.update(
274-
{
275-
SpanAttributes.DB_STATEMENT: "{'query': {'bool': {'filter': '?'}}}"
276-
}
277-
)
278-
279265
request_mock.return_value = (1, {}, '{"hits": {"hits": []}}')
280266
client = Elasticsearch()
281267
search = Search(using=client, index="test-index").filter(
@@ -289,7 +275,7 @@ def test_dsl_search_sanitized(self, request_mock):
289275
self.assertIsNotNone(span.end_time)
290276
self.assertEqual(
291277
span.attributes,
292-
sanitized_search_attributes,
278+
self.search_attributes,
293279
)
294280

295281
def test_dsl_create(self, request_mock):
@@ -320,9 +306,6 @@ def test_dsl_create(self, request_mock):
320306
)
321307

322308
def test_dsl_create_sanitized(self, request_mock):
323-
# Reset instrumentation to explicitly use sanitized query
324-
ElasticsearchInstrumentor().uninstrument()
325-
ElasticsearchInstrumentor().instrument(sanitize_query=True)
326309
request_mock.return_value = (1, {}, {})
327310
client = Elasticsearch()
328311
Article.init(using=client)

0 commit comments

Comments
 (0)