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

Adds a test case as well, but, unfortunately, just that the server is
unrefed. Not immediately clear how we could test based on open handles
or reference counting to get at the behavior over the specific
implementation.
  • Loading branch information
mothershipper committed Oct 26, 2021
1 parent b531acf commit 4fc5b3b
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 4fc5b3b

Please sign in to comment.