Skip to content

Commit

Permalink
fix(exporter-prometheus): add appendTimestamp option to ExporterConfig (
Browse files Browse the repository at this point in the history
#1697)

* fix(exporter-prometheus): add appendTimestamp option to ExporterConfig

Signed-off-by: Antonio Franco <antoniomrfranco@gmail.com>

* chore: lint

Signed-off-by: Antonio Franco <antoniomrfranco@gmail.com>

* chore: add test

Signed-off-by: Antonio Franco <antoniomrfranco@gmail.com>

* fix(exporter-prometheus): check if appendTimestamp is boolean

Signed-off-by: Antonio Franco <antoniomrfranco@gmail.com>

* chore: lint

Signed-off-by: Antonio Franco <antoniomrfranco@gmail.com>

* fix(exporter-prometheus): change appendTimestamp property

Signed-off-by: Antonio Franco <antoniomrfranco@gmail.com>

* chore: rollback unintentional package changes

Signed-off-by: Antonio Franco <antoniomrfranco@gmail.com>

* chore(exporter-prometheus): update test

Co-authored-by: Bartlomiej Obecny <bobecny@gmail.com>

Co-authored-by: Bartlomiej Obecny <bobecny@gmail.com>
  • Loading branch information
antoniomrfranco and obecny committed Dec 14, 2020
1 parent e70a7c8 commit e032feb
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ export class PrometheusExporter implements MetricExporter {
port: 9464,
endpoint: '/metrics',
prefix: '',
appendTimestamp: true,
};

private readonly _logger: api.Logger;
private readonly _port: number;
private readonly _endpoint: string;
private readonly _server: Server;
private readonly _prefix?: string;
private readonly _appendTimestamp: boolean;
private _serializer: PrometheusSerializer;
private _batcher = new PrometheusLabelsBatcher();

Expand All @@ -56,8 +58,15 @@ export class PrometheusExporter implements MetricExporter {
this._logger = config.logger || new NoopLogger();
this._port = config.port || PrometheusExporter.DEFAULT_OPTIONS.port;
this._prefix = config.prefix || PrometheusExporter.DEFAULT_OPTIONS.prefix;
this._appendTimestamp =
typeof config.appendTimestamp === 'boolean'
? config.appendTimestamp
: PrometheusExporter.DEFAULT_OPTIONS.appendTimestamp;
this._server = createServer(this._requestHandler);
this._serializer = new PrometheusSerializer(this._prefix);
this._serializer = new PrometheusSerializer(
this._prefix,
this._appendTimestamp
);

this._endpoint = (
config.endpoint || PrometheusExporter.DEFAULT_OPTIONS.endpoint
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ export interface ExporterConfig {
* */
prefix?: string;

/**
* Append timestamp to metrics
* @default true
*/
appendTimestamp?: boolean;

/**
* Endpoint the metrics should be exposed at with preceding slash
* @default '/metrics'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,36 @@ describe('PrometheusExporter', () => {
}
);
});

it('should export a metric without timestamp', done => {
exporter = new PrometheusExporter(
{
appendTimestamp: false,
},
async () => {
await meter.collect();
exporter!.export(meter.getProcessor().checkPointSet(), () => {
http
.get('http://localhost:9464/metrics', res => {
res.on('data', chunk => {
const body = chunk.toString();
const lines = body.split('\n');

assert.deepStrictEqual(lines, [
'# HELP counter description missing',
'# TYPE counter counter',
'counter{key1="labelValue1"} 10',
'',
]);

done();
});
})
.on('error', errorHandler(done));
});
}
);
});
});
});

Expand Down

0 comments on commit e032feb

Please sign in to comment.