Boost Your NestJS Observability with RedisX
Source: Dev.to

Getting RedisX Rolling in Your NestJS App
Let’s get down to business and set up RedisX for serious observability in your NestJS app. First, install the required packages:
npm install @nestjs-redisx/core @nestjs-redisx/metrics @nestjs-redisx/tracing
Next, modify your AppModule to include MetricsPlugin and TracingPlugin:
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { RedisModule } from '@nestjs-redisx/core';
import { MetricsPlugin } from '@nestjs-redisx/metrics';
import { TracingPlugin } from '@nestjs-redisx/tracing';
@Module({
imports: [
ConfigModule.forRoot({ isGlobal: true }),
RedisModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
plugins: [
new MetricsPlugin({ enabled: true, exposeEndpoint: true }),
new TracingPlugin({ serviceName: 'nestjs-app' }),
],
useFactory: (config: ConfigService) => ({
clients: {
type: 'single',
host: config.get('REDIS_HOST', 'localhost'),
port: config.get('REDIS_PORT', 6379),
},
}),
}),
],
})
export class AppModule {}
Prometheus Metrics: The Lowdown
With MetricsPlugin enabled, your app exposes a /metrics endpoint that provides Redis operation statistics such as cache hits, misses, and lock contentions.
Configure Prometheus to scrape this endpoint. A minimal prometheus.yml might look like:
global:
scrape_interval: 15s
scrape_configs:
- job_name: 'nestjs-app'
metrics_path: '/metrics'
static_configs:
- targets: ['localhost:3000']
OpenTelemetry Tracing: Going Deeper
TracingPlugin leverages OpenTelemetry to trace every Redis operation. You can create custom spans in your NestJS services:
import { Injectable, Inject } from '@nestjs/common';
import { TRACING_SERVICE, ITracingService } from '@nestjs-redisx/tracing';
@Injectable()
export class UserService {
constructor(@Inject(TRACING_SERVICE) private readonly tracing: ITracingService) {}
async getUser(id: string): Promise {
return this.tracing.withSpan('user.get', async () => {
this.tracing.setAttribute('user.id', id);
const user = await this.userRepo.findById(id);
this.tracing.addEvent('user.found', { 'user.email': user.email });
return user;
});
}
}
Wrapping It Up
By integrating RedisX’s MetricsPlugin and TracingPlugin into your NestJS workflow, you gain deep visibility into your application’s behavior, enabling proactive performance tuning and faster issue resolution.
For more details on RedisX plugins, visit the RedisX documentation.