Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add prometheus exporter host and port env vars #1857

Merged
merged 2 commits into from
Feb 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ import { PrometheusLabelsBatcher } from './PrometheusLabelsBatcher';

export class PrometheusExporter implements MetricExporter {
static readonly DEFAULT_OPTIONS = {
host: undefined,
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;
Expand All @@ -55,7 +57,14 @@ export class PrometheusExporter implements MetricExporter {
*/
constructor(config: ExporterConfig = {}, callback?: () => void) {
this._logger = config.logger || new api.NoopLogger();
this._port = config.port || PrometheusExporter.DEFAULT_OPTIONS.port;
this._host =
config.host ||
process.env.OTEL_EXPORTER_PROMETHEUS_HOST ||
naseemkullah marked this conversation as resolved.
Show resolved Hide resolved
PrometheusExporter.DEFAULT_OPTIONS.host;
this._port =
config.port ||
Number(process.env.OTEL_EXPORTER_PROMETHEUS_PORT) ||
naseemkullah marked this conversation as resolved.
Show resolved Hide resolved
PrometheusExporter.DEFAULT_OPTIONS.port;
this._prefix = config.prefix || PrometheusExporter.DEFAULT_OPTIONS.prefix;
this._appendTimestamp =
typeof config.appendTimestamp === 'boolean'
Expand All @@ -72,7 +81,9 @@ export class PrometheusExporter implements MetricExporter {
).replace(/^([^/])/, '/$1');

if (config.preventServerStart !== true) {
this.startServer().then(callback);
this.startServer()
.then(callback)
.catch(err => this._logger.error(err));
naseemkullah marked this conversation as resolved.
Show resolved Hide resolved
} else if (callback) {
callback();
}
Expand Down Expand Up @@ -148,12 +159,18 @@ export class PrometheusExporter implements MetricExporter {
*/
startServer(): Promise<void> {
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,
},
() => {
this._logger.debug(
`Prometheus exporter server started: ${this._host}:${this._port}/${this._endpoint}`
);
resolve();
}
);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ export interface ExporterConfig {
*/
endpoint?: string;

/**
* @default undefined (all interfaces)
*/
host?: string;

/**
* Port number for Prometheus exporter server
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,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();
Expand Down Expand Up @@ -64,7 +69,7 @@ describe('PrometheusExporter', () => {
});

describe('server', () => {
it('it should start on startServer() and call the callback', done => {
it('should start on startServer() and call the callback', done => {
const exporter = new PrometheusExporter({
port: 9722,
preventServerStart: true,
Expand All @@ -76,7 +81,7 @@ describe('PrometheusExporter', () => {
});
});

it('it should listen on the default port and default endpoint', done => {
it('should listen on the default port and default endpoint', done => {
const port = PrometheusExporter.DEFAULT_OPTIONS.port;
const endpoint = PrometheusExporter.DEFAULT_OPTIONS.endpoint;
const exporter = new PrometheusExporter({}, () => {
Expand All @@ -90,7 +95,7 @@ describe('PrometheusExporter', () => {
});
});

it('it should listen on a custom port and endpoint if provided', done => {
it('should listen on a custom port and endpoint if provided', done => {
const port = 9991;
const endpoint = '/metric';

Expand All @@ -111,7 +116,17 @@ describe('PrometheusExporter', () => {
);
});

it('it should not require endpoints to start with a slash', done => {
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';
const exporter = new PrometheusExporter({}, async () => {
await exporter.shutdown();
});
assert.strictEqual(exporter['_host'], '127.0.0.1');
assert.strictEqual(exporter['_port'], 1234);
});

it('should not require endpoints to start with a slash', done => {
const port = 9991;
const endpoint = 'metric';

Expand Down Expand Up @@ -146,7 +161,7 @@ describe('PrometheusExporter', () => {
);
});

it('it should return a HTTP status 404 if the endpoint does not match', done => {
it('should return a HTTP status 404 if the endpoint does not match', done => {
const port = 9912;
const endpoint = '/metrics';
const exporter = new PrometheusExporter(
Expand Down