Node.js SDK
Custom metrics from Node.js
You need a metric source token (WT15_METRICS_TOKEN, wt15ms_…): see metric sources and the ingest API it wraps.
Push points
Section titled “Push points”const result = await wt15.ingest.metrics.push([ { metric: 'orders.completed', metric_type: 'count', value: 1 }, { metric: 'queue.depth', metric_type: 'level', value: 42, recorded_at: new Date() },]);result.accepted; // true when any chunk was acceptedresult.count; // points storedresult.rejected; // every rejection reason, 0 where none- A point that names a
metriccarriesmetric_type,countorlevel; one bymetric_uuidmay omit it. Avaluemust be a finite number. - The SDK stamps no time. A point without
recorded_atis stamped by the server; aDatebecomes epoch milliseconds. push()cuts the points into chunks of at most 1,000 points and 1,000,000 bytes, gzips each and sends them in order. A chunk that fails throws withpartialResultholding the chunks before it.- Keys: each chunk is sent under an
Idempotency-Key. A chunk holding a point older than 30 days is keyed by its content, so a retry is the same batch; any other chunk gets a per-call nonce. Pass{ idempotencyKey }(1 to 100 visible ASCII characters) to choose your own: one chunk uses it as given, several use<key>/0,<key>/1, …. Never reuse your own key for other points. wt15.ingest.metrics.list()returns the source’s metrics with their status,maxMetricsandused.
The aggregator
Section titled “The aggregator”import { Aggregator } from '@watchtower15/sdk';
const metrics = new Aggregator({ client: wt15, allowList: { 'orders.created': 'count', 'queue.depth': 'level' }, heartbeat: 'app.alive',});metrics.increment('orders.created'); // microseconds, never throws, never sends from heremetrics.gauge('queue.depth', 812);process.on('SIGTERM', async () => { await metrics.close(); process.exit(0);});increment and gauge only update memory. Buckets of 60 seconds are closed 5 seconds after their end and sent by a flush: one push per closed bucket, oldest first, without retries, within maxRequests
(10) and 90 seconds a run. An allow-list names every key you count with its type: a key not listed is counted not_allowed and nothing is stored; each entry and the heartbeat key is checked at
construction and a bad one is listed in configProblems. enabled: false stores and sends nothing.
- The timer. The first
incrementorgaugestarts a real interval (flushIntervalMs, 10 seconds by default,falsefor none) that sends closed buckets; it isunref’d, so it never keeps the process alive. flush({ final })resolves to aFlushReport(status,bucketsSent,requests,points,rejected,stopReason,errors,configProblems), never rejects.final: truesends the open bucket too.close({ timeoutMs })stops the timer and sends every bucket within its timeout (10 seconds), resolving to the report. AbeforeExithook runs one final flush if you forget.- Failures are counted, never thrown. A 401, a bad configuration, a redirect or a transport failure ends the run with the matching
stopReasonand keeps the bucket for the next run. - One store per aggregator, per process. Custom metrics keep every sample, so two processes’ points both count; there is nothing to share.
Serverless
Section titled “Serverless”A function may freeze before the next minute: send what you counted before the invocation answers, or hand it to the platform. See Serverless.