Skip to main content

Crate otel_instrumentation_redis

Crate otel_instrumentation_redis 

Source
Expand description

OpenTelemetry instrumentation for redis-rs

This crate provides OpenTelemetry tracing instrumentation for the redis-rs crate, enabling distributed tracing and observability for Redis operations.

The instrumentation captures:

  • Redis command names (GET, SET, HGET, etc.)
  • Database system information (Redis)
  • Database index for SELECT operations
  • Error information when operations fail
  • Timing information for performance monitoring

Service names should be configured at the application level through the OpenTelemetry SDK configuration, not within individual instrumentation libraries.

§Features

  • sync (default): Synchronous Redis client instrumentation
  • aio: Asynchronous Redis client instrumentation

§Examples

§Synchronous Usage

use otel_instrumentation_redis::InstrumentedClient;
use redis::Client;

// Create instrumented client
let client = Client::open("redis://127.0.0.1/")?;
let instrumented = InstrumentedClient::new(client);

// Get a connection
let mut conn = instrumented.get_connection()?;

// Use convenience methods with automatic instrumentation
conn.set("key1", "value1")?;
let value: String = conn.get("key1")?;
let exists: bool = conn.exists("key1")?;

// Or use raw commands for maximum flexibility  
let mut cmd = redis::Cmd::new();
cmd.arg("HSET").arg("hash1").arg("field1").arg("value1");
conn.req_command(&cmd)?;

§Asynchronous Usage

use otel_instrumentation_redis::InstrumentedClient;
use redis::Client;

let client = Client::open("redis://127.0.0.1/")?;
let instrumented = InstrumentedClient::new(client);

// Get async connection
let mut conn = instrumented.get_multiplexed_async_connection().await?;

// Use convenience methods
conn.set("async_key", "async_value").await?;
let value: String = conn.get("async_key").await?;

// Hash operations
conn.hset("user:1", "name", "Alice").await?;
let name: String = conn.hget("user:1", "name").await?;

// Set operations
conn.sadd("active_users", "alice").await?;
let is_member: bool = conn.sismember("active_users", "alice").await?;

§Service Name Configuration

Service names should be configured at the application level through the OpenTelemetry SDK configuration:

// Example using opentelemetry-sdk (add as dependency)
// use opentelemetry_sdk::Resource;
// use opentelemetry_semantic_conventions::resource::SERVICE_NAME;

// Configure service name at the application level
// let resource = Resource::new(vec![
//     (SERVICE_NAME, "my-cache-service".into()),
// ]);

// Use this resource when initializing your tracer provider

§OpenTelemetry Attributes

The following attributes are automatically added to spans:

  • db.system: Always set to “redis”
  • db.operation: The Redis command name (GET, SET, HGET, etc.)
  • db.redis.database_index: Database index for SELECT operations
  • error: Set to true when operations fail
  • error.message: Error message when operations fail
  • otel.status_code: “OK” or “ERROR”
  • otel.status_description: Error description for failures

Service name attributes are set at the application level through the OpenTelemetry SDK resource configuration, not by this instrumentation library.

§Performance Considerations

This instrumentation adds minimal overhead:

  • Command name extraction is done via efficient byte parsing
  • Spans are created lazily only when tracing is enabled
  • No heap allocations for successful operations
  • Error information is captured without affecting performance of successful operations

Re-exports§

pub use client::InstrumentedClient;

Modules§

client
A module providing an instrumented wrapper around a Redis client for enhanced observability.
common
Common utilities and types shared across sync and async implementations
prelude
Re-export commonly used types
sync
This module provides an instrumented wrapper around the redis::Connection to enable enhanced tracing and monitoring capabilities for Redis operations. The InstrumentedConnection enables capturing command spans and attributes,