npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nestjs-statsd-interceptor

v1.0.0

Published

A NestJS interceptor that reports HTTP metrics using statsd

Downloads

4

Readme

NestJS StatsD Interceptor

An interceptor for NestJS that reports HTTP statistics to a statsd agent.

Uses hot-shots and based on node-connect-datadog.

Installation and Usage

npm i nestjs-statsd-interceptor

There are 2 options for using this library:

  1. Automatically register a global interceptor - by importing the module.
  2. Using the interceptor class directly.

Automatically register a global interceptor

Import and configure the module, e.g. in your app module:

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { StatsDInterceptorModule } from './nestjs-statsd-interceptor/nestjs-statsd-interceptor.module';

@Module({
  imports: [StatsDInterceptorModule.configure()],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

This will register a global interceptor.

You can pass a StatsDInterceptorOptions object to the configure() method to configure the global interceptor.

Using the interceptor class directly

Import the interceptor class and use it where you want.

One way of doing this is:

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { APP_INTERCEPTOR } from '@nestjs/core';
import { StatsDInterceptor } from './nestjs-statsd-interceptor/statsd.interceptor';

@Module({
  controllers: [AppController],
  providers: [AppService,
    {
      provide: APP_INTERCEPTOR,
      useValue: new StatsDInterceptor({
        method: true,
        path: true,
        protocol: true,
        responseCode: true,
      }),
    },
],
})
export class AppModule {}

Configuration

Pass an options object to the module's configure() method or to the interceptor's constructor:

export interface StatsDInterceptorOptions {
  /**
   * A StatsD client (from hot-shots package). If null then a new client is created with default options
   */
  statsD?: StatsD;
  /**
   * The stat name under which to send the metrics
   */
  stat?: string;
  /**
   * Array of tags to attach to the metric
   */
  tags?: string[];
  /**
   * If true, create metrics for distinct paths
   */
  path?: boolean;
  /**
   * If true, add the baseUrl to the metric's route
   */
  baseUrl?: boolean;
  /**
   * If true, create metrics for distinct methods
   */
  method?: boolean;
  /**
   * If true, create metrics for distinct protocols
   */
  protocol?: boolean;
  /**
   * If true, create metrics for distinct status codes
   */
  responseCode?: boolean;
  delim?: string;
  /**
   * An adapter that will be used to extract path, route, etc. from the request-response pair.
   */
  adapter?: (request: any, response: any) => RequestResponseAdapter;
}