From e65b7881db5f62b2edfa8d3bc5a2f873db946e7a Mon Sep 17 00:00:00 2001 From: Daniel Wiltshire <35703524+danwiltshire@users.noreply.github.com> Date: Thu, 23 Feb 2023 11:33:10 +0000 Subject: [PATCH 1/7] Add support for enabling Redis sanitization from environment variable --- .../instrumentation/redis/__init__.py | 10 +++++++- .../redis/environment_variables.py | 15 +++++++++++ .../tests/test_redis.py | 25 +++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 instrumentation/opentelemetry-instrumentation-redis/src/opentelemetry/instrumentation/redis/environment_variables.py 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() From 0afa55fb1999dd6d859b904073e8588176e3020c Mon Sep 17 00:00:00 2001 From: Daniel Wiltshire <35703524+danwiltshire@users.noreply.github.com> Date: Thu, 23 Feb 2023 11:43:22 +0000 Subject: [PATCH 2/7] add sanitization environment variable docs --- .../instrumentation/redis/__init__.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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 0ad55fedc1..5bf70087f4 100644 --- a/instrumentation/opentelemetry-instrumentation-redis/src/opentelemetry/instrumentation/redis/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-redis/src/opentelemetry/instrumentation/redis/__init__.py @@ -88,6 +88,22 @@ def response_hook(span, instance, response): client = redis.StrictRedis(host="localhost", port=6379) client.get("my-key") +Configuration +------------- + +Query sanitization +****************** +To enable query sanitization with an environment variable, set +``OTEL_PYTHON_INSTRUMENTATION_SANITIZE_REDIS`` to "true". + +For example, + +:: + + export OTEL_PYTHON_INSTRUMENTATION_SANITIZE_REDIS="true" + +will result in traced queries like "SET ? ?". + API --- """ From c061be7dc1383453df2daecfa895183a9fd42fcf Mon Sep 17 00:00:00 2001 From: Daniel Wiltshire <35703524+danwiltshire@users.noreply.github.com> Date: Thu, 23 Feb 2023 15:01:14 +0000 Subject: [PATCH 3/7] strip environment variable --- .../src/opentelemetry/instrumentation/redis/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 5bf70087f4..3c3341340f 100644 --- a/instrumentation/opentelemetry-instrumentation-redis/src/opentelemetry/instrumentation/redis/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-redis/src/opentelemetry/instrumentation/redis/__init__.py @@ -309,7 +309,7 @@ def _instrument(self, **kwargs): response_hook=kwargs.get("response_hook"), sanitize_query=kwargs.get( "sanitize_query", - environ.get(OTEL_PYTHON_INSTRUMENTATION_SANITIZE_REDIS, "false").lower() + environ.get(OTEL_PYTHON_INSTRUMENTATION_SANITIZE_REDIS, "false").lower().strip() == "true", ) ) From 8d39922745149e18aae18625ed27f634c851de7e Mon Sep 17 00:00:00 2001 From: Daniel Wiltshire <35703524+danwiltshire@users.noreply.github.com> Date: Thu, 23 Feb 2023 15:02:34 +0000 Subject: [PATCH 4/7] update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 242dd4935c..ac8c98dade 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Add connection attributes to sqlalchemy connect span ([#1608](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1608)) +- Add support for enabling Redis sanitization from environment variable + ([#1690](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1690)) ### Fixed From 6397f58bc036e94686f4060fa40ca2c4f82ddfb4 Mon Sep 17 00:00:00 2001 From: Daniel Wiltshire <35703524+danwiltshire@users.noreply.github.com> Date: Thu, 23 Feb 2023 15:32:46 +0000 Subject: [PATCH 5/7] lint --- .../src/opentelemetry/instrumentation/redis/__init__.py | 8 +++++--- .../tests/test_redis.py | 3 ++- 2 files changed, 7 insertions(+), 4 deletions(-) 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 3c3341340f..7f0c67a7ce 100644 --- a/instrumentation/opentelemetry-instrumentation-redis/src/opentelemetry/instrumentation/redis/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-redis/src/opentelemetry/instrumentation/redis/__init__.py @@ -122,7 +122,7 @@ def response_hook(span, instance, response): _format_command_args, ) from opentelemetry.instrumentation.redis.environment_variables import ( - OTEL_PYTHON_INSTRUMENTATION_SANITIZE_REDIS + OTEL_PYTHON_INSTRUMENTATION_SANITIZE_REDIS, ) from opentelemetry.instrumentation.redis.version import __version__ from opentelemetry.instrumentation.utils import unwrap @@ -309,9 +309,11 @@ def _instrument(self, **kwargs): response_hook=kwargs.get("response_hook"), sanitize_query=kwargs.get( "sanitize_query", - environ.get(OTEL_PYTHON_INSTRUMENTATION_SANITIZE_REDIS, "false").lower().strip() + environ.get( + OTEL_PYTHON_INSTRUMENTATION_SANITIZE_REDIS, "false" + ).lower().strip() == "true", - ) + ), ) def _uninstrument(self, **kwargs): diff --git a/instrumentation/opentelemetry-instrumentation-redis/tests/test_redis.py b/instrumentation/opentelemetry-instrumentation-redis/tests/test_redis.py index 4bf333433e..56a0df6a0a 100644 --- a/instrumentation/opentelemetry-instrumentation-redis/tests/test_redis.py +++ b/instrumentation/opentelemetry-instrumentation-redis/tests/test_redis.py @@ -176,7 +176,8 @@ def test_query_sanitizer_enabled_env(self): RedisInstrumentor().uninstrument() env_patch = mock.patch.dict( - "os.environ", {"OTEL_PYTHON_INSTRUMENTATION_SANITIZE_REDIS": "true"} + "os.environ", + {"OTEL_PYTHON_INSTRUMENTATION_SANITIZE_REDIS": "true"}, ) env_patch.start() RedisInstrumentor().instrument( From 7bb24612c44add1b20fbeca044fb61dcf90ff935 Mon Sep 17 00:00:00 2001 From: Daniel Wiltshire <35703524+danwiltshire@users.noreply.github.com> Date: Thu, 23 Feb 2023 15:59:12 +0000 Subject: [PATCH 6/7] lint --- .../src/opentelemetry/instrumentation/redis/__init__.py | 4 +++- .../instrumentation/redis/environment_variables.py | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) 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 7f0c67a7ce..2d5cf0d3ae 100644 --- a/instrumentation/opentelemetry-instrumentation-redis/src/opentelemetry/instrumentation/redis/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-redis/src/opentelemetry/instrumentation/redis/__init__.py @@ -311,7 +311,9 @@ def _instrument(self, **kwargs): "sanitize_query", environ.get( OTEL_PYTHON_INSTRUMENTATION_SANITIZE_REDIS, "false" - ).lower().strip() + ) + .lower() + .strip() == "true", ), ) 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 index 02b7e42163..750b97445e 100644 --- a/instrumentation/opentelemetry-instrumentation-redis/src/opentelemetry/instrumentation/redis/environment_variables.py +++ b/instrumentation/opentelemetry-instrumentation-redis/src/opentelemetry/instrumentation/redis/environment_variables.py @@ -12,4 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -OTEL_PYTHON_INSTRUMENTATION_SANITIZE_REDIS = "OTEL_PYTHON_INSTRUMENTATION_SANITIZE_REDIS" +OTEL_PYTHON_INSTRUMENTATION_SANITIZE_REDIS = ( + "OTEL_PYTHON_INSTRUMENTATION_SANITIZE_REDIS" +) From f9ac51771d959cab64b91e3e2614e7aa71ab64af Mon Sep 17 00:00:00 2001 From: Daniel Wiltshire <35703524+danwiltshire@users.noreply.github.com> Date: Thu, 23 Feb 2023 17:12:48 +0000 Subject: [PATCH 7/7] lint - hopefully the last time --- .../src/opentelemetry/instrumentation/redis/__init__.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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 2d5cf0d3ae..c1068bda27 100644 --- a/instrumentation/opentelemetry-instrumentation-redis/src/opentelemetry/instrumentation/redis/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-redis/src/opentelemetry/instrumentation/redis/__init__.py @@ -107,8 +107,8 @@ def response_hook(span, instance, response): API --- """ -from os import environ import typing +from os import environ from typing import Any, Collection import redis @@ -116,14 +116,14 @@ def response_hook(span, instance, response): from opentelemetry import trace from opentelemetry.instrumentation.instrumentor import BaseInstrumentor +from opentelemetry.instrumentation.redis.environment_variables import ( + OTEL_PYTHON_INSTRUMENTATION_SANITIZE_REDIS, +) from opentelemetry.instrumentation.redis.package import _instruments from opentelemetry.instrumentation.redis.util import ( _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