0.0.4 • Published 2 years ago

@konso/metrics v0.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

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'
})
ParameterTypeRequired
apiKeystringtrue
apiURLstringtrue
bucketIdstringtrue
appNamestringfalse
tagsstring[]false

Methods


startMeasure

startMeasure(name: string) : (extraOptions: MetricsExtraOptions) => void;

Parameters

ParameterTypeRequiredValue
namestringtrue

Parameters for the returned function

ParameterTypeRequiredValue
extraOptionsObjecttrue{tags?: string[],correlationId?: string,responseCode?: number}

on

on(eventName: LogEvents, callback: (text: string) => void);

Parameters

ParameterTypeRequiredValue
eventNamestringtrue'onSuccessful', 'onError' or keys of LogEvent object
callbackfunctiontrue(text: string) => void