Streaming StatsD Protocol Parser
raw JSON →statsd-parser is a Node.js library designed for streaming parsing of the StatsD protocol. It provides both a low-level `parser()` object for direct event-driven parsing and a higher-level `createStream()` API for integrating with Node.js streams, allowing for efficient processing of StatsD metrics from various input sources like network streams or files. The library was last updated in September 2013, with its current and only published version being 0.0.4. Due to its age and lack of maintenance for over a decade, it is not recommended for new projects or existing systems requiring modern Node.js compatibility, security updates, or active support. Key differentiators at the time of its release included its focus on streaming capabilities and event-driven parsing, which was beneficial for handling high-volume metric data efficiently in a Node.js environment.
Common errors
error TypeError: (intermediate value).parser is not a function ↓
const statsd_parser = require('statsd-parser'); const parser = statsd_parser.parser(); error SyntaxError: Unexpected token 'export' ↓
const statsd_parser = require('statsd-parser'); error Error: read ECONNRESET ↓
statsd-parser, implement robust error handling on the stream (stream.on('error')) as shown in the quickstart, and consider using more modern, robust stream processing libraries or network handlers if connection issues are frequent. Warnings
breaking This package is critically unmaintained. The last publish was in 2013 (version 0.0.4) and there has been no development activity for over a decade. It may contain unpatched security vulnerabilities, is unlikely to be compatible with recent Node.js versions, and will not receive bug fixes or new features. ↓
gotcha The package uses a CommonJS module system exclusively. Attempting to use `import` statements will result in runtime errors in environments that enforce ESM-only loading without CommonJS compatibility layers. ↓
gotcha Error handling for the `parser` object requires explicitly clearing the internal error state (`this._parser.error = null; this._parser.resume();`) within the `stream.on('error')` callback to prevent the stream from stopping after the first parsing error. Without this, a single malformed line can halt further processing. ↓
Install
npm install statsd-parser yarn add statsd-parser pnpm add statsd-parser Imports
- statsd_parser wrong
import statsd_parser from 'statsd-parser';correctconst statsd_parser = require('statsd-parser'); - parser wrong
import { parser } from 'statsd-parser';correctconst statsd_parser = require('statsd-parser'); const parser = statsd_parser.parser(); - createStream wrong
import { createStream } from 'statsd-parser';correctconst statsd_parser = require('statsd-parser'); const stream = statsd_parser.createStream(options);
Quickstart
const statsd_parser = require("statsd-parser");
const fs = require('fs');
const { Readable } = require('stream');
// Basic parser usage
const parser = statsd_parser.parser();
parser.onerror = function (e) {
console.error("Parser Error:", e.message);
};
parser.onstat = function (txt, obj) {
console.log(`Parsed stat: ${txt} ->`, obj);
};
parser.onend = function () {
console.log("Basic parser stream finished.\n");
};
console.log("--- Using basic parser ---");
parser.write('foo.bar:1|c\nbaz.qux:2.5|g\n');
parser.write('timer.event:100|ms\n');
parser.close(); // Signal end of input
// Stream usage with a Readable stream
const stream = statsd_parser.createStream();
stream.on("error", function (e) {
console.error("Stream Error:", e.message);
// In a real application, you might need to handle this more robustly,
// like clearing the parser error and resuming if it's an internal parser error.
this._parser.error = null;
this._parser.resume();
});
stream.on("stat", function (txt, obj) {
console.log(`Streamed stat: ${txt} ->`, obj);
});
stream.on("end", () => {
console.log("Stream parser finished.");
});
console.log("--- Using stream parser ---");
const inputData = 'metric.one:10|c\nmetric.two:200|ms\nmetric.three:3.14|g\n';
const readableStream = Readable.from(inputData);
readableStream.pipe(stream);