From 239bfdd3414768133549205c05d15114bc733578 Mon Sep 17 00:00:00 2001 From: naseemkullah Date: Wed, 20 Jan 2021 20:52:15 -0500 Subject: [PATCH] feat: add prometheus exporter host and port env vars Signed-off-by: naseemkullah --- .../src/PrometheusExporter.ts | 25 +++++++++++------ .../src/export/types.ts | 5 ++++ .../test/PrometheusExporter.test.ts | 27 +++++++++++++++++-- 3 files changed, 47 insertions(+), 10 deletions(-) diff --git a/packages/opentelemetry-exporter-prometheus/src/PrometheusExporter.ts b/packages/opentelemetry-exporter-prometheus/src/PrometheusExporter.ts index d47745980ca..05b8cc4e9b2 100644 --- a/packages/opentelemetry-exporter-prometheus/src/PrometheusExporter.ts +++ b/packages/opentelemetry-exporter-prometheus/src/PrometheusExporter.ts @@ -26,16 +26,17 @@ import * as url from 'url'; import { ExporterConfig } from './export/types'; import { PrometheusSerializer } from './PrometheusSerializer'; import { PrometheusLabelsBatcher } from './PrometheusLabelsBatcher'; - export class PrometheusExporter implements MetricExporter { static readonly DEFAULT_OPTIONS = { - port: 9464, + host: process.env.OTEL_EXPORTER_PROMETHEUS_HOST ?? '0.0.0.0', + port: Number(process.env.OTEL_EXPORTER_PROMETHEUS_PORT) ?? 9464, endpoint: '/metrics', prefix: '', appendTimestamp: true, }; private readonly _logger: api.Logger; + private readonly _host: string; private readonly _port: number; private readonly _endpoint: string; private readonly _server: Server; @@ -55,6 +56,7 @@ export class PrometheusExporter implements MetricExporter { */ constructor(config: ExporterConfig = {}, callback?: () => void) { this._logger = config.logger || new api.NoopLogger(); + this._host = config.host || PrometheusExporter.DEFAULT_OPTIONS.host; this._port = config.port || PrometheusExporter.DEFAULT_OPTIONS.port; this._prefix = config.prefix || PrometheusExporter.DEFAULT_OPTIONS.prefix; this._appendTimestamp = @@ -148,12 +150,19 @@ export class PrometheusExporter implements MetricExporter { */ startServer(): Promise { return new Promise(resolve => { - this._server.listen(this._port, () => { - this._logger.debug( - `Prometheus exporter started on port ${this._port} at endpoint ${this._endpoint}` - ); - resolve(); - }); + this._server.listen( + { + port: this._port, + host: this._host, + path: this._endpoint, + }, + () => { + this._logger.debug( + `Prometheus exporter started on port ${this._host}/${this._port} at endpoint ${this._endpoint}` + ); + resolve(); + } + ); }); } diff --git a/packages/opentelemetry-exporter-prometheus/src/export/types.ts b/packages/opentelemetry-exporter-prometheus/src/export/types.ts index 40efc413cfd..a60ba6d23f3 100644 --- a/packages/opentelemetry-exporter-prometheus/src/export/types.ts +++ b/packages/opentelemetry-exporter-prometheus/src/export/types.ts @@ -39,6 +39,11 @@ export interface ExporterConfig { */ endpoint?: string; + /** + * @default '0.0.0.0' + */ + host?: string; + /** * Port number for Prometheus exporter server * diff --git a/packages/opentelemetry-exporter-prometheus/test/PrometheusExporter.test.ts b/packages/opentelemetry-exporter-prometheus/test/PrometheusExporter.test.ts index cb5ccb93bca..47cc2a2c1ec 100644 --- a/packages/opentelemetry-exporter-prometheus/test/PrometheusExporter.test.ts +++ b/packages/opentelemetry-exporter-prometheus/test/PrometheusExporter.test.ts @@ -33,6 +33,11 @@ describe('PrometheusExporter', () => { mockAggregator(LastValueAggregator); mockAggregator(HistogramAggregator); + afterEach(() => { + delete process.env.OTEL_EXPORTER_PROMETHEUS_HOST; + delete process.env.OTEL_EXPORTER_PROMETHEUS_PORT; + }); + describe('constructor', () => { it('should construct an exporter', done => { const exporter = new PrometheusExporter(); @@ -88,7 +93,8 @@ describe('PrometheusExporter', () => { }); }); - it('it should listen on a custom port and endpoint if provided', done => { + it('it should listen on a custom host, port and endpoint if provided', done => { + const host = 'prometheus.exporter.com'; const port = 9991; const endpoint = '/metric'; @@ -98,7 +104,7 @@ describe('PrometheusExporter', () => { endpoint, }, () => { - const url = `http://localhost:${port}${endpoint}`; + const url = `http://${host}:${port}${endpoint}`; http.get(url, (res: any) => { assert.strictEqual(res.statusCode, 200); exporter.shutdown().then(() => { @@ -109,6 +115,23 @@ describe('PrometheusExporter', () => { ); }); + it('it should listen on environmentally set host and port', done => { + const envSetHost = 'env-set-host'; + const envSetPort = '1234'; + process.env.OTEL_EXPORTER_PROMETHEUS_HOST = envSetHost; + process.env.OTEL_EXPORTER_PROMETHEUS_PORT = envSetPort; + + const exporter = new PrometheusExporter({}, () => { + const url = `http://${envSetHost}:${envSetPort}/metrics`; + http.get(url, (res: any) => { + assert.strictEqual(res.statusCode, 200); + exporter.shutdown().then(() => { + return done(); + }); + }); + }); + }); + it('it should not require endpoints to start with a slash', done => { const port = 9991; const endpoint = 'metric';