fix(@opentelemetry/exporter-prometheus): unref prometheus server to prevent process running indefinitely (#2558)

This commit is contained in:
Jack 2021-10-26 12:35:57 -07:00 committed by GitHub
parent b531acffbf
commit c1939a79a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 1 deletions

View File

@ -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

View File

@ -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;
});
@ -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';