Skip to content

Commit 369f429

Browse files
committed
refactor(exporter-prometheus): promisify prometheus tests
1 parent 43e598e commit 369f429

File tree

4 files changed

+158
-254
lines changed

4 files changed

+158
-254
lines changed

doc/metrics.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ const {
5959
getNodeAutoInstrumentations,
6060
} = require("@opentelemetry/auto-instrumentations-node");
6161

62-
const prometheusExporter = new PrometheusExporter({ startServer: true });
62+
const prometheusExporter = new PrometheusExporter();
6363

6464
const sdk = new opentelemetry.NodeSDK({
6565
// Optional - If omitted, the metrics SDK will not be initialized
@@ -147,7 +147,7 @@ const {
147147
getNodeAutoInstrumentations,
148148
} = require("@opentelemetry/auto-instrumentations-node");
149149

150-
const prometheusExporter = new PrometheusExporter({ startServer: true });
150+
const prometheusExporter = new PrometheusExporter();
151151

152152
const sdk = new opentelemetry.NodeSDK({
153153
// Optional - If omitted, the metrics SDK will not be initialized
@@ -499,8 +499,8 @@ to use the Prometheus exporter `PrometheusExporter` which is included in the
499499
const { PrometheusExporter } = require('@opentelemetry/exporter-prometheus');
500500
const { MeterProvider } = require('@opentelemetry/sdk-metrics');
501501

502-
// Add your port and startServer to the Prometheus options
503-
const options = { port: 9464, startServer: true };
502+
// Add your port to the Prometheus options
503+
const options = { port: 9464 };
504504
const exporter = new PrometheusExporter(options);
505505

506506
// Creates MeterProvider and installs the exporter as a MetricReader

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export class PrometheusExporter extends MetricReader {
4444
private readonly _prefix?: string;
4545
private readonly _appendTimestamp: boolean;
4646
private _serializer: PrometheusSerializer;
47+
private _startServerPromise: Promise<void> | undefined;
4748

4849
// This will be required when histogram is implemented. Leaving here so it is not forgotten
4950
// Histogram cannot have a attribute named 'le'
@@ -95,7 +96,8 @@ export class PrometheusExporter extends MetricReader {
9596
callback(err);
9697
});
9798
} else if (callback) {
98-
callback();
99+
// Do not invoke callback immediately to avoid zalgo problem.
100+
queueMicrotask(callback);
99101
}
100102
}
101103

@@ -142,7 +144,7 @@ export class PrometheusExporter extends MetricReader {
142144
* Starts the Prometheus export server
143145
*/
144146
startServer(): Promise<void> {
145-
return new Promise((resolve, reject) => {
147+
this._startServerPromise ??= new Promise((resolve, reject) => {
146148
this._server.once('error', reject);
147149
this._server.listen(
148150
{
@@ -157,6 +159,8 @@ export class PrometheusExporter extends MetricReader {
157159
}
158160
);
159161
});
162+
163+
return this._startServerPromise;
160164
}
161165

162166
/**

0 commit comments

Comments
 (0)