New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@mojaloop/central-services-metrics

Package Overview
Dependencies
Maintainers
4
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@mojaloop/central-services-metrics - npm Package Compare versions

Comparing version 4.4.0 to 5.1.0

lib/index.js.map

1

lib/index.js

@@ -28,1 +28,2 @@ /*****

module.exports = new metrics.Metrics()
// # sourceMappingURL=index.js.map

11

lib/metrics.d.ts

@@ -30,3 +30,3 @@ /*****

/**
* Type that represents the options that are required to setup the prom-client
* Type that represents the options that are required for setup
*/

@@ -36,2 +36,3 @@ declare type metricOptionsType = {

prefix: string;
defaultLabels?: Map<string, string>;
};

@@ -41,7 +42,7 @@ /** Wrapper class for prom-client. */

/** To make sure the setup is run only once */
private alreadySetup;
private _alreadySetup;
/** Object containg the histogram values */
private histograms;
private _histograms;
/** The options passed to the setup */
private options;
private _options;
/**

@@ -64,2 +65,2 @@ * Setup the prom client for collecting metrics using the options passed

}
export { Metrics };
export { Metrics, metricOptionsType };

@@ -35,7 +35,7 @@ /*****

/** To make sure the setup is run only once */
this.alreadySetup = false
this._alreadySetup = false
/** Object containg the histogram values */
this.histograms = {}
this._histograms = {}
/** The options passed to the setup */
this.options = { prefix: '', timeout: 0 }
this._options = { prefix: '', timeout: 0 }
/**

@@ -45,9 +45,17 @@ * Setup the prom client for collecting metrics using the options passed

this.setup = (options) => {
if (this.alreadySetup) {
if (this._alreadySetup) {
return false
}
this.options = options
client.collectDefaultMetrics(this.options)
this._options = options
// map the options to the normalised options specific to the prom-client
let normalisedOptions = {
prefix: this._options.prefix,
timeout: this._options.timeout
}
if (this._options.defaultLabels !== undefined) {
client.register.setDefaultLabels(this._options.defaultLabels)
}
client.collectDefaultMetrics(normalisedOptions)
client.register.metrics()
this.alreadySetup = true
this._alreadySetup = true
return true

@@ -60,6 +68,6 @@ }

try {
if (this.histograms[name]) {
return this.histograms[name]
if (this._histograms[name]) {
return this._histograms[name]
}
this.histograms[name] = new client.Histogram({
this._histograms[name] = new client.Histogram({
name: `${this.getOptions().prefix}${name}`,

@@ -70,3 +78,3 @@ help: help || `${name}_histogram`,

})
return this.histograms[name]
return this._histograms[name]
} catch (e) {

@@ -86,3 +94,3 @@ throw new Error(`Couldn't get metrics histogram for ${name}`)

this.getOptions = () => {
return this.options
return this._options
}

@@ -92,1 +100,2 @@ }

exports.Metrics = Metrics
// # sourceMappingURL=metrics.js.map
{
"name": "@mojaloop/central-services-metrics",
"version": "4.4.0",
"version": "5.1.0",
"description": "Shared code for metrics generation",

@@ -9,9 +9,10 @@ "main": "lib/index.js",

"test": "npm run test:unit | faucet",
"pretest": "standard",
"pretest": "npm run build",
"standard": "standard",
"build": "tsc",
"build": "tsc; standard --fix",
"watch": "tsc -w",
"test:unit": "ts-node node_modules/tape/bin/tape 'test/unit/**/*.test.ts'",
"test:xunit": "npm run test:unit | tap-xunit",
"test:coverage": "nyc --reporter=lcov --require ts-node/register tape test/unit/**/*.test.{ts,js} | tap-spec; nyc report ---reporter=text",
"git:stagelib": "git add lib/*",
"git:stagelib": "git add lib/*ts; git add lib/*.map; git add lib/*.js",
"git:standardFix": "standard --fix"

@@ -22,3 +23,3 @@ },

"git:standardFix",
"test",
"test:unit",
"git:stagelib"

@@ -48,2 +49,3 @@ ],

"@types/tape": "4.2.32",
"debug": "4.1.1",
"faucet": "0.0.1",

@@ -58,8 +60,10 @@ "nyc": "13.1.0",

