Observability¶
coredis supports emitting telemetry traces and metrics through OpenTelemetry.
Installation¶
Install the optional dependencies:
pip install "coredis[otel]"
Enable instrumentation¶
Enable OpenTelemetry instrumentation by setting COREDIS_OTEL_ENABLED:
export COREDIS_OTEL_ENABLED=true
or by setting coredis.Config.otel_enabled to True
Example¶
The example below configures in-memory exporters for both traces and metrics and then executes a command so you can inspect emitted telemetry immediately.
import anyio
import coredis
from opentelemetry import metrics, trace
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics.export import InMemoryMetricReader
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter
async def main() -> None:
coredis.Config.otel_enabled = True
coredis.Config.otel_capture_command_args = True
span_exporter = InMemorySpanExporter()
tracer_provider = TracerProvider()
tracer_provider.add_span_processor(SimpleSpanProcessor(span_exporter))
trace.set_tracer_provider(tracer_provider)
metric_reader = InMemoryMetricReader()
metrics.set_meter_provider(MeterProvider(metric_readers=[metric_reader]))
client = coredis.Redis(host="127.0.0.1", port=6379)
async with client:
await client.set("example:key", "value")
await client.get("example:key")
spans = span_exporter.get_finished_spans()
metric_data = metric_reader.get_metrics_data()
print(f"spans={len(spans)} metric_resources={len(metric_data.resource_metrics)}")
anyio.run(main)
Traces¶
When enabled, coredis emits:
A span for a single command execution:
${COMMAND NAME}A single aggregate span for a pipeline:
PIPELINEA single aggregate span for a transaction pipeline:
MULTI
Span attributes include the following recommended attributes:
db.system.nameAlways set to
redisdb.namespaceThe database index number associated with the connection used when performing the operation
db.operation.nameEither the command name for a single command execution, or
MULTIfor transactions andPIPELINEfor non transactional pipelines.Examples:
SET GET
db.operation.batch.sizeThe number of commands in a transaction or pipeline
db.query.textThe full redis RESP command equivalent with all user input values masked (key names are still shown)
Note
If the span is created for a transaction or pipeline this attribute will contain all the commands in the batch.
Examples:
SET key1 ? GET key1
network.peer.hostnameAddress of the redis node where the operation was performed
network.peer.portPort of the redis node
server.addressAddress of the redis node where the operation was performed
server.portPort of the redis node where the operation was performed
db.client.connection.pool.nameName of the connection pool associated with the operation
Metrics¶
When enabled, the following metrics are emitted:
db.client.operation.durationHistogram (seconds)
db.client.connection.pending_requestsUp/Down Counter
db.client.connection.countUp/Down Counter
db.client.connection.maxUp/Down Counter
db.client.connection.timeoutsCounter
db.client.connection.create_timeHistogram (seconds)
db.client.connection.wait_timeHistogram (seconds)
db.client.connection.use_timeHistogram (seconds)
Span events¶
In addition to span attributes, coredis emits span events for retry attempts.
retryEmitted when a command execution is retried by the configured retry policy. This event includes the following attributes:
exception.type: Fully-qualified exception type where available.exception.message: Exception message if present.
Additionally, any uncaught exceptions raised while executing a command will be emitted as standard exception span events.
Troubleshooting¶
If telemetry is enabled but no data appears, verify OpenTelemetry SDK providers are configured in your application before issuing redis commands.
If OpenTelemetry dependencies are missing, coredis falls back to a noop telemetry provider and emits a warning.
Configuration¶
COREDIS_OTEL_ENABLEDorcoredis.Config.otel_enabledWhether telemetry is enabled or not.
COREDIS_OTEL_CAPTURE_COMMAND_ARGSorcoredis.Config.otel_capture_command_argsIf set to
Truethe argument values for each reported command will be included in the span attributes underdb.query.text. Any user provided values other than keys will be replaced with?.COREDIS_OTEL_DISABLED_COMMANDSorcoredis.Config.otel_disabled_commandsA list of command names to disable emitting spans for. When using an environment variable, provide a comma-separated list (for example:
PING,GET,SET). Command matching is case-insensitive.
Note
The following strings (case insensitive) are accepted as truthy values when using
environment variables: true, t, 1