opentelemetry-js icon indicating copy to clipboard operation
opentelemetry-js copied to clipboard

[OpenTelemetry Prometheus Metric Exporter] Docs not working

Open QuentinN42 opened this issue 3 years ago • 1 comments
trafficstars

What happened?

Steps to Reproduce

Just follow the opentelemetry-exporter-prometheus README.

Expected Result

Some metrics.

Actual Result

Got an failed to export metrics: Error: MetricReader is not bound to a MetricProducer

OpenTelemetry Setup Code

import { PrometheusExporter } from '@opentelemetry/exporter-prometheus';
import { MeterProvider } from '@opentelemetry/sdk-metrics-base';
import fetch from 'node-fetch';

// Add your port and startServer to the Prometheus options
const options = {port: 9464, startServer: true};
const exporter = new PrometheusExporter(options);

// Register the exporter
const meter = new MeterProvider({
  exporter,
  interval: 1000,
}).getMeter('example-prometheus');

// Now, start recording data
const counter = meter.createCounter('metric_name', {
  description: 'Example of a counter'
});
counter.add(10, { pid: process.pid });

console.log("http://localhost:9464/metrics")

setTimeout(() => {
  fetch("http://localhost:9464/metrics").then(x => x.text()).then(console.log)
},
1000)

package.json

{
  "name": "js_prom_9roakq",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@opentelemetry/exporter-prometheus": "^0.30.0",
    "@opentelemetry/sdk-metrics-base": "^0.30.0",
    "node-fetch": "^3.2.9"
  }
}

Relevant log output

http://localhost:9464/metrics
# failed to export metrics: Error: MetricReader is not bound to a MetricProducer

QuentinN42 avatar Jul 22 '22 08:07 QuentinN42

Sorry about this. The document is outdated. You can check out https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/examples/prometheus/index.js for an example project for now.

legendecas avatar Jul 25 '22 09:07 legendecas