diff --git a/instrumentation/opentelemetry-instrumentation-redis/src/opentelemetry/instrumentation/redis/__init__.py b/instrumentation/opentelemetry-instrumentation-redis/src/opentelemetry/instrumentation/redis/__init__.py index 0f18639bd2..0ad55fedc1 100644 --- a/instrumentation/opentelemetry-instrumentation-redis/src/opentelemetry/instrumentation/redis/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-redis/src/opentelemetry/instrumentation/redis/__init__.py @@ -91,6 +91,7 @@ def response_hook(span, instance, response): API --- """ +from os import environ import typing from typing import Any, Collection @@ -104,6 +105,9 @@ def response_hook(span, instance, response): _extract_conn_attributes, _format_command_args, ) +from opentelemetry.instrumentation.redis.environment_variables import ( + OTEL_PYTHON_INSTRUMENTATION_SANITIZE_REDIS +) from opentelemetry.instrumentation.redis.version import __version__ from opentelemetry.instrumentation.utils import unwrap from opentelemetry.semconv.trace import SpanAttributes @@ -287,7 +291,11 @@ def _instrument(self, **kwargs): tracer, request_hook=kwargs.get("request_hook"), response_hook=kwargs.get("response_hook"), - sanitize_query=kwargs.get("sanitize_query", False), + sanitize_query=kwargs.get( + "sanitize_query", + environ.get(OTEL_PYTHON_INSTRUMENTATION_SANITIZE_REDIS, "false").lower() + == "true", + ) ) def _uninstrument(self, **kwargs): diff --git a/instrumentation/opentelemetry-instrumentation-redis/src/opentelemetry/instrumentation/redis/environment_variables.py b/instrumentation/opentelemetry-instrumentation-redis/src/opentelemetry/instrumentation/redis/environment_variables.py new file mode 100644 index 0000000000..02b7e42163 --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-redis/src/opentelemetry/instrumentation/redis/environment_variables.py @@ -0,0 +1,15 @@ +# 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. + +OTEL_PYTHON_INSTRUMENTATION_SANITIZE_REDIS = "OTEL_PYTHON_INSTRUMENTATION_SANITIZE_REDIS" diff --git a/instrumentation/opentelemetry-instrumentation-redis/tests/test_redis.py b/instrumentation/opentelemetry-instrumentation-redis/tests/test_redis.py index 1c64fcb13e..4bf333433e 100644 --- a/instrumentation/opentelemetry-instrumentation-redis/tests/test_redis.py +++ b/instrumentation/opentelemetry-instrumentation-redis/tests/test_redis.py @@ -168,6 +168,31 @@ def test_query_sanitizer_enabled(self): span = spans[0] self.assertEqual(span.attributes.get("db.statement"), "SET ? ?") + def test_query_sanitizer_enabled_env(self): + redis_client = redis.Redis() + connection = redis.connection.Connection() + redis_client.connection = connection + + RedisInstrumentor().uninstrument() + + env_patch = mock.patch.dict( + "os.environ", {"OTEL_PYTHON_INSTRUMENTATION_SANITIZE_REDIS": "true"} + ) + env_patch.start() + RedisInstrumentor().instrument( + tracer_provider=self.tracer_provider, + ) + + with mock.patch.object(redis_client, "connection"): + redis_client.set("key", "value") + + spans = self.memory_exporter.get_finished_spans() + self.assertEqual(len(spans), 1) + + span = spans[0] + self.assertEqual(span.attributes.get("db.statement"), "SET ? ?") + env_patch.stop() + def test_query_sanitizer_disabled(self): redis_client = redis.Redis() connection = redis.connection.Connection()