RedisX 메트릭을 사용하여 NestJS에서 관찰 가능성 높이기

작성자

카테고리:

← 피드로
DEV Community · Suren Krmoian · 2026-06-13 개발(SW)
Cover image for Boosting Observability in NestJS with RedisX Metrics

Suren Krmoian

Observability isn’t just a buzzword; it’s a necessity, especially when diving into distributed systems. If you’re using NestJS, you might want to take a look at RedisX. It’s a modular toolkit that can boost the observability of your applications. A standout feature? The Metrics Plugin. It meshes well with Prometheus, delivering insights into Redis operations in your NestJS setup.

Getting RedisX Metrics Rolling in NestJS

So, first things first. To harness the power of RedisX Metrics, you need to set up your NestJS app with RedisX. This means installing some packages and configuring the RedisModule with the MetricsPlugin.

Hit your terminal and run:

npm install @nestjs-redisx/core @nestjs-redisx/metrics

Enter fullscreen mode Exit fullscreen mode

Now, let’s tweak your AppModule. You want it to use RedisModule with MetricsPlugin:

import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { RedisModule } from '@nestjs-redisx/core';
import { MetricsPlugin } from '@nestjs-redisx/metrics';

@Module({
  imports: [
    ConfigModule.forRoot({ isGlobal: true }),
    RedisModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      plugins: [
        new MetricsPlugin({
          prefix: 'redisx_',
          endpoint: '/metrics',
          defaultLabels: { service: 'my-service' }
        })
      ],
      useFactory: (config: ConfigService) => ({
        clients: {
          host: config.get('REDIS_HOST', 'localhost'),
          port: config.get('REDIS_PORT', 6379),
        },
      }),
    }),
  ],
})
export class AppModule {}

Enter fullscreen mode Exit fullscreen mode

Prometheus Metrics: What You Get

With MetricsPlugin set up, your app now exposes a /metrics endpoint. Prometheus can scrape this endpoint, dishing out detailed metrics about your Redis operations. Here’s a snapshot of what you get:

  • redisx_cache_hits_total: Tracks total cache hits.
  • redisx_lock_acquired_total: Total locks acquired.
  • redisx_redis_commands_total: Total Redis commands run.

Making Prometheus Work for You

To get those insights, set up Prometheus to scrape your /metrics endpoint. Check your prometheus.yml and add a scrape config like this:

scrape_configs:
  - job_name: 'nestjs-app'
    metrics_path: '/metrics'
    static_configs:
      - targets: ['localhost:3000']

Enter fullscreen mode Exit fullscreen mode

This ensures Prometheus gathers metrics from your NestJS app, helping you monitor Redis interactions closely.

Insights: What to Do Next?

With Prometheus grabbing your metrics, visualize and analyze them with Grafana or similar tools. This setup helps you see Redis usage patterns, spot bottlenecks, and fine-tune your app’s performance.

Using NestJS RedisX’s MetricsPlugin isn’t just about plugging in Prometheus metrics. It’s about getting a full view of your Redis operations right out of the box. As you grow and scale, these insights will be key to keeping your systems running smoothly.

For more, check the NestJS RedisX Metrics documentation.

원문에서 계속 ↗

코멘트

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다