Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including moleculer-prometheus with all npm packages installed. Try it out:

require("moleculer/package.json"); // moleculer is a peer dependency. var moleculerPrometheus = require("moleculer-prometheus")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

moleculer-prometheus v0.2.1

Moleculer metrics module for Prometheus.

Moleculer logo

moleculer-prometheus NPM version

Moleculer metrics module for Prometheus.

Grafana screenshot

Features

  • collect default Node.js metrics.
  • measure service calls.
  • support custom metrics.
  • Grafana dashboard example.

Install

$ npm install moleculer-prometheus

Usage

// services/metrics.prometheus.service.js

const PromService = require("moleculer-prometheus");

module.exports = {
    mixins: [PromService],
    settings: {
        port: 3030,
        collectDefaultMetrics: true,
        timeout: 5 * 1000, 
    }
};

// moleculer.config.js
module.exports = {
    // ...
    metrics: true,
    // ...
}

Add custom metric

// services/metrics.prometheus.js
const PromService = require("moleculer-prometheus");

module.exports = {
    mixins: [PromService],
    settings: {
        metrics: {
           "custom_value": { type: "Gauge", help: "Moleculer Prometheus custom metric" } 
        } 
    }
});

Broadcast a metrics.update event to set the metric value

broker.broadcast("metrics.update", {
    name: "custom_value",
    method: "set",
    value: Math.round(Math.random() * 100)
});

Settings

PropertyTypeDefaultDescription
portNumber3030Exposed HTTP port.
collectDefaultMetricsBooleantrueEnable to collect default metrics.
timeoutNumber10000Timeout option for 'collectDefaultMetrics' in milliseconds.
metricsObject{}Metric definitions.

Default Moleculer metrics

NameTypeLabelsDescription
moleculer_nodes_totalGauge-Moleculer nodes count
moleculer_services_totalGauge-Moleculer services count
moleculer_actions_totalGauge-Moleculer actions count
moleculer_events_totalGauge-Moleculer event subscription count
moleculer_nodesGaugenodeID, type, version, langVersionMoleculer node list
moleculer_action_endpoints_totalGaugeactionMoleculer action endpoints
moleculer_service_endpoints_totalGaugeservice, versionMoleculer service endpoints
moleculer_event_endpoints_totalGaugeevent, groupMoleculer event endpoints
moleculer_req_totalCounteraction, service, nodeIDMoleculer request count
moleculer_req_errors_totalCounteraction, service, nodeID, errorCode, errorName, errorTypeMoleculer request error count
moleculer_req_duration_msHistogramaction, service, nodeIDMoleculer request durations

Methods

## `update`

Update a metric value.

Parameters

PropertyTypeDefaultDescription
nameStringrequired
methodStringrequired
labelsObject-
valueanyrequired
timestampanyrequired
RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free