Skip to content

Commit

Permalink
Merge branch 'main' into haddas-approver
Browse files Browse the repository at this point in the history
  • Loading branch information
dyladan authored Feb 8, 2023
2 parents 4a4f5c0 + cb88e4e commit 85467a1
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ Approvers ([@open-telemetry/js-approvers](https://github.com/orgs/open-telemetry
- [John Bley](https://github.com/johnbley), Splunk
- [Marc Pichler](https://github.com/pichlermarc), Dynatrace
- [Mark Wolff](https://github.com/markwolff), Microsoft
- [Martin Kuba](https://github.com/martinkuba), Lightstep
- [Matthew Wear](https://github.com/mwear), LightStep
- [Naseem K. Ullah](https://github.com/naseemkullah), Transit
- [Neville Wylie](https://github.com/MSNev), Microsoft
Expand Down
2 changes: 2 additions & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ All notable changes to experimental packages in this project will be documented

### :bug: (Bug Fix)

* fix(prometheus-exporter): add possibility to respond to errors returned by `server.listen()` [#3552](https://github.com/open-telemetry/opentelemetry-js/pull/3402) @pichlermarc

### :books: (Refine Doc)

### :house: (Internal)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ 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};
const options = {port: 9464};
const exporter = new PrometheusExporter(options);

// Creates MeterProvider and installs the exporter as a MetricReader
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ export class PrometheusExporter extends MetricReader {
* @param config Exporter configuration
* @param callback Callback to be called after a server was started
*/
constructor(config: ExporterConfig = {}, callback?: () => void) {
constructor(
config: ExporterConfig = {},
callback: (error: Error | void) => void = () => {}
) {
super({
aggregationSelector: _instrumentType => Aggregation.Default(),
aggregationTemporalitySelector: _instrumentType =>
Expand Down Expand Up @@ -86,9 +89,10 @@ export class PrometheusExporter extends MetricReader {
).replace(/^([^/])/, '/$1');

if (config.preventServerStart !== true) {
this.startServer()
.then(callback)
.catch(err => diag.error(err));
this.startServer().then(callback, err => {
diag.error(err);
callback(err);
});
} else if (callback) {
callback();
}
Expand Down Expand Up @@ -137,7 +141,8 @@ export class PrometheusExporter extends MetricReader {
* Starts the Prometheus export server
*/
startServer(): Promise<void> {
return new Promise(resolve => {
return new Promise((resolve, reject) => {
this._server.once('error', reject);
this._server.listen(
{
port: this._port,
Expand All @@ -156,7 +161,7 @@ export class PrometheusExporter extends MetricReader {
/**
* Request handler that responds with the current state of metrics
* @param _request Incoming HTTP request of server instance
* @param response HTTP response objet used to response to request
* @param response HTTP response object used to response to request
*/
public getMetricsRequestHandler(
_request: IncomingMessage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,27 @@ describe('PrometheusExporter', () => {
});
});

it('should pass server error to callback when port is already in use', done => {
const firstExporter = new PrometheusExporter({}, error => {
if (error) {
// This should not happen as the port should not be already in use when the test starts.
done(error);
}
});
const secondExporter = new PrometheusExporter({}, error => {
firstExporter
.shutdown()
.then(() => secondExporter.shutdown())
.then(() =>
done(
error
? undefined
: 'Second exporter should respond with EADDRINUSE but did not pass it to callback'
)
);
});
});

it('should not start the server if preventServerStart is passed as an option', () => {
const exporter = new PrometheusExporter({ preventServerStart: true });
assert.ok(exporter['_server'].listening === false);
Expand Down

0 comments on commit 85467a1

Please sign in to comment.