npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

metrics-object-parser

v1.0.1

Published

Parses Prometheus text format into JavaScript objects

Downloads

10

Readme

metrics-object-parser

A module that parses the Prometheus text format. The output aims to be compatible with that of prom2json. The code for parsing individual samples was ported from the Prometheus Python Client.

Installation

npm install --save metrics-object-parser

Usage

import fs from 'fs';
import parsePrometheusTextFormat from 'metrics-object-parser';

const metricsStr = fs.readFileSync('metrics.txt', 'utf8');
const parsed = parsePrometheusTextFormat(metricsStr);

Example

Input:

# HELP jvm_classes_loaded The number of classes that are currently loaded in the JVM
# TYPE jvm_classes_loaded gauge
jvm_classes_loaded 3851.0

# HELP http_requests_total The total number of HTTP requests.
# TYPE http_requests_total counter
http_requests_total{method="post",code="200"} 1027 1395066363000
http_requests_total{method="post",code="400"}    3 1395066363000

# Escaping in label values:
msdos_file_access_time_seconds{path="C:\\DIR\\FILE.TXT",error="Cannot find file:\n\"FILE.TXT\""} 1.458255915e9

# Minimalistic line:
metric_without_timestamp_and_labels 12.47

# A weird metric from before the epoch:
something_weird{problem="division by zero"} +Inf -3982045

# A histogram, which has a pretty complex representation in the text format:
# HELP http_request_duration_seconds A histogram of the request duration.
# TYPE http_request_duration_seconds histogram
http_request_duration_seconds_bucket{le="0.05"} 24054
http_request_duration_seconds_bucket{le="0.1"} 33444
http_request_duration_seconds_bucket{le="0.2"} 100392
http_request_duration_seconds_bucket{le="0.5"} 129389
http_request_duration_seconds_bucket{le="1"} 133988
http_request_duration_seconds_bucket{le="+Inf"} 144320
http_request_duration_seconds_sum 53423
http_request_duration_seconds_count 144320

# Finally a summary, which has a complex representation, too:
# HELP rpc_duration_seconds A summary of the RPC duration in seconds.
# TYPE rpc_duration_seconds summary
rpc_duration_seconds{quantile="0.01"} 3102
rpc_duration_seconds{quantile="0.05"} 3272
rpc_duration_seconds{quantile="0.5"} 4773
rpc_duration_seconds{quantile="0.9"} 9001
rpc_duration_seconds{quantile="0.99"} 76656
rpc_duration_seconds_sum 1.7560473e+07
rpc_duration_seconds_count 2693

# HELP jvm_gc_collection_seconds Time spent in a given JVM garbage collector in seconds.
# TYPE jvm_gc_collection_seconds summary
jvm_gc_collection_seconds_count{gc="Copy",} 104.0
jvm_gc_collection_seconds_sum{gc="Copy",} 0.491
jvm_gc_collection_seconds_count{gc="MarkSweepCompact",} 12.0
jvm_gc_collection_seconds_sum{gc="MarkSweepCompact",} 0.486

Stringified result:

{
    "jvm_classes_loaded": {
        "help": "The number of classes that are currently loaded in the JVM",
            "type": "GAUGE",
            "metrics": [
            {
                "value": 3851
            }
        ]
    },
    "http_requests_total": {
        "help": "The total number of HTTP requests.",
            "type": "COUNTER",
            "metrics": [
            {
                "value": 1027,
                "labels": {
                    "method": "post",
                    "code": "200"
                },
                "timestamp_ms": 1395066363000
            },
            {
                "value": 3,
                "labels": {
                    "method": "post",
                    "code": "400"
                },
                "timestamp_ms": 1395066363000
            }
        ]
    },
    "msdos_file_access_time_seconds": {
        "help": "",
            "type": "UNTYPED",
            "metrics": [
            {
                "value": 1458255915,
                "labels": {
                    "path": "C:\\DIR\\FILE.TXT",
                    "error": "Cannot find file:\n\"FILE.TXT\""
                }
            }
        ]
    },
    "metric_without_timestamp_and_labels": {
        "help": "",
            "type": "UNTYPED",
            "metrics": [
            {
                "value": 12.47
            }
        ]
    },
    "something_weird": {
        "help": "",
            "type": "UNTYPED",
            "metrics": [
            {
                "value": null,
                "labels": {
                    "problem": "division by zero"
                },
                "timestamp_ms": -3982045
            }
        ]
    },
    "http_request_duration_seconds": {
        "help": "A histogram of the request duration.",
            "type": "HISTOGRAM",
            "metrics": [
            {
                "buckets": {
                    "1": 133988,
                    "0.05": 24054,
                    "0.1": 33444,
                    "0.2": 100392,
                    "0.5": 129389,
                    "+Inf": 144320
                },
                "count": 144320,
                "sum": 53423
            }
        ]
    },
    "rpc_duration_seconds": {
        "help": "A summary of the RPC duration in seconds.",
            "type": "SUMMARY",
            "metrics": [
            {
                "quantiles": {
                    "0.01": 3102,
                    "0.05": 3272,
                    "0.5": 4773,
                    "0.9": 9001,
                    "0.99": 76656
                },
                "count": 2693,
                "sum": 17560473
            }
        ]
    },
    "myprogram_runtime_gc_pause_ns": {
        "help": "myprogram_runtime_gc_pause_ns",
            "type": "SUMMARY",
            "metrics": [
            {
                "count": 3,
                "sum": 91900
            }
        ]
    },
    "jvm_gc_collection_seconds": {
        "help": "Time spent in a given JVM garbage collector in seconds.",
            "type": "SUMMARY",
            "metrics": [
            {
                "labels": {
                    "gc": "Copy"
                },
                "count": 104,
                "sum": 0.491
            },
            {
                "labels": {
                    "gc": "MarkSweepCompact"
                },
                "count": 12,
                "sum": 0.486
            }
        ]
    }
}