Skip to content

Commit 4fc5b3b

Browse files
committed
fix(@opentelemetry/exporter-prometheus): unref prometheus server to prevent 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.
1 parent b531acf commit 4fc5b3b

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

experimental/packages/opentelemetry-exporter-prometheus/src/PrometheusExporter.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ export class PrometheusExporter implements MetricExporter {
6868
typeof config.appendTimestamp === 'boolean'
6969
? config.appendTimestamp
7070
: PrometheusExporter.DEFAULT_OPTIONS.appendTimestamp;
71-
this._server = createServer(this._requestHandler);
71+
// unref to prevent prometheus exporter from holding the process open on exit
72+
this._server = createServer(this._requestHandler).unref();
7273
this._serializer = new PrometheusSerializer(
7374
this._prefix,
7475
this._appendTimestamp

experimental/packages/opentelemetry-exporter-prometheus/test/PrometheusExporter.test.ts

+11
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ describe('PrometheusExporter', () => {
3636
mockAggregator(HistogramAggregator);
3737

3838
afterEach(() => {
39+
sinon.restore();
3940
delete process.env.OTEL_EXPORTER_PROMETHEUS_HOST;
4041
delete process.env.OTEL_EXPORTER_PROMETHEUS_PORT;
4142
});
@@ -116,6 +117,16 @@ describe('PrometheusExporter', () => {
116117
);
117118
});
118119

120+
it('should unref the server to allow graceful termination', () => {
121+
const mockServer = sinon.createStubInstance(http.Server);
122+
const createStub = sinon.stub(http, 'createServer');
123+
createStub.returns((mockServer as any) as http.Server);
124+
const exporter = new PrometheusExporter({}, async () => {
125+
await exporter.shutdown();
126+
});
127+
sinon.assert.calledOnce(mockServer.unref);
128+
});
129+
119130
it('should listen on environmentally set host and port', () => {
120131
process.env.OTEL_EXPORTER_PROMETHEUS_HOST = '127.0.0.1';
121132
process.env.OTEL_EXPORTER_PROMETHEUS_PORT = '1234';

0 commit comments

Comments
 (0)