Skip to content

Commit

Permalink
refactor(exporter-prometheus): promisify prometheus tests (#4431)
Browse files Browse the repository at this point in the history
* refactor(exporter-prometheus): promisify prometheus tests

* fix: lint

---------

Co-authored-by: Marc Pichler <marc.pichler@dynatrace.com>
  • Loading branch information
legendecas and pichlermarc committed Jan 25, 2024
1 parent df63272 commit bf4d553
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 254 deletions.
8 changes: 4 additions & 4 deletions doc/metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const {
getNodeAutoInstrumentations,
} = require("@opentelemetry/auto-instrumentations-node");

const prometheusExporter = new PrometheusExporter({ startServer: true });
const prometheusExporter = new PrometheusExporter();

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

const prometheusExporter = new PrometheusExporter({ startServer: true });
const prometheusExporter = new PrometheusExporter();

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

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

// Creates MeterProvider and installs the exporter as a MetricReader
Expand Down
3 changes: 3 additions & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ All notable changes to experimental packages in this project will be documented

### :bug: (Bug Fix)

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

### :house: (Internal)

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

## 0.47.0

### :boom: Breaking Change
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export class PrometheusExporter extends MetricReader {
private readonly _prefix?: string;
private readonly _appendTimestamp: boolean;
private _serializer: PrometheusSerializer;
private _startServerPromise: Promise<void> | undefined;

// This will be required when histogram is implemented. Leaving here so it is not forgotten
// Histogram cannot have a attribute named 'le'
Expand Down Expand Up @@ -95,7 +96,8 @@ export class PrometheusExporter extends MetricReader {
callback(err);
});
} else if (callback) {
callback();
// Do not invoke callback immediately to avoid zalgo problem.
queueMicrotask(callback);
}
}

Expand Down Expand Up @@ -142,7 +144,7 @@ export class PrometheusExporter extends MetricReader {
* Starts the Prometheus export server
*/
startServer(): Promise<void> {
return new Promise((resolve, reject) => {
this._startServerPromise ??= new Promise((resolve, reject) => {
this._server.once('error', reject);
this._server.listen(
{
Expand All @@ -157,6 +159,8 @@ export class PrometheusExporter extends MetricReader {
}
);
});

return this._startServerPromise;
}

/**
Expand Down
Loading

0 comments on commit bf4d553

Please sign in to comment.