Skip to content

Commit

Permalink
chore: adding timestamp for prometheus metric Counter (#947)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Dyla <[email protected]>
  • Loading branch information
obecny and dyladan authored Apr 8, 2020
1 parent 7ab6aba commit d1c99ae
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
6 changes: 5 additions & 1 deletion packages/opentelemetry-exporter-prometheus/src/prometheus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
37 changes: 27 additions & 10 deletions packages/opentelemetry-exporter-prometheus/test/prometheus.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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}`,
'',
]);

Expand Down Expand Up @@ -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'],
Expand All @@ -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}`,
'',
]);

Expand Down Expand Up @@ -330,7 +347,7 @@ describe('PrometheusExporter', () => {
assert.deepStrictEqual(lines, [
'# HELP counter description missing',
'# TYPE counter counter',
'counter 10',
`counter 10 ${mockedTimeMS}`,
'',
]);

Expand All @@ -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}`,
'',
]);

Expand Down Expand Up @@ -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}`,
'',
]);

Expand Down Expand Up @@ -461,7 +478,7 @@ describe('PrometheusExporter', () => {
assert.deepStrictEqual(lines, [
'# HELP counter description missing',
'# TYPE counter counter',
'counter 10',
`counter 10 ${mockedTimeMS}`,
'',
]);

Expand Down Expand Up @@ -490,7 +507,7 @@ describe('PrometheusExporter', () => {
assert.deepStrictEqual(lines, [
'# HELP counter description missing',
'# TYPE counter counter',
'counter 10',
`counter 10 ${mockedTimeMS}`,
'',
]);

Expand Down

0 comments on commit d1c99ae

Please sign in to comment.