Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"use strict";
const { Meter } = require("@opentelemetry/metrics");
const { PrometheusExporter } = require("@opentelemetry/exporter-prometheus");
const meter = new Meter();
const exporter = new PrometheusExporter(
{
startServer: true
},
() => {
console.log("prometheus scrape endpoint: http://localhost:9464/metrics");
}
);
meter.addExporter(exporter);
// Monotonic counters and gauges can only be increased.
const monotonicCounter = meter.createCounter("monotonic_counter", {
monotonic: true,
labelKeys: ["pid"],
private _newMetric(
readableMetric: ReadableMetric,
name: string
): Metric | undefined {
const metricObject = {
name,
// prom-client throws with empty description which is our default
help: readableMetric.descriptor.description || 'description missing',
labelNames: readableMetric.descriptor.labelKeys,
// list of registries to register the newly created metric
registers: [this._registry],
};
switch (readableMetric.descriptor.type) {
case MetricDescriptorType.COUNTER_DOUBLE:
case MetricDescriptorType.COUNTER_INT64:
// there is no such thing as a non-monotonic counter in prometheus
return readableMetric.descriptor.monotonic
? new Counter(metricObject)
: new Gauge(metricObject);
case MetricDescriptorType.GAUGE_DOUBLE:
case MetricDescriptorType.GAUGE_INT64:
return new Gauge(metricObject);
default:
// Other metric types are currently unimplemented
return undefined;
}
}
private _newMetric(
readableMetric: ReadableMetric,
name: string
): Metric | undefined {
const metricObject = {
name,
// prom-client throws with empty description which is our default
help: readableMetric.descriptor.description || 'description missing',
labelNames: readableMetric.descriptor.labelKeys,
// list of registries to register the newly created metric
registers: [this._registry],
};
switch (readableMetric.descriptor.type) {
case MetricDescriptorType.COUNTER_DOUBLE:
case MetricDescriptorType.COUNTER_INT64:
// there is no such thing as a non-monotonic counter in prometheus
return readableMetric.descriptor.monotonic
? new Counter(metricObject)
: new Gauge(metricObject);
case MetricDescriptorType.GAUGE_DOUBLE:
case MetricDescriptorType.GAUGE_INT64:
return new Gauge(metricObject);
default:
// Other metric types are currently unimplemented
return undefined;
}
}
name,
// prom-client throws with empty description which is our default
help: readableMetric.descriptor.description || 'description missing',
labelNames: readableMetric.descriptor.labelKeys,
// list of registries to register the newly created metric
registers: [this._registry],
};
switch (readableMetric.descriptor.type) {
case MetricDescriptorType.COUNTER_DOUBLE:
case MetricDescriptorType.COUNTER_INT64:
// there is no such thing as a non-monotonic counter in prometheus
return readableMetric.descriptor.monotonic
? new Counter(metricObject)
: new Gauge(metricObject);
case MetricDescriptorType.GAUGE_DOUBLE:
case MetricDescriptorType.GAUGE_INT64:
return new Gauge(metricObject);
default:
// Other metric types are currently unimplemented
return undefined;
}
}
// prom-client throws with empty description which is our default
help: readableMetric.descriptor.description || 'description missing',
labelNames: readableMetric.descriptor.labelKeys,
// list of registries to register the newly created metric
registers: [this._registry],
};
switch (readableMetric.descriptor.type) {
case MetricDescriptorType.COUNTER_DOUBLE:
case MetricDescriptorType.COUNTER_INT64:
// there is no such thing as a non-monotonic counter in prometheus
return readableMetric.descriptor.monotonic
? new Counter(metricObject)
: new Gauge(metricObject);
case MetricDescriptorType.GAUGE_DOUBLE:
case MetricDescriptorType.GAUGE_INT64:
return new Gauge(metricObject);
default:
// Other metric types are currently unimplemented
return undefined;
}
}