Skip to content

Instantly share code, notes, and snippets.

@luebken
Created May 14, 2020 16:39
Show Gist options
  • Save luebken/a1667d915f23cbf97e9c998ab92783b8 to your computer and use it in GitHub Desktop.
Save luebken/a1667d915f23cbf97e9c998ab92783b8 to your computer and use it in GitHub Desktop.
const { PrometheusExporter } = require('@opentelemetry/exporter-prometheus');
const { MeterProvider } = require('@opentelemetry/metrics');
const { Resource } = require('@opentelemetry/resources');
// Add your port and startServer to the Prometheus options
const options = {port: 9464, startServer: true};
const exporter = new PrometheusExporter(options);
const resource = new Resource({
'k8s.io/container/name': 'c1',
'k8s.io/namespace/name': 'default',
'k8s.io/pod/name': 'pod-xyz-123',
});
// Register the exporter
const meter = new MeterProvider({
exporter,
interval: 1000,
resource: resource
}).getMeter('example-prometheus');
// Now, start recording data
const counter = meter.createCounter('metric_name', {
labelKeys: ['pid', 'k8s.io/container/name'], // <= throws new Error('Invalid label name');
description: 'Example of a counter'
});
counter.add(10, { pid: process.pid });
// Record data using Instrument: It is recommended to keep a reference to the Bound Instrument instead of
// always calling `bind()` method for every operations.
const boundCounter = counter.bind({ pid: process.pid });
boundCounter.add(10);
console.log("started server at :9464")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment