Skip to content

Added OTel instrumentation and metrics export for sync client#3954

Merged
vladvildanov merged 39 commits intomasterfrom
feat/observability
Feb 16, 2026
Merged

Added OTel instrumentation and metrics export for sync client#3954
vladvildanov merged 39 commits intomasterfrom
feat/observability

Conversation

@vladvildanov
Copy link
Collaborator

@vladvildanov vladvildanov commented Feb 12, 2026

Description of change

This PR adds optional OpenTelemetry (OTel) instrumentation to redis-py, enabling automatic metrics collection and export for Redis operations. The implementation follows a singleton pattern where observability is initialised once globally, and all Redis clients automatically collect metrics without additional configuration.

It provides:

  • Automatic metrics collection for commands, connections, client-side caching, pub/sub, and streaming operations
  • Configurable metric groups to enable/disable specific categories
  • Privacy controls to hide sensitive information (channel names, stream names)
  • Zero overhead when disabled - no performance impact if OTel dependencies aren't installed
  • Uses global MeterProvider - integrates with your existing OTel setup

How to Enable

Install OTel dependencies:

pip install redis[otel]

Set up your OTel exporter and initialize redis-py observability:

from opentelemetry import metrics
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader
from opentelemetry.exporter.otlp.proto.http.metric_exporter import OTLPMetricExporter
Configure your OTel exporter
exporter = OTLPMetricExporter(endpoint='http://localhost:4318/v1/metrics')
reader = PeriodicExportingMetricReader(exporter, export_interval_millis=10000)
provider = MeterProvider(metric_readers=[reader])
metrics.set_meter_provider(provider)
Initialize redis-py observability (once at app startup)
from redis.observability.providers import get_observability_instance
from redis.observability.config import OTelConfig, MetricGroup

otel = get_observability_instance()
otel.init(OTelConfig())
All Redis clients now automatically collect metrics
import redis
r = redis.Redis(host='localhost', port=6379)
r.set('key', 'value')  # Metrics collected automatically
Optional: Customise metric groups:
from redis.observability.config import MetricGroup

otel = get_observability_instance()
otel.init(OTelConfig(
    metric_groups=[
        MetricGroup.COMMAND,
        MetricGroup.PUBSUB,
        MetricGroup.STREAMING,
        MetricGroup.CSC,
        MetricGroup.CONNECTION_BASIC,
        MetricGroup.CONNECTION_ADVANCED,
        MetricGroup.RESILIENCY,
    ]
))

Pull Request check-list

Please make sure to review and check all of these items:

  • Do tests and lints pass with this change?
  • Do the CI tests pass with this change (enable it first in your forked repo and wait for the github action build to finish)?
  • Is the new or changed code fully tested?
  • Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?
  • Is there an example added to the examples folder (if applicable)?

vladvildanov and others added 15 commits December 8, 2025 14:27
* Added intrastructure and integration point with OTel

* Added check for enabled metric groups

* Applied comments
* Added stadalone client metrics export

* Added support for cluster client

* Removed unused dispatchers and test
* Added stadalone client metrics export

* Added support for cluster client

* Added error metric export on fail commands for standalone client

* Removed unused dispatchers and test

* Added on error metrics export for cluster

* Added export of maint notification count metric

* Apply comments

* Remove redundant Union
* Added export of connection basic metrics

* Added new error category attribute

* Update tests/test_observability/test_recorder.py

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update tests/test_observability/test_recorder.py

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Apply comments

* Applied comments

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* Added export of connection basic metrics

* Added new error category attribute

* Added pub/sub and stream lag metrics export

* Added metric export form XREAD

* Update redis/client.py

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update redis/event.py

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Applied comments

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* Added export of connection basic metrics

* Added new error category attribute

* Added pub/sub and stream lag metrics export

* Added metric export form XREAD

* Added benchmark tests

* Added error output

* Added env variable for otel host

* Fixed endpoint

* Added iterations and results averaging

* Use unsecured gRPC

* Added --with-command-metrics argument

* Removed incorrect identation

* Update benchmarks/otel_benchmark.py

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update benchmarks/otel_benchmark.py

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update benchmarks/otel_benchmark.py

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update benchmarks/otel_benchmark.py

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update benchmarks/otel_benchmark.py

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* Added export of connection advanced metrics

