From d1c99ae1307fbd578faf265c90434aba2a0328b5 Mon Sep 17 00:00:00 2001 From: Bartlomiej Obecny Date: Wed, 8 Apr 2020 18:22:56 +0200 Subject: [PATCH] chore: adding timestamp for prometheus metric Counter (#947) Co-authored-by: Daniel Dyla --- .../src/prometheus.ts | 6 ++- .../test/prometheus.test.ts | 37 ++++++++++++++----- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/packages/opentelemetry-exporter-prometheus/src/prometheus.ts b/packages/opentelemetry-exporter-prometheus/src/prometheus.ts index 2623138e1f..9d266ecdc1 100644 --- a/packages/opentelemetry-exporter-prometheus/src/prometheus.ts +++ b/packages/opentelemetry-exporter-prometheus/src/prometheus.ts @@ -134,7 +134,11 @@ export class PrometheusExporter implements MetricExporter { // 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(labelValues, point.value as Sum); + metric.inc( + labelValues, + point.value as Sum, + hrTimeToMilliseconds(point.timestamp) + ); } if (metric instanceof Gauge) { diff --git a/packages/opentelemetry-exporter-prometheus/test/prometheus.test.ts b/packages/opentelemetry-exporter-prometheus/test/prometheus.test.ts index 1dba630675..2ed9852a54 100644 --- a/packages/opentelemetry-exporter-prometheus/test/prometheus.test.ts +++ b/packages/opentelemetry-exporter-prometheus/test/prometheus.test.ts @@ -14,18 +14,35 @@ * limitations under the License. */ -import { ObserverResult } from '@opentelemetry/api'; +import { HrTime, ObserverResult } from '@opentelemetry/api'; import { CounterMetric, + CounterSumAggregator, Meter, MeterProvider, ObserverMetric, + Point, } from '@opentelemetry/metrics'; import * as assert from 'assert'; import * as http from 'http'; import { PrometheusExporter } from '../src'; +const mockedHrTime: HrTime = [1586347902211, 0]; +const mockedTimeMS = 1586347902211000; + describe('PrometheusExporter', () => { + let toPoint: () => Point; + before(() => { + toPoint = CounterSumAggregator.prototype.toPoint; + CounterSumAggregator.prototype.toPoint = function(): Point { + const point = toPoint.apply(this); + point.timestamp = mockedHrTime; + return point; + }; + }); + after(() => { + CounterSumAggregator.prototype.toPoint = toPoint; + }); describe('constructor', () => { it('should construct an exporter', () => { const exporter = new PrometheusExporter(); @@ -209,7 +226,7 @@ describe('PrometheusExporter', () => { assert.deepStrictEqual(lines, [ '# HELP counter a test description', '# TYPE counter counter', - 'counter{key1="labelValue1"} 20', + `counter{key1="labelValue1"} 20 ${mockedTimeMS}`, '', ]); @@ -266,7 +283,7 @@ describe('PrometheusExporter', () => { }); }); - it('should export multiple labelsets', done => { + it('should export multiple labels', done => { const counter = meter.createCounter('counter', { description: 'a test description', labelKeys: ['counterKey1'], @@ -285,8 +302,8 @@ describe('PrometheusExporter', () => { assert.deepStrictEqual(lines, [ '# HELP counter a test description', '# TYPE counter counter', - 'counter{counterKey1="labelValue1"} 10', - 'counter{counterKey1="labelValue2"} 20', + `counter{counterKey1="labelValue1"} 10 ${mockedTimeMS}`, + `counter{counterKey1="labelValue2"} 20 ${mockedTimeMS}`, '', ]); @@ -330,7 +347,7 @@ describe('PrometheusExporter', () => { assert.deepStrictEqual(lines, [ '# HELP counter description missing', '# TYPE counter counter', - 'counter 10', + `counter 10 ${mockedTimeMS}`, '', ]); @@ -356,7 +373,7 @@ describe('PrometheusExporter', () => { assert.deepStrictEqual(lines, [ '# HELP counter_bad_name description missing', '# TYPE counter_bad_name counter', - 'counter_bad_name 10', + `counter_bad_name 10 ${mockedTimeMS}`, '', ]); @@ -432,7 +449,7 @@ describe('PrometheusExporter', () => { assert.deepStrictEqual(lines, [ '# HELP test_prefix_counter description missing', '# TYPE test_prefix_counter counter', - 'test_prefix_counter 10', + `test_prefix_counter 10 ${mockedTimeMS}`, '', ]); @@ -461,7 +478,7 @@ describe('PrometheusExporter', () => { assert.deepStrictEqual(lines, [ '# HELP counter description missing', '# TYPE counter counter', - 'counter 10', + `counter 10 ${mockedTimeMS}`, '', ]); @@ -490,7 +507,7 @@ describe('PrometheusExporter', () => { assert.deepStrictEqual(lines, [ '# HELP counter description missing', '# TYPE counter counter', - 'counter 10', + `counter 10 ${mockedTimeMS}`, '', ]);