npm.io
0.0.4 • Published 4 years ago

@konso/metrics

Licence
MIT
Version
0.0.4
Deps
3
Size
11 kB
Vulns
0
Weekly
0

Metrics

Installation


npm i @konso/metrics

Example


import { createMetricsClient, LogEvents } from "@konso/metrics";

const client = createMetricsClient({
  apiURL: 'https://devapis.konso.io',
  bucketId: '1fc41560',
  apiKey: 'REa9xeOvk6fRxAcNEyebk+5nC5LnbeCr+bJTBpoLm5E=',
  appName: 'test'
})

client.on(LogEvents.onSuccessful, (text) => { console.log(text) })
  .on(LogEvents.onError, (text) => console.error(text));


const app = express();

function metricExpressMiddleware(client) {
  if (!client) {
    throw new Error('you should provide client for metrics')
  }

  return function (req, res, next) {
    let endMeasure = client.startMeasure(`${req.method} ${req.path}`);

    res.on("finish", () => {
      endMeasure({ responseCode: res.statusCode, tags: ['client'] });
    });

    next();
  }
}

app.use(metricExpressMiddleware(client));

createMetricsClient

createMetricsClient - the function takes a parameters object and returns an instance of the Metrics class

const client = createMetricsClient({
  apiKey: 'REa9xeOvk6fRxAcNEyebk+5nC5LnbeCr+bJTBpoLm5E=',
  apiURL: 'https://devapis.konso.io',
  bucketId: '1fc41560',
  appName: 'test'
})
Parameter Type Required
apiKey string true
apiURL string true
bucketId string true
appName string false
tags string[] false

Methods


startMeasure

startMeasure(name: string) : (extraOptions: MetricsExtraOptions) => void;
Parameters
Parameter Type Required Value
name string true
Parameters for the returned function
Parameter Type Required Value
extraOptions Object true {
tags?: string[],
correlationId?: string,
responseCode?: number
}

on

on(eventName: LogEvents, callback: (text: string) => void);
Parameters
Parameter Type Required Value
eventName string true 'onSuccessful', 'onError' or keys of LogEvent object
callback function true (text: string) => void