Skip to content

Metrics Plugin

Prometheus-compatible metrics for monitoring Redis operations, cache performance, and plugin health.

Overview

The Metrics Plugin provides observability into your Redis operations with Prometheus-format metrics.

QuestionWithout MetricsWith Metrics Plugin
Is Redis slow?Unknownp99 latency visible
Cache hit rate?UnknownHit rate visible
Lock contention?GuessworkMetrics available
Error rate?Check logsError counters

Key Features

  • Prometheus Format — Standard metrics format compatible with Prometheus-based systems
  • Automatic Instrumentation — Plugins emit metrics when enabled
  • Built-in Endpoint — HTTP endpoint for Prometheus scraping
  • Custom Metrics — API for adding application-specific metrics
  • Grafana Dashboards — Example dashboards for visualizations
  • Alert Templates — Example AlertManager configurations

Installation

bash
npm install @nestjs-redisx/core @nestjs-redisx/metrics ioredis
bash
npm install @nestjs-redisx/core @nestjs-redisx/metrics redis

Basic Configuration

typescript
import { Module } from '@nestjs/common';
import { RedisModule } from '@nestjs-redisx/core';
import { MetricsPlugin } from '@nestjs-redisx/metrics';

@Module({
  imports: [
    RedisModule.forRoot({
      clients: {
        host: 'localhost',
        port: 6379,
      },
      plugins: [
        new MetricsPlugin({
          prefix: 'redisx_',
          endpoint: '/metrics',
          defaultLabels: {
            service: 'my-service',
          },
        }),
      ],
    }),
  ],
})
export class AppModule {}

Viewing Metrics

bash
curl http://localhost:3000/metrics
yaml
# HELP redisx_cache_hits_total Total cache hits
# TYPE redisx_cache_hits_total counter
redisx_cache_hits_total{layer="l1"} 15234

# HELP redisx_cache_misses_total Total cache misses
# TYPE redisx_cache_misses_total counter
redisx_cache_misses_total{layer="l1"} 892

# HELP redisx_command_duration_seconds Redis command latency in seconds
# TYPE redisx_command_duration_seconds histogram
redisx_command_duration_seconds_bucket{command="GET",client="default",le="0.001"} 1523
redisx_command_duration_seconds_bucket{command="GET",client="default",le="0.005"} 2891

Available Metrics

Command Metrics

MetricTypeLabelsDescription
commands_totalCountercommand, client, statusTotal Redis commands
command_duration_secondsHistogramcommand, clientCommand latency
connections_activeGaugeclientActive connections
errors_totalCounterclient, error_typeTotal errors

Cache Metrics

MetricTypeLabelsDescription
cache_hits_totalCounterlayerCache hits
cache_misses_totalCounterlayerCache misses
cache_hit_ratioGaugelayerHit ratio (0-1)
cache_sizeGaugelayerCurrent cache size
cache_stampede_prevented_totalCounterStampede preventions

Lock Metrics

MetricTypeLabelsDescription
lock_acquisitions_totalCounterstatusLock attempts
lock_wait_duration_secondsHistogramWait time
lock_hold_duration_secondsHistogramHold time
locks_activeGaugeActive lock count

Rate Limit Metrics

MetricTypeLabelsDescription
ratelimit_requests_totalCounterstatusRate limit requests

Stream Metrics

MetricTypeLabelsDescription
stream_messages_published_totalCounterstreamMessages published
stream_messages_consumed_totalCounterstream, group, statusMessages consumed
stream_publish_duration_secondsHistogramstreamPublish latency
stream_publish_errors_totalCounterstreamPublish errors
stream_processing_duration_secondsHistogramstream, groupProcessing duration

Idempotency Metrics

MetricTypeLabelsDescription
idempotency_requests_totalCounterstatusIdempotency requests
idempotency_duration_secondsHistogramCheck duration

Architecture

Documentation

TopicDescription
Core ConceptsUnderstanding metrics collection
ConfigurationConfiguration reference
PrometheusPrometheus integration
Grafana DashboardsExample visualizations
Plugin MetricsMetrics from each plugin
Custom MetricsAdding application metrics
AlertingAlertManager configuration
TroubleshootingDebugging common issues

Released under the MIT License.