Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not clear other labelsets when updating metrics #941

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions packages/opentelemetry-exporter-prometheus/src/prometheus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,29 +123,26 @@ export class PrometheusExporter implements MetricExporter {
const metric = this._registerMetric(record);
if (!metric) return;

const labelKeys = record.descriptor.labelKeys;
const labelValues = this._getLabelValues(
record.descriptor.labelKeys,
record.labels
);
const point = record.aggregator.toPoint();

if (metric instanceof Counter) {
// Prometheus counter saves internal state and increments by given value.
// MetricRecord value is the current state, not the delta to be incremented by.
// Currently, _registerMetric creates a new counter every time the value changes,
// so the increment here behaves as a set value (increment from 0)
metric.inc(
this._getLabelValues(labelKeys, record.labels),
point.value as Sum
);
metric.inc(labelValues, point.value as Sum);
}

if (metric instanceof Gauge) {
if (record.aggregator instanceof CounterSumAggregator) {
metric.set(
this._getLabelValues(labelKeys, record.labels),
point.value as Sum
);
metric.set(labelValues, point.value as Sum);
} else if (record.aggregator instanceof ObserverAggregator) {
metric.set(
this._getLabelValues(labelKeys, record.labels),
labelValues,
point.value as LastValue,
hrTimeToMilliseconds(point.timestamp)
);
Expand Down Expand Up @@ -179,8 +176,12 @@ export class PrometheusExporter implements MetricExporter {
* https://prometheus.io/docs/instrumenting/exposition_formats/
*/
if (metric instanceof Counter) {
this._registry.removeSingleMetric(metricName);
} else if (metric) return metric;
metric.remove(
dyladan marked this conversation as resolved.
Show resolved Hide resolved
...record.descriptor.labelKeys.map(k => record.labels[k].toString())
);
}

if (metric) return metric;

return this._newMetric(record, metricName);
}
Expand Down
8 changes: 4 additions & 4 deletions packages/opentelemetry-metrics/src/export/Batcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ export class UngroupedBatcher extends Batcher {
}

process(record: MetricRecord): void {
this._batchMap.set(
record.descriptor.name + record.labels.identifier,
record
);
const labels = record.descriptor.labelKeys
.map(k => `${k}=${record.labels[k]}`)
.join(',');
this._batchMap.set(record.descriptor.name + labels, record);
}
}