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(exporter-prometheus): add appendTimestamp option to ExporterConfig #1697

Merged
merged 12 commits into from
Dec 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
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