* Update tests/test_connection_pool.py

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update redis/observability/metrics.py

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Refactored kwargs instead of args

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* Added export of connection advanced metrics

* Added CSC metrics export

* Revert changes

* Added observable gauge registry and refactored observables metric export

* Fixed case with trackin non-cachable cache tracking
* Added full benchmark tests + fixes

* Added Memory and CPU usage calculation
…3934)

* Removed events dispatching overhead, refactored metrics attributes

* Fixed incorrect retry attempts, changed indexes to named parameters

* Fixed issue with connection attribute and metric recording
* Added async recorders and registry

* Removed async registry

* Added more coverage
* Added async recorders and registry

* Added bucket override configuration

* Removed async registry

* Added more coverage

* Fixed version constraints, removed ubused imports

* Moved module desription on top of file
@vladvildanov vladvildanov added the feature New feature label Feb 12, 2026
Copy link
Contributor

@elena-kolevska elena-kolevska left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to use standard UCUM units, as defined in the SemConv

Signed-off-by: Elena Kolevska <elena@kolevska.com>
Signed-off-by: Elena Kolevska <elena@kolevska.com>
Signed-off-by: Elena Kolevska <elena@kolevska.com>
Signed-off-by: Elena Kolevska <elena@kolevska.com>
@vladvildanov
Copy link
Collaborator Author

augment review

@augmentcode
Copy link

augmentcode bot commented Feb 13, 2026

This pull request is too large for Augment to review. The PR exceeds the maximum size limit of 100000 tokens (approximately 400000 characters) for automated code review. Please consider breaking this PR into smaller, more focused changes.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds comprehensive OpenTelemetry (OTel) instrumentation to redis-py, enabling automatic metrics collection for Redis operations. The implementation follows a singleton pattern where observability is initialized once globally, and all Redis clients automatically collect metrics.

Changes:

  • Adds new redis/observability module with configuration, metrics collection, and provider management
  • Integrates metrics recording into command execution, connection management, PubSub, streaming, and caching
  • Adds error type categorization to exceptions for better error metrics
  • Updates retry mechanism to support failure count tracking
  • Fixes test assertions to use e.value instead of str(e)
  • Adds comprehensive unit and integration tests for metrics recording

Reviewed changes

Copilot reviewed 39 out of 41 changed files in this pull request and generated 37 comments.

Show a summary per file
File Description
tests/test_ssl.py Fixed pytest assertions to use e.value instead of str(e)
tests/test_search.py Fixed pytest assertions to use e.value instead of str(e)
tests/test_pubsub.py Fixed assertions + added metrics recording tests for PubSub
tests/test_pipeline.py Added comprehensive metrics recording tests for Pipeline
tests/test_maint_notifications.py Updated tests for new notification signature + metrics tests
tests/test_credentials.py Added connection attributes (host, port, db) to mock connections
tests/test_connection_pool.py Added tests for connection count observability
tests/test_connection.py Added tests for connection pool methods and CSC metrics
tests/test_cluster_transaction.py Added metrics recording tests for cluster transactions
tests/test_cluster.py Added comprehensive cluster and pipeline metrics tests
tests/test_client.py New file with Redis client metrics recording tests
tests/test_cache.py Added CacheProxy tests for CSC eviction metrics
tests/test_observability/*.py New unit tests for observability modules
redis/observability/*.py New observability module with config, metrics, providers, attributes, registry
redis/client.py Integrated metrics recording into command execution and PubSub
redis/connection.py Added connection metrics and CSC metrics recording
redis/cache.py Added CacheProxy for eviction metrics
redis/cluster.py Integrated metrics for cluster operations
redis/event.py Added connection count observability initialization
redis/exceptions.py Added error type categorization
redis/retry.py Added failure count support
redis/maint_notifications.py Added maintenance notification metrics
redis/commands/core.py Added PubSub and streaming metrics
pyproject.toml Added otel optional dependencies
docs/opentelemetry.rst Added comprehensive documentation

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

vladvildanov and others added 3 commits February 13, 2026 14:31
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link
Collaborator

@petyaslavova petyaslavova left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@vladvildanov vladvildanov merged commit b41e47d into master Feb 16, 2026
177 of 179 checks passed
@vladvildanov vladvildanov deleted the feat/observability branch February 16, 2026 15:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants