Observability is one of those things you can't ignore if you're managing modern applications. It's the secret sauce that gives developers a peek into how their systems are really performing. If you're working with NestJS and Redis, the RedisX toolkit is your new best friend for upping your observability game with Prometheus metrics and OpenTelemetry tracing.
Getting RedisX Rolling in Your NestJS App
Let's get down to business and set up RedisX for some serious observability in your NestJS app. First things first, you'll need to install a few packages. Fire up your terminal and run:
npm install @nestjs-redisx/core @nestjs-redisx/metrics @nestjs-redisx/tracing
Once that's sorted, it's time to tweak your AppModule. You'll want to add MetricsPlugin and TracingPlugin to get Prometheus metrics and OpenTelemetry tracing without breaking a sweat.
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 in place, your app now has a /metrics endpoint ready to roll. This little endpoint is a goldmine of info on Redis operations—think cache hits, misses, and lock contentions.
If you're using Prometheus, you need to set it up to scrape this endpoint. Your prometheus.yml should look something like this:
global:
scrape_interval: 15s
scrape_configs:
- job_name: 'nestjs-app'
metrics_path: '/metrics'
static_configs:
- targets: ['localhost:3000']
OpenTelemetry Tracing: Going Deeper
TracingPlugin in RedisX uses OpenTelemetry to keep track of every Redis operation. It's like having a magnifying glass on every command—great for spotting performance bottlenecks.
Here's a quick example of how you might use custom spans in a NestJS service:
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<unknown> {
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 bringing RedisX’s MetricsPlugin and TracingPlugin into your NestJS workflow, you're not just monitoring your app—you're setting it up for success. These tools give you the insights you need to keep things running smoothly and fix issues before they become problems.
Want more details on RedisX plugins? Check out the RedisX documentation.
Top comments (0)