DEV Community

Netanel Avraham
Netanel Avraham

Posted on

[Open Source] Simplify Metrics Reporting in NestJS with a Zero-Dependency-Injection Global Reporter

Hey everyone! 👋

I'd like to share an open-source package I recently developed called nestjs-metrics-client. It's designed to make metrics reporting in NestJS as simple and seamless as possible.

Why Did I Create This?

When using other metrics libraries, I found that the dependency injection setup and boilerplate often got in the way more than they helped. Because of this, I wrote a zero-dependency-injection alternative to make reporting metrics from anywhere in your codebase easier.

I wrote about the motivation and technical details in more depth in my Medium article Avoid Prometheus Mess in NestJS

Key Features

  • No Dependency Injection – Global static ReporterService for clean, portable code.
  • Effortless Integration – Zero-setup, start tracking metrics instantly.
  • Support for Pushgateway – Push batch job metrics effortlessly.
  • Designed for Simplicity – Spend time coding, rather than dealing with complex configurations.

How It Works

With a minimal setup in your AppModule, you'll start reporting metrics like counters, gauges, histograms, and summaries in no time:

1. Install the package:

npm install nestjs-metrics-client
Enter fullscreen mode Exit fullscreen mode

2. Configure the module:

ReporterModule.forRoot({
   defaultMetricsEnabled: true,
   defaultLabels: {
     app: 'my-app',
   },
}),
Enter fullscreen mode Exit fullscreen mode

3. Report metrics anywhere in your application:

ReporterService.gauge('active_users', 42, { region: 'us-east-1' });
Enter fullscreen mode Exit fullscreen mode

I'd be happy to hear your feedback! Feel free to dive in, open issues, or send PRs my way.

👉 GitHub Repo: nestjs-metrics-client

👉 NPM Package: nestjs-metrics-client

If you find this helpful, please consider starring ⭐ the repo on GitHub and using the package in your projects. Your feedback will help make it even better.

Top comments (0)