Skip to content

Commit 03e741b

Browse files
author
Naseem
authored
feat: add prometheus exporter host and port env vars (#1857)
1 parent 9f965b0 commit 03e741b

File tree

3 files changed

+50
-13
lines changed

3 files changed

+50
-13
lines changed

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

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,15 @@ import { PrometheusLabelsBatcher } from './PrometheusLabelsBatcher';
2929

3030
export class PrometheusExporter implements MetricExporter {
3131
static readonly DEFAULT_OPTIONS = {
32+
host: undefined,
3233
port: 9464,
3334
endpoint: '/metrics',
3435
prefix: '',
3536
appendTimestamp: true,
3637
};
3738

3839
private readonly _logger: api.Logger;
40+
private readonly _host?: string;
3941
private readonly _port: number;
4042
private readonly _endpoint: string;
4143
private readonly _server: Server;
@@ -55,7 +57,14 @@ export class PrometheusExporter implements MetricExporter {
5557
*/
5658
constructor(config: ExporterConfig = {}, callback?: () => void) {
5759
this._logger = config.logger || new api.NoopLogger();
58-
this._port = config.port || PrometheusExporter.DEFAULT_OPTIONS.port;
60+
this._host =
61+
config.host ||
62+
process.env.OTEL_EXPORTER_PROMETHEUS_HOST ||
63+
PrometheusExporter.DEFAULT_OPTIONS.host;
64+
this._port =
65+
config.port ||
66+
Number(process.env.OTEL_EXPORTER_PROMETHEUS_PORT) ||
67+
PrometheusExporter.DEFAULT_OPTIONS.port;
5968
this._prefix = config.prefix || PrometheusExporter.DEFAULT_OPTIONS.prefix;
6069
this._appendTimestamp =
6170
typeof config.appendTimestamp === 'boolean'
@@ -72,7 +81,9 @@ export class PrometheusExporter implements MetricExporter {
7281
).replace(/^([^/])/, '/$1');
7382

7483
if (config.preventServerStart !== true) {
75-
this.startServer().then(callback);
84+
this.startServer()
85+
.then(callback)
86+
.catch(err => this._logger.error(err));
7687
} else if (callback) {
7788
callback();
7889
}
@@ -148,12 +159,18 @@ export class PrometheusExporter implements MetricExporter {
148159
*/
149160
startServer(): Promise<void> {
150161
return new Promise(resolve => {
151-
this._server.listen(this._port, () => {
152-
this._logger.debug(
153-
`Prometheus exporter started on port ${this._port} at endpoint ${this._endpoint}`
154-
);
155-
resolve();
156-
});
162+
this._server.listen(
163+
{
164+
port: this._port,
165+
host: this._host,
166+
},
167+
() => {
168+
this._logger.debug(
169+
`Prometheus exporter server started: ${this._host}:${this._port}/${this._endpoint}`
170+
);
171+
resolve();
172+
}
173+
);
157174
});
158175
}
159176

packages/opentelemetry-exporter-prometheus/src/export/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ export interface ExporterConfig {
3939
*/
4040
endpoint?: string;
4141

42+
/**
43+
* @default undefined (all interfaces)
44+
*/
45+
host?: string;
46+
4247
/**
4348
* Port number for Prometheus exporter server
4449
*

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ describe('PrometheusExporter', () => {
3535
mockAggregator(LastValueAggregator);
3636
mockAggregator(HistogramAggregator);
3737

38+
afterEach(() => {
39+
delete process.env.OTEL_EXPORTER_PROMETHEUS_HOST;
40+
delete process.env.OTEL_EXPORTER_PROMETHEUS_PORT;
41+
});
42+
3843
describe('constructor', () => {
3944
it('should construct an exporter', done => {
4045
const exporter = new PrometheusExporter();
@@ -64,7 +69,7 @@ describe('PrometheusExporter', () => {
6469
});
6570

6671
describe('server', () => {
67-
it('it should start on startServer() and call the callback', done => {
72+
it('should start on startServer() and call the callback', done => {
6873
const exporter = new PrometheusExporter({
6974
port: 9722,
7075
preventServerStart: true,
@@ -76,7 +81,7 @@ describe('PrometheusExporter', () => {
7681
});
7782
});
7883

79-
it('it should listen on the default port and default endpoint', done => {
84+
it('should listen on the default port and default endpoint', done => {
8085
const port = PrometheusExporter.DEFAULT_OPTIONS.port;
8186
const endpoint = PrometheusExporter.DEFAULT_OPTIONS.endpoint;
8287
const exporter = new PrometheusExporter({}, () => {
@@ -90,7 +95,7 @@ describe('PrometheusExporter', () => {
9095
});
9196
});
9297

93-
it('it should listen on a custom port and endpoint if provided', done => {
98+
it('should listen on a custom port and endpoint if provided', done => {
9499
const port = 9991;
95100
const endpoint = '/metric';
96101

@@ -111,7 +116,17 @@ describe('PrometheusExporter', () => {
111116
);
112117
});
113118

114-
it('it should not require endpoints to start with a slash', done => {
119+
it('should listen on environmentally set host and port', () => {
120+
process.env.OTEL_EXPORTER_PROMETHEUS_HOST = '127.0.0.1';
121+
process.env.OTEL_EXPORTER_PROMETHEUS_PORT = '1234';
122+
const exporter = new PrometheusExporter({}, async () => {
123+
await exporter.shutdown();
124+
});
125+
assert.strictEqual(exporter['_host'], '127.0.0.1');
126+
assert.strictEqual(exporter['_port'], 1234);
127+
});
128+
129+
it('should not require endpoints to start with a slash', done => {
115130
const port = 9991;
116131
const endpoint = 'metric';
117132

@@ -146,7 +161,7 @@ describe('PrometheusExporter', () => {
146161
);
147162
});
148163

149-
it('it should return a HTTP status 404 if the endpoint does not match', done => {
164+
it('should return a HTTP status 404 if the endpoint does not match', done => {
150165
const port = 9912;
151166
const endpoint = '/metrics';
152167
const exporter = new PrometheusExporter(

0 commit comments

Comments
 (0)