Skip to content

Commit 6bc55f3

Browse files
danwiltshiresrikanthccv
authored andcommitted
Add support for enabling Redis sanitization from environment variable (open-telemetry#1690)
* Add support for enabling Redis sanitization from environment variable * add sanitization environment variable docs * strip environment variable * update changelog * lint * lint * lint - hopefully the last time --------- Co-authored-by: Srikanth Chekuri <[email protected]>
1 parent 92f7a15 commit 6bc55f3

File tree

4 files changed

+74
-1
lines changed

4 files changed

+74
-1
lines changed

CHANGELOG.md

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

1515
- Add connection attributes to sqlalchemy connect span
1616
([#1608](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1608))
17+
- Add support for enabling Redis sanitization from environment variable
18+
([#1690](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1690))
1719

1820
### Fixed
1921

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

+29-1
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,37 @@ def response_hook(span, instance, response):
8888
client = redis.StrictRedis(host="localhost", port=6379)
8989
client.get("my-key")
9090
91+
Configuration
92+
-------------
93+
94+
Query sanitization
95+
******************
96+
To enable query sanitization with an environment variable, set
97+
``OTEL_PYTHON_INSTRUMENTATION_SANITIZE_REDIS`` to "true".
98+
99+
For example,
100+
101+
::
102+
103+
export OTEL_PYTHON_INSTRUMENTATION_SANITIZE_REDIS="true"
104+
105+
will result in traced queries like "SET ? ?".
106+
91107
API
92108
---
93109
"""
94110
import typing
111+
from os import environ
95112
from typing import Any, Collection
96113

97114
import redis
98115
from wrapt import wrap_function_wrapper
99116

100117
from opentelemetry import trace
101118
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
119+
from opentelemetry.instrumentation.redis.environment_variables import (
120+
OTEL_PYTHON_INSTRUMENTATION_SANITIZE_REDIS,
121+
)
102122
from opentelemetry.instrumentation.redis.package import _instruments
103123
from opentelemetry.instrumentation.redis.util import (
104124
_extract_conn_attributes,
@@ -287,7 +307,15 @@ def _instrument(self, **kwargs):
287307
tracer,
288308
request_hook=kwargs.get("request_hook"),
289309
response_hook=kwargs.get("response_hook"),
290-
sanitize_query=kwargs.get("sanitize_query", False),
310+
sanitize_query=kwargs.get(
311+
"sanitize_query",
312+
environ.get(
313+
OTEL_PYTHON_INSTRUMENTATION_SANITIZE_REDIS, "false"
314+
)
315+
.lower()
316+
.strip()
317+
== "true",
318+
),
291319
)
292320

293321
def _uninstrument(self, **kwargs):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
OTEL_PYTHON_INSTRUMENTATION_SANITIZE_REDIS = (
16+
"OTEL_PYTHON_INSTRUMENTATION_SANITIZE_REDIS"
17+
)

instrumentation/opentelemetry-instrumentation-redis/tests/test_redis.py

+26
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,32 @@ def test_query_sanitizer_enabled(self):
168168
span = spans[0]
169169
self.assertEqual(span.attributes.get("db.statement"), "SET ? ?")
170170

171+
def test_query_sanitizer_enabled_env(self):
172+
redis_client = redis.Redis()
173+
connection = redis.connection.Connection()
174+
redis_client.connection = connection
175+
176+
RedisInstrumentor().uninstrument()
177+
178+
env_patch = mock.patch.dict(
179+
"os.environ",
180+
{"OTEL_PYTHON_INSTRUMENTATION_SANITIZE_REDIS": "true"},
181+
)
182+
env_patch.start()
183+
RedisInstrumentor().instrument(
184+
tracer_provider=self.tracer_provider,
185+
)
186+
187+
with mock.patch.object(redis_client, "connection"):
188+
redis_client.set("key", "value")
189+
190+
spans = self.memory_exporter.get_finished_spans()
191+
self.assertEqual(len(spans), 1)
192+
193+
span = spans[0]
194+
self.assertEqual(span.attributes.get("db.statement"), "SET ? ?")
195+
env_patch.stop()
196+
171197
def test_query_sanitizer_disabled(self):
172198
redis_client = redis.Redis()
173199
connection = redis.connection.Connection()

0 commit comments

Comments
 (0)