Skip to content

Commit

Permalink
fix: setting host port via env
Browse files Browse the repository at this point in the history
  • Loading branch information
naseemkullah committed Feb 6, 2021
1 parent 09d5339 commit c6b4101
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import { PrometheusSerializer } from './PrometheusSerializer';
import { PrometheusLabelsBatcher } from './PrometheusLabelsBatcher';
export class PrometheusExporter implements MetricExporter {
static readonly DEFAULT_OPTIONS = {
host: process.env.OTEL_EXPORTER_PROMETHEUS_HOST,
port: Number(process.env.OTEL_EXPORTER_PROMETHEUS_PORT) ?? 9464,
host: undefined,
port: 9464,
endpoint: '/metrics',
prefix: '',
appendTimestamp: true,
Expand All @@ -56,8 +56,14 @@ 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._host =
config.host ||
process.env.OTEL_EXPORTER_PROMETHEUS_HOST ||
PrometheusExporter.DEFAULT_OPTIONS.host;
this._port =
config.port ||
Number(process.env.OTEL_EXPORTER_PROMETHEUS_PORT) ||
PrometheusExporter.DEFAULT_OPTIONS.port;
this._prefix = config.prefix || PrometheusExporter.DEFAULT_OPTIONS.prefix;
this._appendTimestamp =
typeof config.appendTimestamp === 'boolean'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ describe('PrometheusExporter', () => {
});
});

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

Expand All @@ -104,7 +103,7 @@ describe('PrometheusExporter', () => {
endpoint,
},
() => {
const url = `http://${host}:${port}${endpoint}`;
const url = `http://localhost:${port}${endpoint}`;
http.get(url, (res: any) => {
assert.strictEqual(res.statusCode, 200);
exporter.shutdown().then(() => {
Expand All @@ -115,21 +114,13 @@ describe('PrometheusExporter', () => {
);
});

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('should listen on environmentally set host and port', async () => {
process.env.OTEL_EXPORTER_PROMETHEUS_HOST = '127.0.0.1';
process.env.OTEL_EXPORTER_PROMETHEUS_PORT = '1234';
const exporter = new PrometheusExporter({}, () => {});
assert.strictEqual(exporter['_host'], '127.0.0.1');
assert.strictEqual(exporter['_port'], 1234);
await exporter.shutdown();
});

it('should not require endpoints to start with a slash', done => {
Expand Down

0 comments on commit c6b4101

Please sign in to comment.