Skip to content

Commit d1c99ae

Browse files
obecnydyladan
andauthored
chore: adding timestamp for prometheus metric Counter (#947)
Co-authored-by: Daniel Dyla <[email protected]>
1 parent 7ab6aba commit d1c99ae

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

packages/opentelemetry-exporter-prometheus/src/prometheus.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,11 @@ export class PrometheusExporter implements MetricExporter {
134134
// MetricRecord value is the current state, not the delta to be incremented by.
135135
// Currently, _registerMetric creates a new counter every time the value changes,
136136
// so the increment here behaves as a set value (increment from 0)
137-
metric.inc(labelValues, point.value as Sum);
137+
metric.inc(
138+
labelValues,
139+
point.value as Sum,
140+
hrTimeToMilliseconds(point.timestamp)
141+
);
138142
}
139143

140144
if (metric instanceof Gauge) {

packages/opentelemetry-exporter-prometheus/test/prometheus.test.ts

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,35 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { ObserverResult } from '@opentelemetry/api';
17+
import { HrTime, ObserverResult } from '@opentelemetry/api';
1818
import {
1919
CounterMetric,
20+
CounterSumAggregator,
2021
Meter,
2122
MeterProvider,
2223
ObserverMetric,
24+
Point,
2325
} from '@opentelemetry/metrics';
2426
import * as assert from 'assert';
2527
import * as http from 'http';
2628
import { PrometheusExporter } from '../src';
2729

30+
const mockedHrTime: HrTime = [1586347902211, 0];
31+
const mockedTimeMS = 1586347902211000;
32+
2833
describe('PrometheusExporter', () => {
34+
let toPoint: () => Point;
35+
before(() => {
36+
toPoint = CounterSumAggregator.prototype.toPoint;
37+
CounterSumAggregator.prototype.toPoint = function(): Point {
38+
const point = toPoint.apply(this);
39+
point.timestamp = mockedHrTime;
40+
return point;
41+
};
42+
});
43+
after(() => {
44+
CounterSumAggregator.prototype.toPoint = toPoint;
45+
});
2946
describe('constructor', () => {
3047
it('should construct an exporter', () => {
3148
const exporter = new PrometheusExporter();
@@ -209,7 +226,7 @@ describe('PrometheusExporter', () => {
209226
assert.deepStrictEqual(lines, [
210227
'# HELP counter a test description',
211228
'# TYPE counter counter',
212-
'counter{key1="labelValue1"} 20',
229+
`counter{key1="labelValue1"} 20 ${mockedTimeMS}`,
213230
'',
214231
]);
215232

@@ -266,7 +283,7 @@ describe('PrometheusExporter', () => {
266283
});
267284
});
268285

269-
it('should export multiple labelsets', done => {
286+
it('should export multiple labels', done => {
270287
const counter = meter.createCounter('counter', {
271288
description: 'a test description',
272289
labelKeys: ['counterKey1'],
@@ -285,8 +302,8 @@ describe('PrometheusExporter', () => {
285302
assert.deepStrictEqual(lines, [
286303
'# HELP counter a test description',
287304
'# TYPE counter counter',
288-
'counter{counterKey1="labelValue1"} 10',
289-
'counter{counterKey1="labelValue2"} 20',
305+
`counter{counterKey1="labelValue1"} 10 ${mockedTimeMS}`,
306+
`counter{counterKey1="labelValue2"} 20 ${mockedTimeMS}`,
290307
'',
291308
]);
292309

@@ -330,7 +347,7 @@ describe('PrometheusExporter', () => {
330347
assert.deepStrictEqual(lines, [
331348
'# HELP counter description missing',
332349
'# TYPE counter counter',
333-
'counter 10',
350+
`counter 10 ${mockedTimeMS}`,
334351
'',
335352
]);
336353

@@ -356,7 +373,7 @@ describe('PrometheusExporter', () => {
356373
assert.deepStrictEqual(lines, [
357374
'# HELP counter_bad_name description missing',
358375
'# TYPE counter_bad_name counter',
359-
'counter_bad_name 10',
376+
`counter_bad_name 10 ${mockedTimeMS}`,
360377
'',
361378
]);
362379

@@ -432,7 +449,7 @@ describe('PrometheusExporter', () => {
432449
assert.deepStrictEqual(lines, [
433450
'# HELP test_prefix_counter description missing',
434451
'# TYPE test_prefix_counter counter',
435-
'test_prefix_counter 10',
452+
`test_prefix_counter 10 ${mockedTimeMS}`,
436453
'',
437454
]);
438455

@@ -461,7 +478,7 @@ describe('PrometheusExporter', () => {
461478
assert.deepStrictEqual(lines, [
462479
'# HELP counter description missing',
463480
'# TYPE counter counter',
464-
'counter 10',
481+
`counter 10 ${mockedTimeMS}`,
465482
'',
466483
]);
467484

@@ -490,7 +507,7 @@ describe('PrometheusExporter', () => {
490507
assert.deepStrictEqual(lines, [
491508
'# HELP counter description missing',
492509
'# TYPE counter counter',
493-
'counter 10',
510+
`counter 10 ${mockedTimeMS}`,
494511
'',
495512
]);
496513

0 commit comments

Comments
 (0)