/openapm-nodejs

APM for NodeJS using Prometheus

Primary LanguageTypeScriptApache License 2.0Apache-2.0

@last9/openapm

Installation

npm install --save @last9/openapm@latest

Usage

  1. Express
  2. MySQL
  3. NestJS

Express

In the example below, the metrics will be served on localhost:9097/metrics. To change the port, you can update it through the options (See the options documentation).

const express = require('express)
const { OpenAPM } = require('@last9/openapm')

const app = express();
const openapm = new OpenAPM();

app.use(openapm.REDMiddleware);

// ...

app.listen(3000)

MySQL

This currently supports instrumentation for all Node.js ORMs, which are mysql2 compatible.

Ensure to add this line of code before you initialize db connection/pool/poolCluster.

openapm.instrument('mysql');

NestJS

OpenAPM currently supports RED Metrics for NestJS v4 and above.

openapm.instrument('nestjs');

Options

Usage

const openapm = new OpenAPM({
  // Options go here
});
  1. path: The path at which the metrics will be served. For eg. /metrics

  2. metricsServerPort: (Optional) The port at which the metricsServer will run.

  3. environment: (Optional) The application environment. Defaults to production.

  4. defaultLabels: (Optional) Any default labels to be included.

  5. requestsCounterConfig: (Optional) Requests counter configuration, same as Counter in prom-client. Defaults to

    {
       name: 'http_requests_total',
       help: 'Total number of requests',
       labelNames: ['path', 'method', 'status'],
     }
  6. requestDurationHistogramConfig: (Optional) Requests Duration histogram configuration, the same as Histogram in prom-client. Defaults to

     {
         name: 'http_requests_duration_milliseconds',
         help: 'Duration of HTTP requests in milliseconds',
         labelNames: ['path', 'method', 'status'],
         buckets: promClient.exponentialBuckets(0.25, 1.5, 31),
       }
  7. extractLabels: (Optional) Extract labels from URL params (WIP: Headers, Subdomain)

    // To extract from the URL params
    {
       ...
       extractLabels: {
          tenant: { // Here 'tenant' is the label name
             from : 'params',
             key: 'org' // Which key to extract from the params
             mask: ':org' // Replacement string
          }
       }
    }
  8. excludeDefaultLabels: (Optional) Provide labels to exclude from the default labels

{
  ...
  excludeDefaultLabels: ['environment', 'version']
}
  1. levitateConfig: (Optional) Configuration for Levitate TSDB. Adding this configuration will enable the Change Events.
{
   ...
   levitateConfig: {
      host: 'https://app.last9.io',
      orgSlug: 'last9', /** The slug can be obtained from the Last9 dashboard.*/
      dataSourceName: 'data-source', /** The data source can be obtained from the data source pages in the Last9 dashboard*/
      refreshTokens: {
         write: '0d2a1a9a45XXXXXXXXXXXXXX3f1342790d2a1a9a45XXXXXXXXXXXXXX3f1342790d2a1a9a45XXXXXXXXXXXXXX3f134279' /** You can get this from the API access page on Last9 dashboard*/
      }
   }
}

Setup locally

Make sure you are in the express directory.

  • Install packages
npm install
  • Build package

    • This will build the package and store the JS and type declaration files in the dist folder.
npm run build

Grafana Dashboard View

  1. Import this dashboard into your Grafana
  2. Select your data source
  3. Save the dashboard

APM Dashboard APM Dashboard APM Dashboard

About Last9

Last9 builds reliability tools for SRE and DevOps.