Skip to content

Commit

Permalink
Merge pull request #1221 from MichaelErmer/patch-1
Browse files Browse the repository at this point in the history
Fix: prometheus reporter to accept host option
  • Loading branch information
icebob committed Aug 3, 2023
2 parents cdd4933 + 8afed19 commit 58f51a3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/metrics/reporters/prometheus.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ class PrometheusReporter extends BaseReporter {
nodeID: registry.broker.nodeID
})
});

if (!this.opts.host && this.opts.port !== 0) {
this.opts.host = "0.0.0.0";
}
if (this.opts.port === 0) {
// host can not be used in combination with port 0 = auto port assignment
delete this.opts.host;
}
}

/**
Expand All @@ -66,7 +74,7 @@ class PrometheusReporter extends BaseReporter {

this.server = http.createServer();
this.server.on("request", this.handler.bind(this));
this.server.listen(this.opts.port, err => {
this.server.listen(this.opts.port, this.opts.host, err => {
if (err) {
/* istanbul ignore next */
return this.registry.broker.fatal(
Expand All @@ -75,7 +83,9 @@ class PrometheusReporter extends BaseReporter {
}

this.logger.info(
`Prometheus metric reporter listening on http://0.0.0.0:${this.opts.port}${this.opts.path} address.`
`Prometheus metric reporter listening on http://${this.server.address().address}:${
this.server.address().port
}${this.opts.path} address.`
);
});
this.defaultLabels = isFunction(this.opts.defaultLabels)
Expand Down
3 changes: 3 additions & 0 deletions test/unit/metrics/reporters/prometheus.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe("Test Prometheus Reporter class", () => {
metricNameFormatter: null,
labelNameFormatter: null,

host: "0.0.0.0",
port: 3030,
path: "/metrics",
defaultLabels: expect.any(Function)
Expand All @@ -39,6 +40,7 @@ describe("Test Prometheus Reporter class", () => {
metricNameFormatter: () => {},
labelNameFormatter: () => {},

host: "0.0.0.0",
port: 12345,
path: "/meter"
});
Expand All @@ -51,6 +53,7 @@ describe("Test Prometheus Reporter class", () => {
metricNameFormatter: expect.any(Function),
labelNameFormatter: expect.any(Function),

host: "0.0.0.0",
port: 12345,
path: "/meter",
defaultLabels: expect.any(Function)
Expand Down

0 comments on commit 58f51a3

Please sign in to comment.