Publish single or summary metrics to AWS CloudWatch.
Atelemetry
plugin.
Click to expand
const telemetry = require('@telemetry-js/telemetry')()
const cloudwatch = require('@telemetry-js/publisher-cloudwatch')
telemetry.task()
.publish(cloudwatch)
// Or with options
telemetry.task()
.publish(cloudwatch, { /* options */ })
If an HTTP request to CloudWatch fails, it is retried. If it fails 5 times, an error
event will be emitted and in this case forwarded to telemetry
:
telemetry.on('error', (err) => {
console.error(err)
})
Useful to publish one-time metrics.
const cloudwatch = require('@telemetry-js/publisher-cloudwatch')
const single = require('@telemetry-js/metric').single
const publisher = cloudwatch()
const metric = single('myapp.example.count', { unit: 'count', value: 10 })
publisher.publish(metric)
await publisher.flush()
The flush
method will yield an error if the HTTP request failed (after retries).
To publish multiple metrics (in one HTTP request), repeat the .publish()
call before .flush()
:
const metric1 = single('myapp.example.count', { unit: 'count', value: 10 })
const metric2 = single('myapp.foobar.bytes', { unit: 'bytes', value: 10 })
publisher.publish(metric1)
publisher.publish(metric2)
await publisher.flush()
namespace
: string, defaults totelemetry
retry
: boolean, defaults to trueretryDelay
: number, milliseconds, defaults to 1000timeout
: socket timeout, number, milliseconds, defaults to 60 secondscredentials
: AWS credentials in the form of{ accessKeyId, secretAccessKey }
. You normally don't need to set this, as credentials are fetched withawscred
which supports EC2, ECS, Lambda, ..region
: AWS CloudWatch region. You normally don't need to set this, as it is fetched withawscred
.
With npm do:
npm install @telemetry-js/publisher-cloudwatch
This project is kindly sponsored by Reason Cybersecurity Ltd.
MIT © Vincent Weevers