-
Notifications
You must be signed in to change notification settings - Fork 599
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add elasticsearch db.statement sanitization #1598
Merged
srikanthccv
merged 15 commits into
open-telemetry:main
from
nemoshlag:add/elasticsearch-sanitization
Feb 10, 2023
Merged
Changes from 10 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
59a512d
add elasticsearch sanitization
nemoshlag a3b3fb3
Merge branch 'main' into add/elasticsearch-sanitization
nemoshlag 044d2c6
Merge branch 'main' into add/elasticsearch-sanitization
nemoshlag b773269
add specific query sanitization
nemoshlag 0c19af8
tox generate fix
nemoshlag 9579688
Merge branch 'main' into add/elasticsearch-sanitization
nemoshlag 2bfa30f
Merge branch 'main' into add/elasticsearch-sanitization
nemoshlag dfc33ea
Merge branch 'main' into add/elasticsearch-sanitization
srikanthccv d1cf110
Merge branch 'main' into add/elasticsearch-sanitization
nemoshlag 44c0bb2
lint fixes
nemoshlag d84bf7e
clean CHANGELOG.md
nemoshlag 5df3034
clean CHANGELOG.md2
nemoshlag d389280
lint fix
nemoshlag b370f43
lint fix2
nemoshlag a9ebfcc
Merge branch 'main' into add/elasticsearch-sanitization
nemoshlag File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,7 @@ | |
|
||
The instrument() method accepts the following keyword args: | ||
tracer_provider (TracerProvider) - an optional tracer provider | ||
sanitize_query (bool) - an optional query sanitization flag | ||
request_hook (Callable) - a function with extra user-defined logic to be performed before performing the request | ||
this function signature is: | ||
def request_hook(span: Span, method: str, url: str, kwargs) | ||
|
@@ -96,6 +97,8 @@ def response_hook(span, response): | |
from opentelemetry.semconv.trace import SpanAttributes | ||
from opentelemetry.trace import SpanKind, get_tracer | ||
|
||
from .utils import sanitize_body | ||
|
||
logger = getLogger(__name__) | ||
|
||
|
||
|
@@ -135,11 +138,16 @@ def _instrument(self, **kwargs): | |
tracer = get_tracer(__name__, __version__, tracer_provider) | ||
request_hook = kwargs.get("request_hook") | ||
response_hook = kwargs.get("response_hook") | ||
sanitize_query = kwargs.get("sanitize_query", False) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't the default be True? See also open-telemetry/opentelemetry-specification#3104 |
||
_wrap( | ||
elasticsearch, | ||
"Transport.perform_request", | ||
_wrap_perform_request( | ||
tracer, self._span_name_prefix, request_hook, response_hook | ||
tracer, | ||
sanitize_query, | ||
self._span_name_prefix, | ||
request_hook, | ||
response_hook, | ||
), | ||
) | ||
|
||
|
@@ -154,7 +162,11 @@ def _uninstrument(self, **kwargs): | |
|
||
|
||
def _wrap_perform_request( | ||
tracer, span_name_prefix, request_hook=None, response_hook=None | ||
tracer, | ||
sanitize_query, | ||
span_name_prefix, | ||
request_hook=None, | ||
response_hook=None, | ||
): | ||
# pylint: disable=R0912,R0914 | ||
def wrapper(wrapped, _, args, kwargs): | ||
|
@@ -213,7 +225,10 @@ def wrapper(wrapped, _, args, kwargs): | |
if method: | ||
attributes["elasticsearch.method"] = method | ||
if body: | ||
attributes[SpanAttributes.DB_STATEMENT] = str(body) | ||
statement = str(body) | ||
if sanitize_query: | ||
statement = sanitize_body(body) | ||
attributes[SpanAttributes.DB_STATEMENT] = statement | ||
if params: | ||
attributes["elasticsearch.params"] = str(params) | ||
if doc_id: | ||
|
59 changes: 59 additions & 0 deletions
59
...ry-instrumentation-elasticsearch/src/opentelemetry/instrumentation/elasticsearch/utils.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Copyright The OpenTelemetry Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
sanitized_keys = ( | ||
"message", | ||
"should", | ||
"filter", | ||
"query", | ||
"queries", | ||
"intervals", | ||
"match", | ||
) | ||
sanitized_value = "?" | ||
|
||
|
||
# pylint: disable=C0103 | ||
def _flatten_dict(d, parent_key=""): | ||
items = [] | ||
for k, v in d.items(): | ||
new_key = parent_key + "." + k if parent_key else k | ||
if isinstance(v, dict): | ||
items.extend(_flatten_dict(v, new_key).items()) | ||
else: | ||
items.append((new_key, v)) | ||
return dict(items) | ||
|
||
|
||
def _unflatten_dict(d): | ||
res = {} | ||
for k, v in d.items(): | ||
keys = k.split(".") | ||
d = res | ||
for key in keys[:-1]: | ||
if key not in d: | ||
d[key] = {} | ||
d = d[key] | ||
d[keys[-1]] = v | ||
return res | ||
|
||
|
||
def sanitize_body(body) -> str: | ||
flatten_body = _flatten_dict(body) | ||
|
||
for key in flatten_body.keys(): | ||
if key.endswith(sanitized_keys): | ||
flatten_body[key] = sanitized_value | ||
|
||
return str(_unflatten_dict(flatten_body)) |
65 changes: 65 additions & 0 deletions
65
instrumentation/opentelemetry-instrumentation-elasticsearch/tests/sanitization_queries.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
interval_query = { | ||
"query": { | ||
"intervals": { | ||
"my_text": { | ||
"all_of": { | ||
"ordered": True, | ||
"intervals": [ | ||
{ | ||
"match": { | ||
"query": "my favorite food", | ||
"max_gaps": 0, | ||
"ordered": True, | ||
} | ||
}, | ||
{ | ||
"any_of": { | ||
"intervals": [ | ||
{"match": {"query": "hot water"}}, | ||
{"match": {"query": "cold porridge"}}, | ||
] | ||
} | ||
}, | ||
], | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
match_query = {"query": {"match": {"message": {"query": "this is a test"}}}} | ||
|
||
filter_query = { | ||
"query": { | ||
"bool": { | ||
"must": [ | ||
{"match": {"title": "Search"}}, | ||
{"match": {"content": "Elasticsearch"}}, | ||
], | ||
"filter": [ | ||
{"term": {"status": "published"}}, | ||
{"range": {"publish_date": {"gte": "2015-01-01"}}}, | ||
], | ||
} | ||
} | ||
} | ||
|
||
interval_query_sanitized = { | ||
"query": { | ||
"intervals": { | ||
"my_text": {"all_of": {"ordered": True, "intervals": "?"}} | ||
} | ||
} | ||
} | ||
match_query_sanitized = {"query": {"match": {"message": {"query": "?"}}}} | ||
filter_query_sanitized = { | ||
"query": { | ||
"bool": { | ||
"must": [ | ||
{"match": {"title": "Search"}}, | ||
{"match": {"content": "Elasticsearch"}}, | ||
], | ||
"filter": "?", | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The merge with the main accidentally adds this entry