This library allows tracing requests made by the Redis library.
Installationpip install opentelemetry-instrumentation-redisUsage
Instrument redis to report Redis queries.
Instrument All ClientsThe easiest way to instrument all redis client instances is by RedisInstrumentor().instrument()
:
from opentelemetry.instrumentation.redis import RedisInstrumentor import redis # Instrument redis RedisInstrumentor().instrument() # This will report a span with the default settings client = redis.StrictRedis(host="localhost", port=6379) client.get("my-key")
Async Redis clients (i.e. redis.asyncio.Redis
) are also instrumented in the same way:
from opentelemetry.instrumentation.redis import RedisInstrumentor import redis.asyncio # Instrument redis RedisInstrumentor().instrument() # This will report a span with the default settings async def redis_get(): client = redis.asyncio.Redis(host="localhost", port=6379) await client.get("my-key")
Note
Calling the instrument
method will instrument the client classes, so any client created after the instrument
call will be instrumented. To instrument only a single client, use RedisInstrumentor.instrument_client()
method.
The RedisInstrumentor.instrument_client()
can instrument a connection instance. This is useful when there are multiple clients with a different redis database index. Or, you might have a different connection pool used for an application function you don’t want instrumented.
from opentelemetry.instrumentation.redis import RedisInstrumentor import redis instrumented_client = redis.Redis() not_instrumented_client = redis.Redis() # Instrument redis RedisInstrumentor.instrument_client(client=instrumented_client) # This will report a span with the default settings instrumented_client.get("my-key") # This will not have a span not_instrumented_client.get("my-key")Request/Response Hooks
from opentelemetry.instrumentation.redis import RedisInstrumentor import redis def request_hook(span, instance, args, kwargs): if span and span.is_recording(): span.set_attribute("custom_user_attribute_from_request_hook", "some-value") def response_hook(span, instance, response): if span and span.is_recording(): span.set_attribute("custom_user_attribute_from_response_hook", "some-value") # Instrument redis with hooks RedisInstrumentor().instrument(request_hook=request_hook, response_hook=response_hook) # This will report a span with the default settings and the custom attributes added from the hooks client = redis.StrictRedis(host="localhost", port=6379) client.get("my-key")API
Bases: BaseInstrumentor
Instruments all Redis/StrictRedis/RedisCluster and async client instances.
tracer_provider (Optional
[TracerProvider
]) – A TracerProvider, defaults to global.
request_hook (Optional
[Callable
[[Span
, Connection
, list
[Any
], dict
[str
, Any
]], None
]]) –
a function with extra user-defined logic to run before performing the request.
The args
is a tuple, where items are command arguments. For example client.set("mykey", "value", ex=5)
would have args
as ('SET', 'mykey', 'value', 'EX', 5)
.
The kwargs
represents occasional options
passed by redis. For example, if you use client.set("mykey", "value", get=True)
, the kwargs
would be {'get': True}
.
response_hook (Optional
[Callable
[[Span
, Connection
, Any
], None
]]) –
a function with extra user-defined logic to run after the request is complete.
The args
represents the response.
Instrument the provided Redis Client. The client can be sync or async. Cluster client is also supported.
client (Redis
| Redis
| RedisCluster
| RedisCluster
) – The redis client.
tracer_provider (Optional
[TracerProvider
]) – A TracerProvider, defaults to global.
request_hook (Optional
[Callable
[[Span
, Connection
, list
[Any
], dict
[str
, Any
]], None
]]) –
a function with extra user-defined logic to run before performing the request.
The args
is a tuple, where items are command arguments. For example client.set("mykey", "value", ex=5)
would have args
as ('SET', 'mykey', 'value', 'EX', 5)
.
The kwargs
represents occasional options
passed by redis. For example, if you use client.set("mykey", "value", get=True)
, the kwargs
would be {'get': True}
.
response_hook (Optional
[Callable
[[Span
, Connection
, Any
], None
]]) –
a function with extra user-defined logic to run after the request is complete.
The args
represents the response.
Disables instrumentation for the given client instance
client (Redis
| Redis
| RedisCluster
| RedisCluster
) – The redis client
Return a list of python packages with versions that the will be instrumented.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4