"ts-node": "7.0.1",
"tslint": "5.12.1",
"typedoc": "0.13.0",
"typescript": "3.1.6"
"typescript": "3.3.1"
},
"nyc": {
"include": [
"src/**/*.ts"
"src/**/*.ts",
"test/**/*.test.ts"
],

@@ -66,0 +70,0 @@ "extension": [

# central-services-metrics
central-services-metrics
[![npm version](https://badge.fury.io/js/%40mojaloop%2Fcentral-services-metrics.svg)](https://badge.fury.io/js/%40mojaloop%2Fcentral-services-metrics) [![CircleCI](https://circleci.com/gh/mojaloop/central-services-metrics.svg?style=svg)](https://circleci.com/gh/mojaloop/central-services-metrics)
## Installation
```bash
npm install @mojaloop/central-services-metrics
```
## Usage
Import Metrics library:
```javascript
const Metrics = require('@mojaloop/central-services-metrics')
```
Set configuration options:
```javascript
let config = {
"timeout": 5000, // Set the timeout in ms for the underlying prom-client library. Default is '5000'.
"prefix": "<PREFIX>", // Set prefix for all defined metrics names
"defaultLabels": { // Set default labels that will be applied to all metrics
"serviceName": "<NAME_OF_SERVICE>"
}
}
```
Initialise Metrics library:
```JAVASCRIPT
Metrics.setup(config)
```
Example instrumentation:
```javascript
const exampleFunction = async (error, message) => {
const histTimerEnd = Metrics.getHistogram( // Create a new Histogram instrumentation
'exampleFunctionMetric', // Name of metric. Note that this name will be concatenated after the prefix set in the config. i.e. '<PREFIX>_exampleFunctionMetric'
'Instrumentation for exampleFunction', // Description of metric
['success'] // Define a custom label 'success'
).startTimer() // Start instrumentation
try {
Logger.info('do something meaningful here')
histTimerEnd({success: true}) // End the instrumentation & set custom label 'success=true'
} catch (e) {
histTimerEnd({success: false}) // End the instrumentation & set custom label 'success=false'
}
}
```

@@ -34,9 +34,19 @@ /*****

/**
* Type that represents the options that are required to setup the prom-client
* Type that represents the options that are required for setup
*/
type metricOptionsType = {
timeout: number,
prefix: string
timeout: number,
prefix: string,
defaultLabels?: Map<string, string>
}
/**
* Type that represents the options that are required to setup the prom-client specifically
*/
type normalisedMetricOptionsType = {
timeout: number,
prefix: string
}
/**
* Object that holds the histogram values

@@ -48,60 +58,71 @@ */

class Metrics {
/** To make sure the setup is run only once */
private alreadySetup: boolean = false
/** To make sure the setup is run only once */
private _alreadySetup: boolean = false
/** Object containg the histogram values */
private histograms: histogramsType = {}
/** Object containg the histogram values */
private _histograms: histogramsType = {}
/** The options passed to the setup */
private options: metricOptionsType = { prefix: '', timeout: 0 }
/** The options passed to the setup */
private _options: metricOptionsType = { prefix: '', timeout: 0 }
/**
* Setup the prom client for collecting metrics using the options passed
*/
setup = (options: metricOptionsType): boolean => {
if (this.alreadySetup) {
return false
/**
* Setup the prom client for collecting metrics using the options passed
*/
setup = (options: metricOptionsType): boolean => {
if (this._alreadySetup) {
return false
}
this._options = options
// map the options to the normalised options specific to the prom-client
let normalisedOptions: normalisedMetricOptionsType = {
prefix: this._options.prefix,
timeout: this._options.timeout
}
if(this._options.defaultLabels !== undefined){
client.register.setDefaultLabels(this._options.defaultLabels)
}
client.collectDefaultMetrics(normalisedOptions)
client.register.metrics()
this._alreadySetup = true
return true
}
this.options = options
client.collectDefaultMetrics(this.options)
client.register.metrics()
this.alreadySetup = true
return true
}
/**
* Get the histogram values for given name
*/
getHistogram = (name: string, help?: string, labelNames?: string[], buckets: number[] = [0.010, 0.050, 0.1, 0.5, 1, 2, 5]): client.Histogram => {
try {
if (this.histograms[name]) {
return this.histograms[name]
}
this.histograms[name] = new client.Histogram({
name: `${this.getOptions().prefix}${name}`,
help: help || `${name}_histogram`,
labelNames,
buckets // this is in seconds - the startTimer().end() collects in seconds with ms precision
})
return this.histograms[name]
} catch (e) {
throw new Error(`Couldn't get metrics histogram for ${name}`)
/**
* Get the histogram values for given name
*/
getHistogram = (name: string, help?: string, labelNames?: string[], buckets: number[] = [0.010, 0.050, 0.1, 0.5, 1, 2, 5]): client.Histogram => {
try {
if (this._histograms[name]) {
return this._histograms[name]
}
this._histograms[name] = new client.Histogram({
name: `${this.getOptions().prefix}${name}`,
help: help || `${name}_histogram`,
labelNames,
buckets // this is in seconds - the startTimer().end() collects in seconds with ms precision
})
return this._histograms[name]
} catch (e) {
throw new Error(`Couldn't get metrics histogram for ${name}`)
}
}
}
/**
* Get the metrics
*/
getMetricsForPrometheus = (): string => {
return client.register.metrics()
}
/**
* Get the metrics
*/
getMetricsForPrometheus = (): string => {
return client.register.metrics()
}
/**
* Get the options that are used to setup the prom-client
*/
getOptions = (): metricOptionsType => {
return this.options
}
/**
* Get the options that are used to setup the prom-client
*/
getOptions = (): metricOptionsType => {
return this._options
}
}
export { Metrics }
export {
Metrics,
metricOptionsType
}

@@ -29,9 +29,5 @@ /*****

const Test = require('tapes')(require('tape'))
import { Metrics } from "../../src/metrics"
import { Metrics, metricOptionsType } from "../../src/metrics"
Test('Metrics Class Test', (metricsTest: any) => {
type metricOptionsType = {
timeout: number,
prefix: string
}

@@ -51,3 +47,3 @@ metricsTest.test('setup should', (setupTest: any) => {

} catch (e) {
test.fail('Error Thrown')
test.fail(`Error Thrown - ${e}`)
test.end()

@@ -57,2 +53,30 @@ }

setupTest.test('initialize the metrics object with default labels', async (test: any) => {
console.log('******STARTING********')
try {
const metrics: Metrics = new Metrics()
let defaultLabels = new Map()
defaultLabels.set('serviceName', 'testService')
// const defaultLabels = {
// serviceName: 'testService'
// }
const options: metricOptionsType = {
prefix: 'prefix2_',
timeout: 1000,
defaultLabels
}
console.log(`options=${JSON.stringify(options)}`)
console.log('******SET-START********')
let result = metrics.setup(options)
console.log('******SET-END********')
test.equal(result, true, 'Result match')
console.log(`options: ${JSON.stringify(metrics.getOptions())}`)
test.deepEqual(metrics.getOptions(), options, 'Options match')
test.end()
} catch (e) {
test.fail(`Error Thrown - ${e}`)
test.end()
}
})
setupTest.test('return false if setup is already initialized', async (test: any) => {

@@ -70,3 +94,3 @@ try {

} catch (e) {
test.fail('Error Thrown')
test.fail(`Error Thrown - ${e}`)
test.end()

@@ -104,3 +128,3 @@ }

console.log(e)
test.fail('Error Thrown')
test.fail(`Error Thrown - ${e}`)
test.end()

@@ -127,3 +151,3 @@ }

console.log(e)
test.fail('Error Thrown')
test.fail(`Error Thrown - ${e}`)
test.end()

@@ -164,3 +188,3 @@ }

} catch (e) {
test.fail('Error Thrown')
test.fail(`Error Thrown - ${e}`)
test.end()

@@ -197,3 +221,3 @@ }

} catch (e) {
test.fail('Error Thrown')
test.fail(`Error Thrown - ${e}`)
test.end()

@@ -233,3 +257,3 @@ }

} catch (e) {
test.fail('Error Thrown')
test.fail(`Error Thrown - ${e}`)
test.end()

@@ -236,0 +260,0 @@ }

{
"compilerOptions": {
/* Basic Options */
"target": "es2015", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
"target": "ES2015", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */

@@ -12,3 +12,3 @@ // "lib": [], /* Specify library files to be included in the compilation. */

// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
// "sourceMap": true, /* Generates corresponding '.map' file. */
"sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */

@@ -50,3 +50,3 @@ "outDir": "./lib", /* Redirect output structure to the directory. */

// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
"inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
/* Experimental Options */

@@ -57,8 +57,11 @@ // "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */

"include": [
"src/**/*"
"src/**/*.ts"
],
"exclude": [
"node_modules",
"**/*.spec.ts"
"**/*.spec.ts",
"test",
"lib",
"coverage"
]
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc