Skip to content

Commit c4fe9fc

Browse files
committed
Prometheus disabled by default
1 parent 546b735 commit c4fe9fc

File tree

4 files changed

+46
-19
lines changed

4 files changed

+46
-19
lines changed

README.md

+10-9
Original file line numberDiff line numberDiff line change
@@ -270,17 +270,18 @@ openssl pkcs12 -export -in cert.pem -inkey privkey.pem -out certpkcs12.pfx
270270

271271
## Prometheus Metrics
272272

273-
By default the server uses `express-prom-bundle` middleware to expose http performance
274-
metrics in `/metrics`.
273+
To expose http performance metrics, set the `PROMETHEUS_ENABLED` environment variable to true, the metrics will be available at `/metrics`. This uses the [`express-prom-bundle`](https://github.com/jochen-schweizer/express-prom-bundle) middleware
274+
275275
You can configure these metrics using the following variables:
276276

277-
| Variable | Description | Default Value |
278-
| -------- | ----------- | ------------- |
279-
| PROMETHEUS_DISABLED | Toggles off the prometheus middleware | false |
280-
| PROMETHEUS_WITH_PATH | Partitions the metrics by the requested path | false |
281-
| PROMETHEUS_WITH_METHOD | Partitions the metrics by HTTP method | true |
282-
| PROMETHEUS_WITH_STATUS | Partitions the metrics by HTTP status | true |
283-
| PROMETHEUS_METRIC_TYPE | Sets the type of metric, histogram or summary | summary |
277+
| Variable | Description | Default Value |
278+
| ----------------------- | --------------------------------------------- | ------------- |
279+
| PROMETHEUS_ENABLED | Toggles on the prometheus middleware | false |
280+
| PROMETHEUS_METRICS_PATH | The path at which the metrics will be visible | /metrics |
281+
| PROMETHEUS_WITH_PATH | Partitions the metrics by the requested path | false |
282+
| PROMETHEUS_WITH_METHOD | Partitions the metrics by HTTP method | true |
283+
| PROMETHEUS_WITH_STATUS | Partitions the metrics by HTTP status | true |
284+
| PROMETHEUS_METRIC_TYPE | Sets the type of metric, histogram or summary | summary |
284285

285286
> Please check the middleware [documentation](https://github.com/jochen-schweizer/express-prom-bundle#options) for more details.
286287

docker-compose.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ services:
55
environment:
66
- HTTP_PORT=8888
77
- HTTPS_PORT=9999
8-
- PROMETHEUS_DISABLED=false
8+
- PROMETHEUS_ENABLED=true
9+
- PROMETHEUS_METRICS_PATH=/metrics
910
- PROMETHEUS_WITH_PATH=false
1011
- PROMETHEUS_WITH_METHOD=true
1112
- PROMETHEUS_WITH_STATUS=true

index.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ const { promisify } = require('util');
99
const promBundle = require("express-prom-bundle");
1010

1111
const {
12-
PROMETHEUS_DISABLED = false,
12+
PROMETHEUS_ENABLED = false,
13+
PROMETHEUS_METRICS_PATH = '/metrics',
1314
PROMETHEUS_WITH_PATH = false,
1415
PROMETHEUS_WITH_METHOD = 'true',
1516
PROMETHEUS_WITH_STATUS = 'true',
@@ -18,6 +19,7 @@ const {
1819

1920
const sleep = promisify(setTimeout);
2021
const metricsMiddleware = promBundle({
22+
metricsPath: PROMETHEUS_METRICS_PATH,
2123
includePath: (PROMETHEUS_WITH_PATH == 'true'),
2224
includeMethod: (PROMETHEUS_WITH_METHOD == 'true'),
2325
includeStatusCode: (PROMETHEUS_WITH_STATUS == 'true'),
@@ -27,7 +29,8 @@ const metricsMiddleware = promBundle({
2729
const app = express()
2830
app.set('json spaces', 2);
2931
app.set('trust proxy', ['loopback', 'linklocal', 'uniquelocal']);
30-
if(PROMETHEUS_DISABLED !== 'true') {
32+
33+
if(PROMETHEUS_ENABLED === 'true') {
3134
app.use(metricsMiddleware);
3235
}
3336

tests.sh

+29-7
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ fi
256256

257257
message " Stop containers "
258258
docker stop http-echo-tests
259+
sleep 5
259260

260261
message " Start container with DISABLE_REQUEST_LOGS "
261262
docker run -d --rm -e DISABLE_REQUEST_LOGS=true --name http-echo-tests -p 8080:8080 -p 8443:8443 -t mendhak/http-https-echo
@@ -442,18 +443,39 @@ message " Stop containers "
442443
docker stop http-echo-tests
443444
sleep 5
444445

445-
message " Start container with PROMETHEUS "
446-
docker run -d --rm -e --name http-echo-tests -p 8080:8080 -p 8443:8443 -t mendhak/http-https-echo
446+
message " Start container with PROMETHEUS disabled "
447+
docker run -d --rm --name http-echo-tests -p 8080:8080 -p 8443:8443 -t mendhak/http-https-echo
447448
sleep 5
448449
curl -s -k -X POST -d "tiramisu" https://localhost:8443/ > /dev/null
449-
METRICS_CHECK="$(curl -sk http://localhost:8080/metrics | grep http_request_duration_seconds_count | grep GET | cut -d' ' -f2)"
450450

451-
if [ "$METRICS_CHECK" == "1" ]
451+
# grep for http_request_duration_seconds_count ensure it is not present at /metric path
452+
453+
METRICS_CHECK="$(curl -sk http://localhost:8080/metrics | grep -v http_request_duration_seconds_count )"
454+
455+
if [[ "$METRICS_CHECK" == *"http_request_duration_seconds_count"* ]]
452456
then
453-
passed "PROMETHEUS metrics are working"
457+
failed "PROMETHEUS metrics are enabled"
458+
exit 1
454459
else
455-
failed "PROMETHEUS metrics are not working"
456-
docker logs http-echo-tests
460+
passed "PROMETHEUS metrics are disabled by default"
461+
fi
462+
463+
message " Stop containers "
464+
docker stop http-echo-tests
465+
sleep 5
466+
467+
message " Start container with PROMETHEUS enabled "
468+
docker run -d -e PROMETHEUS_ENABLED=true --rm --name http-echo-tests -p 8080:8080 -p 8443:8443 -t mendhak/http-https-echo
469+
sleep 5
470+
curl -s -k -X POST -d "tiramisu" https://localhost:8443/ > /dev/null
471+
472+
METRICS_CHECK="$(curl -sk http://localhost:8080/metrics | grep http_request_duration_seconds_count )"
473+
474+
if [[ "$METRICS_CHECK" == *"http_request_duration_seconds_count"* ]]
475+
then
476+
passed "PROMETHEUS metrics are enabled"
477+
else
478+
failed "PROMETHEUS metrics are disabled"
457479
exit 1
458480
fi
459481

0 commit comments

Comments
 (0)