Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e65b788

Browse files
committedFeb 23, 2023
Add support for enabling Redis sanitization from environment variable
1 parent 74a8b90 commit e65b788

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed
 

Diff for: ‎instrumentation/opentelemetry-instrumentation-redis/src/opentelemetry/instrumentation/redis/__init__.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ def response_hook(span, instance, response):
9191
API
9292
---
9393
"""
94+
from os import environ
9495
import typing
9596
from typing import Any, Collection
9697

@@ -104,6 +105,9 @@ def response_hook(span, instance, response):
104105
_extract_conn_attributes,
105106
_format_command_args,
106107
)
108+
from opentelemetry.instrumentation.redis.environment_variables import (
109+
OTEL_PYTHON_INSTRUMENTATION_SANITIZE_REDIS
110+
)
107111
from opentelemetry.instrumentation.redis.version import __version__
108112
from opentelemetry.instrumentation.utils import unwrap
109113
from opentelemetry.semconv.trace import SpanAttributes
@@ -287,7 +291,11 @@ def _instrument(self, **kwargs):
287291
tracer,
288292
request_hook=kwargs.get("request_hook"),
289293
response_hook=kwargs.get("response_hook"),
290-
sanitize_query=kwargs.get("sanitize_query", False),
294+
sanitize_query=kwargs.get(
295+
"sanitize_query",
296+
environ.get(OTEL_PYTHON_INSTRUMENTATION_SANITIZE_REDIS, "false").lower()
297+
== "true",
298+
)
291299
)
292300

293301
def _uninstrument(self, **kwargs):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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 = "OTEL_PYTHON_INSTRUMENTATION_SANITIZE_REDIS"

Diff for: ‎instrumentation/opentelemetry-instrumentation-redis/tests/test_redis.py

+25
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,31 @@ 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", {"OTEL_PYTHON_INSTRUMENTATION_SANITIZE_REDIS": "true"}
180+
)
181+
env_patch.start()
182+
RedisInstrumentor().instrument(
183+
tracer_provider=self.tracer_provider,
184+
)
185+
186+
with mock.patch.object(redis_client, "connection"):
187+
redis_client.set("key", "value")
188+
189+
spans = self.memory_exporter.get_finished_spans()
190+
self.assertEqual(len(spans), 1)
191+
192+
span = spans[0]
193+
self.assertEqual(span.attributes.get("db.statement"), "SET ? ?")
194+
env_patch.stop()
195+
171196
def test_query_sanitizer_disabled(self):
172197
redis_client = redis.Redis()
173198
connection = redis.connection.Connection()

0 commit comments

Comments
 (0)
Please sign in to comment.