Skip to content

Commit bf4d553

Browse files
refactor(exporter-prometheus): promisify prometheus tests (#4431)
* refactor(exporter-prometheus): promisify prometheus tests * fix: lint --------- Co-authored-by: Marc Pichler <[email protected]>
1 parent df63272 commit bf4d553

File tree

5 files changed

+154
-254
lines changed

5 files changed

+154
-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/CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ All notable changes to experimental packages in this project will be documented
1616

1717
### :bug: (Bug Fix)
1818

19+
* fix(exporter-prometheus): avoid invoking callback synchronously [#4431](https://github.com/open-telemetry/opentelemetry-js/pull/4431) @legendecas
1920
* fix(exporter-logs-otlp-grpc): set User-Agent header [#4398](https://github.com/open-telemetry/opentelemetry-js/pull/4398) @Vunovati
2021
* fix(exporter-logs-otlp-http): set User-Agent header [#4398](https://github.com/open-telemetry/opentelemetry-js/pull/4398) @Vunovati
2122
* fix(exporter-logs-otlp-proto): set User-Agent header [#4398](https://github.com/open-telemetry/opentelemetry-js/pull/4398) @Vunovati
@@ -24,6 +25,8 @@ All notable changes to experimental packages in this project will be documented
2425

2526
### :house: (Internal)
2627

28+
* refactor(exporter-prometheus): promisify prometheus tests [#4431](https://github.com/open-telemetry/opentelemetry-js/pull/4431) @legendecas
29+
2730
## 0.47.0
2831

2932
### :boom: Breaking Change

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)