
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
spm-metrics-js
Advanced tools
SPM Performance Monitoring - Custom Metrics and Event API client for node.js.
This module implements:
A free account to generate API tokens is available at www.sematext.com
npm install spm-metrics-js
var SPM = require('spm-metrics-js')
// YOUR API TOKEN, e.g. from environment variables
var token = process.env.SPM_TOKEN
Create client object, automatically send every 30 seconds, 0 disables interval transmission. When transmission interval is disabled metrics are transmitted when the internal buffer size reaches 100 entries.
var spmClient = new SPM(token, 30000)
Capture relevant metrics for your application and forward it to SPM
var userMetric = spmClient.getCustomMetric ({name: 'user.count', aggregation: 'avg', filter1: os.hostname()})
userMetric.set (users.count)
...
userMetric.set (users.count)
Force sending the metrics immediately
spmClient.send()
If you want to apply statistical functions to metrics and send the results to SPM at a fixed interval to SPM, the metric object should be initialized by declaring an 'interval' for generating the statistics:
var options = {name: 'requests', aggregation: 'avg', filter1: os.hostname(), interval: 60000}
var requestCounterMetric = spmClient.getCustomMetric (options)
Measurement functions like histogram(), meter(), timer() add calculated values as properties to the metrics like count, min, max, mean or percentiles. In SPM a metric called "time" might then appear as "time.min", "time.max", "time.mean" etc.
For example: If you want to send only 'count' and 'currentRate' calculated by a "meter" measurement to SPM you can use valueFilter option to limit what you send to SPM:
var options = {name: 'requests', aggregation: 'avg', filter1: os.hostname(), valueFilter: ['count', 'currentRate']}
See details of used properties below.
A counter can be incremented and decremented
var options = {name: 'requests', aggregation: 'avg', filter1: os.hostname(), interval: 60000}
var userCounterMetric = spmClient.getCustomMetric (options)
userCounterMetric.counter()
// use counter e.g. on login
userCounterMetric.inc()
// or on logout
userCounterMetric.dec()
// save data - only needed if no "interval" is specified
userCounterMetric.save()
A meter can be used to count events and calculate rates
var options = {name: 'requests', aggregation: 'avg', filter1: os.hostname()}
var requestCounterMetric = spmClient.getCustomMetric (options)
// activate meter functions
requestCounterMetric.meter()
// use meter e.g. in in each request
requestCounterMetric.mark()
// save data - only needed if no "interval" is specified
requestCounterMetric.save()
This values will be calculated and attached to your metric:
In our example it would be "requests.mean", "requests.count" etc.
Keeps a resevoir of statistically relevant values biased towards the last 5 minutes to explore their distribution.
var options = {name: 'requestTime', aggregation: 'avg', filter1: os.hostname()}
var requestTimeMetric = spmClient.getCustomMetric (options)
// activate histogram functions
requestTimeMetric.histogram()
// use histogram update function e.g. in in each request with measured time
requestTimeMetric.update(measuredTime)
// save data and reset histogram - only needed if no "interval" is specified
var metricValues = requestTimeMetric.save()
// output values
console.log (metricValues)
This values will be calculated and attached to your metric:
In our example it would be "requestTime.min", "requestTime.max", "requestTime.p75" etc.
Timer measures the time between start() and end() call and provides as result properties of Histogram and Meter.
var timerMetric = spmClient.getCustomMetric (options)
var stopwatch = timerMetric.timer().start()
stopwatch.end()
timerMetric.save()
The measurement functions are based on the excellent Measured package. We use a concept of delegate functions to simlify the combined usage of Custom Metrics and Measured. For example a call to metric.histogram() adds dynamically all Measured.Histogram functions to the metrics object created by getCustomMetrics(). The save() function uses toJSON() of the "Measured" objects to generate the calculated properties, which are added to each metric object.
We recommend reading the documentation of Measured for a better understanding of the measurement functions.
Add an event to SPM Events, the callback function is optional (see events of spm-metrics-js)
var spmClient = new SPM(token, 0)
// configure defaults
var eventLogger = spmClient.getEventLogger ({
type: 'system',
name: 'system',
tags: ['job', 'queue'],
creator: 'jobsQ.js',
data: 'String or Base64 coded content'})
// send Event Message, the callback is optional
eventLogger.log ('forked new worker', function (err, res) {
console.log(err || res)
})
process.on ('exit', function () {
eventLogger.log ('Exit job queue PID:' + process.pid)
})
You can add event handlers for errors and actions
spmClient.on ('error', console.log)
spmClient.on ('add', console.log)
spmClient.on ('send metrics', console.log)
spmClient.on ('send event', console.log)
FAQs
Node.js client for SPM - Custom Metrics and Event API
The npm package spm-metrics-js receives a total of 8 weekly downloads. As such, spm-metrics-js popularity was classified as not popular.
We found that spm-metrics-js demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.