Skip to content

Commit

Permalink
fix(@opentelemetry/exporter-prometheus): unref prometheus server to p…
Browse files Browse the repository at this point in the history
…revent process running indefinitely (#2558)
  • Loading branch information
mothershipper authored Oct 26, 2021
1 parent b531acf commit c1939a7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ export class PrometheusExporter implements MetricExporter {
typeof config.appendTimestamp === 'boolean'
? config.appendTimestamp
: PrometheusExporter.DEFAULT_OPTIONS.appendTimestamp;
this._server = createServer(this._requestHandler);
// unref to prevent prometheus exporter from holding the process open on exit
this._server = createServer(this._requestHandler).unref();
this._serializer = new PrometheusSerializer(
this._prefix,
this._appendTimestamp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ describe('PrometheusExporter', () => {
mockAggregator(HistogramAggregator);

afterEach(() => {
sinon.restore();
delete process.env.OTEL_EXPORTER_PROMETHEUS_HOST;
delete process.env.OTEL_EXPORTER_PROMETHEUS_PORT;
});
Expand Down Expand Up @@ -116,6 +117,16 @@ describe('PrometheusExporter', () => {
);
});

it('should unref the server to allow graceful termination', () => {
const mockServer = sinon.createStubInstance(http.Server);
const createStub = sinon.stub(http, 'createServer');
createStub.returns((mockServer as any) as http.Server);
const exporter = new PrometheusExporter({}, async () => {
await exporter.shutdown();
});
sinon.assert.calledOnce(mockServer.unref);
});

it('should listen on environmentally set host and port', () => {
process.env.OTEL_EXPORTER_PROMETHEUS_HOST = '127.0.0.1';
process.env.OTEL_EXPORTER_PROMETHEUS_PORT = '1234';
Expand Down

0 comments on commit c1939a7

Please sign in to comment.