mirror of
https://github.com/open-telemetry/opentelemetry-python-contrib.git
synced 2025-07-29 13:12:39 +08:00
Rename db framework packages from "ext" to "instrumentation" (#966)
This commit is contained in:
@ -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
|
@ -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
|
@ -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 <https://opentelemetry-python.readthedocs.io/en/latest/ext/opentelemetry-instrumentation-redis/opentelemetry-instrumentation-redis.html>`_
|
||||
* `OpenTelemetry Project <https://opentelemetry.io/>`_
|
@ -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
|
26
instrumentation/opentelemetry-instrumentation-redis/setup.py
Normal file
26
instrumentation/opentelemetry-instrumentation-redis/setup.py
Normal file
@ -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__"])
|
@ -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")
|
@ -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)
|
@ -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"
|
@ -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.
|
@ -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)
|
Reference in New Issue
Block a user