From 2e237cf76010da327daf5737b2c73bd342325b22 Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Mon, 3 Aug 2020 17:48:44 -0700 Subject: [PATCH] Rename db framework packages from "ext" to "instrumentation" (#966) --- .../CHANGELOG.md | 12 ++ .../MANIFEST.in | 9 + .../README.rst | 23 +++ .../setup.cfg | 58 +++++++ .../setup.py | 26 +++ .../instrumentation/redis/__init__.py | 158 ++++++++++++++++++ .../instrumentation/redis/util.py | 57 +++++++ .../instrumentation/redis/version.py | 15 ++ .../tests/__init__.py | 13 ++ .../tests/test_redis.py | 51 ++++++ 10 files changed, 422 insertions(+) create mode 100644 instrumentation/opentelemetry-instrumentation-redis/CHANGELOG.md create mode 100644 instrumentation/opentelemetry-instrumentation-redis/MANIFEST.in create mode 100644 instrumentation/opentelemetry-instrumentation-redis/README.rst create mode 100644 instrumentation/opentelemetry-instrumentation-redis/setup.cfg create mode 100644 instrumentation/opentelemetry-instrumentation-redis/setup.py create mode 100644 instrumentation/opentelemetry-instrumentation-redis/src/opentelemetry/instrumentation/redis/__init__.py create mode 100644 instrumentation/opentelemetry-instrumentation-redis/src/opentelemetry/instrumentation/redis/util.py create mode 100644 instrumentation/opentelemetry-instrumentation-redis/src/opentelemetry/instrumentation/redis/version.py create mode 100644 instrumentation/opentelemetry-instrumentation-redis/tests/__init__.py create mode 100644 instrumentation/opentelemetry-instrumentation-redis/tests/test_redis.py diff --git a/instrumentation/opentelemetry-instrumentation-redis/CHANGELOG.md b/instrumentation/opentelemetry-instrumentation-redis/CHANGELOG.md new file mode 100644 index 000000000..79c4e6bf9 --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-redis/CHANGELOG.md @@ -0,0 +1,12 @@ +# Changelog + +## Unreleased + +- Change package name to opentelemetry-instrumentation-redis + ([#999](https://github.com/open-telemetry/opentelemetry-python/pull/999)) + +## 0.7b1 + +Released 2020-05-12 + +- Initial release \ No newline at end of file diff --git a/instrumentation/opentelemetry-instrumentation-redis/MANIFEST.in b/instrumentation/opentelemetry-instrumentation-redis/MANIFEST.in new file mode 100644 index 000000000..aed3e3327 --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-redis/MANIFEST.in @@ -0,0 +1,9 @@ +graft src +graft tests +global-exclude *.pyc +global-exclude *.pyo +global-exclude __pycache__/* +include CHANGELOG.md +include MANIFEST.in +include README.rst +include LICENSE diff --git a/instrumentation/opentelemetry-instrumentation-redis/README.rst b/instrumentation/opentelemetry-instrumentation-redis/README.rst new file mode 100644 index 000000000..289a0be0c --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-redis/README.rst @@ -0,0 +1,23 @@ +OpenTelemetry Redis Instrumentation +=================================== + +|pypi| + +.. |pypi| image:: https://badge.fury.io/py/opentelemetry-instrumentation-redis.svg + :target: https://pypi.org/project/opentelemetry-instrumentation-redis/ + +This library allows tracing requests made by the Redis library. + +Installation +------------ + +:: + + pip install opentelemetry-instrumentation-redis + + +References +---------- + +* `OpenTelemetry Redis Instrumentation `_ +* `OpenTelemetry Project `_ diff --git a/instrumentation/opentelemetry-instrumentation-redis/setup.cfg b/instrumentation/opentelemetry-instrumentation-redis/setup.cfg new file mode 100644 index 000000000..27793a5be --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-redis/setup.cfg @@ -0,0 +1,58 @@ +# 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. +# +[metadata] +name = opentelemetry-instrumentation-redis +description = OpenTelemetry Redis instrumentation +long_description = file: README.rst +long_description_content_type = text/x-rst +author = OpenTelemetry Authors +author_email = cncf-opentelemetry-contributors@lists.cncf.io +url = https://github.com/open-telemetry/opentelemetry-python/tree/master/instrumentation/opentelemetry-instrumentation-redis +platforms = any +license = Apache-2.0 +classifiers = + Development Status :: 4 - Beta + Intended Audience :: Developers + License :: OSI Approved :: Apache Software License + Programming Language :: Python + Programming Language :: Python :: 3 + Programming Language :: Python :: 3.4 + Programming Language :: Python :: 3.5 + Programming Language :: Python :: 3.6 + Programming Language :: Python :: 3.7 + Programming Language :: Python :: 3.8 + +[options] +python_requires = >=3.4 +package_dir= + =src +packages=find_namespace: +install_requires = + opentelemetry-api == 0.12.dev0 + opentelemetry-instrumentation == 0.12.dev0 + redis >= 2.6 + wrapt >= 1.12.1 + +[options.extras_require] +test = + opentelemetry-test == 0.12.dev0 + opentelemetry-sdk == 0.12.dev0 + +[options.packages.find] +where = src + +[options.entry_points] +opentelemetry_instrumentor = + redis = opentelemetry.instrumentation.redis:RedisInstrumentor diff --git a/instrumentation/opentelemetry-instrumentation-redis/setup.py b/instrumentation/opentelemetry-instrumentation-redis/setup.py new file mode 100644 index 000000000..df80a8fd1 --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-redis/setup.py @@ -0,0 +1,26 @@ +# 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. +import os + +import setuptools + +BASE_DIR = os.path.dirname(__file__) +VERSION_FILENAME = os.path.join( + BASE_DIR, "src", "opentelemetry", "instrumentation", "redis", "version.py" +) +PACKAGE_INFO = {} +with open(VERSION_FILENAME) as f: + exec(f.read(), PACKAGE_INFO) + +setuptools.setup(version=PACKAGE_INFO["__version__"]) diff --git a/instrumentation/opentelemetry-instrumentation-redis/src/opentelemetry/instrumentation/redis/__init__.py b/instrumentation/opentelemetry-instrumentation-redis/src/opentelemetry/instrumentation/redis/__init__.py new file mode 100644 index 000000000..fef856041 --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-redis/src/opentelemetry/instrumentation/redis/__init__.py @@ -0,0 +1,158 @@ +# 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. +# +""" +Instrument `redis`_ to report Redis queries. + +There are two options for instrumenting code. The first option is to use the +``opentelemetry-instrumentation`` executable which will automatically +instrument your Redis client. The second is to programmatically enable +instrumentation via the following code: + +.. _redis: https://pypi.org/project/redis/ + +Usage +----- + +.. code:: python + + from opentelemetry import trace + from opentelemetry.instrumentation.redis import RedisInstrumentor + from opentelemetry.sdk.trace import TracerProvider + import redis + + trace.set_tracer_provider(TracerProvider()) + + # Instrument redis + RedisInstrumentor().instrument(tracer_provider=trace.get_tracer_provider()) + + # This will report a span with the default settings + client = redis.StrictRedis(host="localhost", port=6379) + client.get("my-key") + +API +--- +""" + +import redis +from wrapt import ObjectProxy, wrap_function_wrapper + +from opentelemetry import trace +from opentelemetry.instrumentation.instrumentor import BaseInstrumentor +from opentelemetry.instrumentation.redis.util import ( + _extract_conn_attributes, + _format_command_args, +) +from opentelemetry.instrumentation.redis.version import __version__ +from opentelemetry.instrumentation.utils import unwrap + +_DEFAULT_SERVICE = "redis" +_RAWCMD = "db.statement" +_CMD = "redis.command" + + +def _set_connection_attributes(span, conn): + for key, value in _extract_conn_attributes( + conn.connection_pool.connection_kwargs + ).items(): + span.set_attribute(key, value) + + +def _traced_execute_command(func, instance, args, kwargs): + tracer = getattr(redis, "_opentelemetry_tracer") + query = _format_command_args(args) + with tracer.start_as_current_span(_CMD) as span: + span.set_attribute("service", tracer.instrumentation_info.name) + span.set_attribute(_RAWCMD, query) + _set_connection_attributes(span, instance) + span.set_attribute("redis.args_length", len(args)) + return func(*args, **kwargs) + + +def _traced_execute_pipeline(func, instance, args, kwargs): + tracer = getattr(redis, "_opentelemetry_tracer") + + cmds = [_format_command_args(c) for c, _ in instance.command_stack] + resource = "\n".join(cmds) + + with tracer.start_as_current_span(_CMD) as span: + span.set_attribute("service", tracer.instrumentation_info.name) + span.set_attribute(_RAWCMD, resource) + _set_connection_attributes(span, instance) + span.set_attribute( + "redis.pipeline_length", len(instance.command_stack) + ) + return func(*args, **kwargs) + + +class RedisInstrumentor(BaseInstrumentor): + """An instrumentor for Redis + See `BaseInstrumentor` + """ + + def _instrument(self, **kwargs): + tracer_provider = kwargs.get( + "tracer_provider", trace.get_tracer_provider() + ) + setattr( + redis, + "_opentelemetry_tracer", + tracer_provider.get_tracer(_DEFAULT_SERVICE, __version__), + ) + + if redis.VERSION < (3, 0, 0): + wrap_function_wrapper( + "redis", "StrictRedis.execute_command", _traced_execute_command + ) + wrap_function_wrapper( + "redis.client", + "BasePipeline.execute", + _traced_execute_pipeline, + ) + wrap_function_wrapper( + "redis.client", + "BasePipeline.immediate_execute_command", + _traced_execute_command, + ) + else: + wrap_function_wrapper( + "redis", "Redis.execute_command", _traced_execute_command + ) + wrap_function_wrapper( + "redis.client", "Pipeline.execute", _traced_execute_pipeline + ) + wrap_function_wrapper( + "redis.client", + "Pipeline.immediate_execute_command", + _traced_execute_command, + ) + + def _uninstrument(self, **kwargs): + if redis.VERSION < (3, 0, 0): + unwrap(redis.StrictRedis, "execute_command") + unwrap(redis.StrictRedis, "pipeline") + unwrap(redis.Redis, "pipeline") + unwrap( + redis.client.BasePipeline, # pylint:disable=no-member + "execute", + ) + unwrap( + redis.client.BasePipeline, # pylint:disable=no-member + "immediate_execute_command", + ) + else: + unwrap(redis.Redis, "execute_command") + unwrap(redis.Redis, "pipeline") + unwrap(redis.client.Pipeline, "execute") + unwrap(redis.client.Pipeline, "immediate_execute_command") diff --git a/instrumentation/opentelemetry-instrumentation-redis/src/opentelemetry/instrumentation/redis/util.py b/instrumentation/opentelemetry-instrumentation-redis/src/opentelemetry/instrumentation/redis/util.py new file mode 100644 index 000000000..289513408 --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-redis/src/opentelemetry/instrumentation/redis/util.py @@ -0,0 +1,57 @@ +# 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. +# +""" +Some utils used by the redis integration +""" + + +def _extract_conn_attributes(conn_kwargs): + """ Transform redis conn info into dict """ + attributes = { + "db.type": "redis", + "db.instance": conn_kwargs.get("db", 0), + } + try: + attributes["db.url"] = "redis://{}:{}".format( + conn_kwargs["host"], conn_kwargs["port"] + ) + except KeyError: + pass # don't include url attribute + + return attributes + + +def _format_command_args(args): + """Format command arguments and trim them as needed""" + value_max_len = 100 + value_too_long_mark = "..." + cmd_max_len = 1000 + length = 0 + out = [] + for arg in args: + cmd = str(arg) + + if len(cmd) > value_max_len: + cmd = cmd[:value_max_len] + value_too_long_mark + + if length + len(cmd) > cmd_max_len: + prefix = cmd[: cmd_max_len - length] + out.append("%s%s" % (prefix, value_too_long_mark)) + break + + out.append(cmd) + length += len(cmd) + + return " ".join(out) diff --git a/instrumentation/opentelemetry-instrumentation-redis/src/opentelemetry/instrumentation/redis/version.py b/instrumentation/opentelemetry-instrumentation-redis/src/opentelemetry/instrumentation/redis/version.py new file mode 100644 index 000000000..780a92b6a --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-redis/src/opentelemetry/instrumentation/redis/version.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. + +__version__ = "0.12.dev0" diff --git a/instrumentation/opentelemetry-instrumentation-redis/tests/__init__.py b/instrumentation/opentelemetry-instrumentation-redis/tests/__init__.py new file mode 100644 index 000000000..b0a6f4284 --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-redis/tests/__init__.py @@ -0,0 +1,13 @@ +# 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. diff --git a/instrumentation/opentelemetry-instrumentation-redis/tests/test_redis.py b/instrumentation/opentelemetry-instrumentation-redis/tests/test_redis.py new file mode 100644 index 000000000..86b2459c1 --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-redis/tests/test_redis.py @@ -0,0 +1,51 @@ +# 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. +from unittest import mock + +import redis + +from opentelemetry.instrumentation.redis import RedisInstrumentor +from opentelemetry.test.test_base import TestBase + + +class TestRedis(TestBase): + def test_instrument_uninstrument(self): + redis_client = redis.Redis() + RedisInstrumentor().instrument(tracer_provider=self.tracer_provider) + + with mock.patch.object(redis_client, "connection"): + redis_client.get("key") + + spans = self.memory_exporter.get_finished_spans() + self.assertEqual(len(spans), 1) + self.memory_exporter.clear() + + # Test uninstrument + RedisInstrumentor().uninstrument() + + with mock.patch.object(redis_client, "connection"): + redis_client.get("key") + + spans = self.memory_exporter.get_finished_spans() + self.assertEqual(len(spans), 0) + self.memory_exporter.clear() + + # Test instrument again + RedisInstrumentor().instrument() + + with mock.patch.object(redis_client, "connection"): + redis_client.get("key") + + spans = self.memory_exporter.get_finished_spans() + self.assertEqual(len(spans), 1)