Skip to content

Commit 3b20a63

Browse files
committed
Serve metrics only with metrics port option
1 parent cbe294b commit 3b20a63

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

bin/configurable-http-proxy

+17-3
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ cli
8787
)
8888
.option("--insecure", "Disable SSL cert verification")
8989
.option("--host-routing", "Use host routing (host as first level of path)")
90-
.option("--disable-metrics", "Disable metrics endpoint")
90+
.option("--metrics-ip <ip>", "IP for metrics server", "0.0.0.0")
91+
.option("--metrics-port <n>", "Port of metrics server. Defaults to no metrics server")
9192
.option("--log-level <loglevel>", "Log level (debug, info, warn, error)", "info")
9293
.option(
9394
"--timeout <n>",
@@ -264,8 +265,8 @@ options.headers = args.customHeader;
264265
options.timeout = args.timeout;
265266
options.proxyTimeout = args.proxyTimeout;
266267

267-
// prometheus options
268-
options.disableMetrics = args.disableMetrics;
268+
// metrics options
269+
options.enableMetrics = !!args.metricsPort;
269270

270271
// certs need to be provided for https redirection
271272
if (!options.ssl && options.redirectPort) {
@@ -318,9 +319,14 @@ if (args.ip === "*") {
318319
listen.ip = args.ip;
319320
listen.apiIp = args.apiIp || "localhost";
320321
listen.apiPort = args.apiPort || listen.port + 1;
322+
listen.metricsIp = args.metricsIp || "0.0.0.0";
323+
listen.metricsPort = args.metricsPort;
321324

322325
proxy.proxyServer.listen(listen.port, listen.ip);
323326
proxy.apiServer.listen(listen.apiPort, listen.apiIp);
327+
if (listen.metricsPort) {
328+
proxy.metricsServer.listen(listen.metricsPort, listen.metricsIp);
329+
}
324330

325331
log.info(
326332
"Proxying %s://%s:%s to %s",
@@ -335,6 +341,14 @@ log.info(
335341
listen.apiIp || "*",
336342
listen.apiPort
337343
);
344+
if (listen.metricsPort) {
345+
log.info(
346+
"Serve metrics at %s://%s:%s/metrics",
347+
"http",
348+
listen.metricsIp,
349+
listen.metricsPort
350+
);
351+
}
338352

339353
if (args.pidFile) {
340354
log.info("Writing pid %s to %s", process.pid, args.pidFile);

lib/configproxy.js

+10-9
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,10 @@ class ConfigurableProxy extends EventEmitter {
161161
}
162162
this.errorPath = options.errorPath || path.join(__dirname, "error");
163163

164-
if (options.disableMetrics) {
165-
this.metrics = new metrics.MockMetrics();
166-
} else {
164+
if (this.options.enableMetrics) {
167165
this.metrics = new metrics.Metrics();
166+
} else {
167+
this.metrics = new metrics.MockMetrics();
168168
}
169169

170170
if (this.options.defaultTarget) {
@@ -213,6 +213,12 @@ class ConfigurableProxy extends EventEmitter {
213213
this.apiServer = http.createServer(apiCallback);
214214
}
215215

216+
// handle metrics
217+
if (this.options.enableMetrics) {
218+
var metricsCallback = logErrors(that.handleMetrics);
219+
this.metricsServer = http.createServer(metricsCallback);
220+
}
221+
216222
// proxy requests separately
217223
var proxyCallback = logErrors(this.handleProxyWeb);
218224
if (this.options.ssl) {
@@ -617,16 +623,11 @@ class ConfigurableProxy extends EventEmitter {
617623
if (req.url === "/metrics") {
618624
return this.metrics.render(res);
619625
}
626+
fail(req, res, 404);
620627
}
621628

622629
handleApiRequest(req, res) {
623630
// Handle a request to the REST API
624-
if (!this.options.disableMetrics) {
625-
var p = this.handleMetrics(req, res);
626-
if (p) {
627-
return p;
628-
}
629-
}
630631
if (res) {
631632
var that = this;
632633
res.on("finish", () => {

0 commit comments

Comments
 (0)