Skip to content

Commit b75e7e6

Browse files
authored
Merge pull request #57 from mxcoder/issue-56-prometheus-exporter
#56 Feat: Add middleware for prometheus metrics
2 parents 9a3b1e4 + b84cbb3 commit b75e7e6

8 files changed

Lines changed: 135 additions & 47 deletions

File tree

‎.github/workflows/build.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ jobs:
4949
uses: docker/build-push-action@v4
5050
with:
5151
context: .
52-
platforms: linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64/v8,linux/ppc64le,linux/s390x
52+
platforms: linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64/v8,linux/ppc64le
5353
push: false
5454
tags: ${{ steps.meta.outputs.tags }}
5555
labels: ${{ steps.meta.outputs.labels }}

‎.github/workflows/publish.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ jobs:
5353
uses: docker/build-push-action@v4
5454
with:
5555
context: .
56-
platforms: linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64/v8,linux/ppc64le,linux/s390x
56+
platforms: linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64/v8,linux/ppc64le
5757
push: true
5858
tags: ${{ steps.meta.outputs.tags }}
5959
labels: ${{ steps.meta.outputs.labels }}

‎.vscode/settings.json‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"markdown.extension.toc.updateOnSave": false
3+
}

‎README.md‎

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/mendhak/docker-http-https-echo/build.yml?color=darkgreen&style=for-the-badge&branch=master)](https://github.com/mendhak/docker-http-https-echo/actions?query=workflow%3ABuild)
44

55

6-
76
`mendhak/http-https-echo` is a Docker image that can echo various HTTP request properties back to client in the response, as well as in the Docker container logs.
87
It comes with various options that can manipulate the response output, see the table of contents for a full list.
98

109
![browser](https://raw.githubusercontent.com/mendhak/docker-http-https-echo/master/screenshots/screenshot.png)
1110

12-
The image is available on [Docker Hub](https://hub.docker.com/r/mendhak/http-https-echo): `mendhak/http-https-echo:30`
11+
The image is available on [Docker Hub](https://hub.docker.com/r/mendhak/http-https-echo): `mendhak/http-https-echo:30`
1312
The image is available on [Github Container Registry](https://github.com/mendhak/docker-http-https-echo/pkgs/container/http-https-echo): `ghcr.io/mendhak/http-https-echo:30`
1413

1514
Please do not use the `:latest` tag as it will break without warning, use a specific version instead.
@@ -31,7 +30,8 @@ This image is executed as non root by default and is fully compliant with Kubern
3130
- [Add a delay before response](#add-a-delay-before-response)
3231
- [Only return body in the response](#only-return-body-in-the-response)
3332
- [Include environment variables in the response](#include-environment-variables-in-the-response)
34-
- [Client certificate details (mTLS) in the response](#client-certificate-details--mtls--in-the-response)
33+
- [Client certificate details (mTLS) in the response](#client-certificate-details-mtls-in-the-response)
34+
- [Prometheus Metrics](#prometheus-metrics)
3535
- [Output](#output)
3636
- [Building](#building)
3737
- [Changelog](#changelog)
@@ -75,9 +75,9 @@ With docker compose, this would be:
7575

7676
## Use your own certificates
7777

78-
The certificates are at `/app/fullchain.pem` and `/app/privkey.pem`.
78+
The certificates are at `/app/fullchain.pem` and `/app/privkey.pem`.
7979

80-
You can use volume mounting to substitute the certificate and private key with your own.
80+
You can use volume mounting to substitute the certificate and private key with your own.
8181

8282
my-http-listener:
8383
image: mendhak/http-https-echo:30
@@ -166,7 +166,7 @@ docker run -e ECHO_BACK_TO_CLIENT=false -p 8080:8080 -p 8443:8443 --rm -t mendha
166166

167167
## Custom status code
168168

169-
Use `x-set-response-status-code` to set a custom status code.
169+
Use `x-set-response-status-code` to set a custom status code.
170170

171171
You can send it as a header:
172172

@@ -188,7 +188,7 @@ That will cause the reponse status code to be:
188188

189189
## Set response Content-Type
190190

191-
Use `x-set-response-content-type` to set the Content-Type of the response.
191+
Use `x-set-response-content-type` to set the Content-Type of the response.
192192

193193
You can send it as a header:
194194

@@ -211,61 +211,77 @@ This will cause the response content type to be:
211211

212212
## Add a delay before response
213213

214-
Use `x-set-response-delay-ms` to set a custom delay in milliseconds. This will allow you to simulate slow responses.
214+
Use `x-set-response-delay-ms` to set a custom delay in milliseconds. This will allow you to simulate slow responses.
215215

216216
You can send it as a header:
217217

218218
```bash
219219
curl -v -H "x-set-response-delay-ms: 6000" http://localhost:8080/
220220
```
221221

222-
You can send it as a querystring parameter:
222+
You can send it as a querystring parameter:
223223

224224
```bash
225225
curl -v http://localhost:8080/some/path?x-set-response-delay-ms=6000
226226
```
227227

228228
## Only return body in the response
229229

230-
Use the querystring parameter, `response_body_only=true` to get just the request body in the response, none of the associated metadata.
230+
Use the querystring parameter, `response_body_only=true` to get just the request body in the response, none of the associated metadata.
231231

232232
```bash
233233
curl -s -k -X POST -d 'cauliflower' http://localhost:8080/a/b/c?response_body_only=true
234234
```
235235

236-
The output will be 'cauliflower'.
236+
The output will be 'cauliflower'.
237237

238238
## Include environment variables in the response
239239

240-
You can have environment variables (that are visible to the echo server's process) added to the response body. Because this could contain sensitive information, it is not a default behavior.
240+
You can have environment variables (that are visible to the echo server's process) added to the response body. Because this could contain sensitive information, it is not a default behavior.
241241

242-
Pass the `ECHO_INCLUDE_ENV_VARS=1` environment variable in.
242+
Pass the `ECHO_INCLUDE_ENV_VARS=1` environment variable in.
243243

244244
```bash
245245
docker run -d --rm -e ECHO_INCLUDE_ENV_VARS=1 --name http-echo-tests -p 8080:8080 -p 8443:8443 -t mendhak/http-https-echo:30
246246
```
247247

248-
Then do a normal request via curl or browser, and you will see the `env` property in the response body.
248+
Then do a normal request via curl or browser, and you will see the `env` property in the response body.
249249

250250

251251
## Client certificate details (mTLS) in the response
252252

253-
To get client certificate details in the response body, start the container with `MTLS_ENABLE=1` environment variable. When passing a client certificate, the details about that certificate can be echoed back in the response body. The client certificate will not be validated.
253+
To get client certificate details in the response body, start the container with `MTLS_ENABLE=1` environment variable. When passing a client certificate, the details about that certificate can be echoed back in the response body. The client certificate will not be validated.
254254

255-
For example, invoke using curl, passing a certificate and key.
255+
For example, invoke using curl, passing a certificate and key.
256256

257257
```bash
258258
curl -k --cert cert.pem --key privkey.pem https://localhost:8443/
259259
```
260260

261-
The response body will contain details about the client certificate passed in.
261+
The response body will contain details about the client certificate passed in.
262262

263-
If you browse to https://localhost:8443/ in Firefox, you won't get prompted to supply a client certificate unless you have [an imported certificate by the same issuer as the server](https://superuser.com/questions/1043415/firefox-doesnt-ask-me-for-a-certificate-when-visiting-a-site-that-needs-one). If you need browser prompting to work, you'll need to follow the 'use your own certificates' section. Firefox needs the imported certificate to be in a PKCS12 format, so if you have a certificate and key already, you can combine them using
263+
If you browse to https://localhost:8443/ in Firefox, you won't get prompted to supply a client certificate unless you have [an imported certificate by the same issuer as the server](https://superuser.com/questions/1043415/firefox-doesnt-ask-me-for-a-certificate-when-visiting-a-site-that-needs-one). If you need browser prompting to work, you'll need to follow the 'use your own certificates' section. Firefox needs the imported certificate to be in a PKCS12 format, so if you have a certificate and key already, you can combine them using
264264

265265
```bash
266266
openssl pkcs12 -export -in cert.pem -inkey privkey.pem -out certpkcs12.pfx
267267
```
268268

269+
## Prometheus Metrics
270+
271+
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
272+
273+
You can configure these metrics using the following variables:
274+
275+
| Variable | Description | Default Value |
276+
| ----------------------- | --------------------------------------------- | ------------- |
277+
| PROMETHEUS_ENABLED | Toggles on the prometheus middleware | false |
278+
| PROMETHEUS_METRICS_PATH | The path at which the metrics will be visible | /metrics |
279+
| PROMETHEUS_WITH_PATH | Partitions the metrics by the requested path | false |
280+
| PROMETHEUS_WITH_METHOD | Partitions the metrics by HTTP method | true |
281+
| PROMETHEUS_WITH_STATUS | Partitions the metrics by HTTP status | true |
282+
| PROMETHEUS_METRIC_TYPE | Sets the type of metric, histogram or summary | summary |
283+
284+
> Please check the middleware [documentation](https://github.com/jochen-schweizer/express-prom-bundle#options) for more details.
269285
270286
## Output
271287

‎docker-compose.yml‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
version: '3'
22
services:
3-
43
my-http-listener:
54
image: mendhak/http-https-echo:30
6-
environment:
5+
environment:
76
- HTTP_PORT=8888
87
- HTTPS_PORT=9999
8+
- PROMETHEUS_ENABLED=true
9+
- PROMETHEUS_METRICS_PATH=/metrics
10+
- PROMETHEUS_WITH_PATH=false
11+
- PROMETHEUS_WITH_METHOD=true
12+
- PROMETHEUS_WITH_STATUS=true
13+
- PROMETHEUS_METRIC_TYPE=summary
914
ports:
1015
- "8080:8888"
1116
- "8443:9999"

‎index.js‎

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,39 @@
1-
var express = require('express')
2-
const morgan = require('morgan');
3-
var http = require('http')
4-
var https = require('https')
5-
var app = express()
61
const os = require('os');
72
const jwt = require('jsonwebtoken');
8-
var concat = require('concat-stream');
3+
const http = require('http')
4+
const https = require('https')
5+
const morgan = require('morgan');
6+
const express = require('express')
7+
const concat = require('concat-stream');
98
const { promisify } = require('util');
9+
const promBundle = require("express-prom-bundle");
10+
11+
const {
12+
PROMETHEUS_ENABLED = false,
13+
PROMETHEUS_METRICS_PATH = '/metrics',
14+
PROMETHEUS_WITH_PATH = false,
15+
PROMETHEUS_WITH_METHOD = 'true',
16+
PROMETHEUS_WITH_STATUS = 'true',
17+
PROMETHEUS_METRIC_TYPE = 'summary',
18+
} = process.env
19+
1020
const sleep = promisify(setTimeout);
21+
const metricsMiddleware = promBundle({
22+
metricsPath: PROMETHEUS_METRICS_PATH,
23+
includePath: (PROMETHEUS_WITH_PATH == 'true'),
24+
includeMethod: (PROMETHEUS_WITH_METHOD == 'true'),
25+
includeStatusCode: (PROMETHEUS_WITH_STATUS == 'true'),
26+
metricType: PROMETHEUS_METRIC_TYPE,
27+
});
1128

29+
const app = express()
1230
app.set('json spaces', 2);
1331
app.set('trust proxy', ['loopback', 'linklocal', 'uniquelocal']);
1432

33+
if(PROMETHEUS_ENABLED === 'true') {
34+
app.use(metricsMiddleware);
35+
}
36+
1537
if(process.env.DISABLE_REQUEST_LOGS !== 'true'){
1638
app.use(morgan('combined'));
1739
}
@@ -48,11 +70,11 @@ app.all('*', (req, res) => {
4870
};
4971

5072
//Add client certificate details to the output, if present
51-
//This only works if `requestCert` is true when starting the server.
73+
//This only works if `requestCert` is true when starting the server.
5274
if(req.socket.getPeerCertificate){
5375
echo.clientCertificate = req.socket.getPeerCertificate();
5476
}
55-
77+
5678
//Include visible environment variables
5779
if(process.env.ECHO_INCLUDE_ENV_VARS){
5880
echo.env = process.env;
@@ -91,33 +113,33 @@ app.all('*', (req, res) => {
91113
if(setResponseContentType){
92114
res.contentType(setResponseContentType);
93115
}
94-
116+
95117
//Ability to send an empty response back
96118
if (process.env.ECHO_BACK_TO_CLIENT != undefined && process.env.ECHO_BACK_TO_CLIENT == "false"){
97119
res.end();
98-
}
120+
}
99121
//Ability to send just the request body in the response, nothing else
100122
else if ("response_body_only" in req.query && req.query["response_body_only"] == "true") {
101123
res.send(req.body);
102-
}
124+
}
103125
//Normal behavior, send everything back
104126
else {
105127
res.json(echo);
106128
}
107129

108130
//Certain paths can be ignored in the container logs, useful to reduce noise from healthchecks
109131
if (process.env.LOG_IGNORE_PATH != req.path) {
110-
132+
111133
let spacer = 4;
112134
if(process.env.LOG_WITHOUT_NEWLINE){
113135
spacer = null;
114136
}
115-
137+
116138
console.log(JSON.stringify(echo, null, spacer));
117139
}
118140
});
119141

120-
142+
121143
});
122144

123145
let sslOpts = {
@@ -127,10 +149,10 @@ let sslOpts = {
127149

128150
//Whether to enable the client certificate feature
129151
if(process.env.MTLS_ENABLE){
130-
sslOpts = {
131-
requestCert: true,
132-
rejectUnauthorized: false,
133-
...sslOpts
152+
sslOpts = {
153+
requestCert: true,
154+
rejectUnauthorized: false,
155+
...sslOpts
134156
}
135157
}
136158

‎package.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"concat-stream": "^2.0.0",
2121
"express": "^4.17.1",
2222
"jsonwebtoken": "^9.0.0",
23-
"morgan": "^1.10.0"
23+
"morgan": "^1.10.0",
24+
"express-prom-bundle": "^6.6.0"
2425
}
2526
}

‎tests.sh‎

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,27 +95,27 @@ fi
9595

9696

9797
REQUEST_WITH_SLEEP_MS=$(curl -o /dev/null -Ss -H "x-set-response-delay-ms: 6000" -k https://localhost:8443/ -w '%{time_total}')
98-
if [[ $(echo "$REQUEST_WITH_SLEEP_MS>5" | bc -l) == 1 ]]; then
98+
if [[ $(echo "$REQUEST_WITH_SLEEP_MS>5" | bc -l) == 1 ]]; then
9999
passed "Request header with response delay passed"
100-
else
100+
else
101101
failed "Request header with response delay failed"
102102
echo $REQUEST_WITH_SLEEP_MS
103103
exit 1
104104
fi
105105

106106
REQUEST_WITH_SLEEP_MS=$(curl -o /dev/null -Ss -k https://localhost:8443/sleep/test?x-set-response-delay-ms=5000 -w '%{time_total}')
107-
if [[ $(echo "$REQUEST_WITH_SLEEP_MS>4" | bc -l) == 1 ]]; then
107+
if [[ $(echo "$REQUEST_WITH_SLEEP_MS>4" | bc -l) == 1 ]]; then
108108
passed "Request query with response delay passed"
109-
else
109+
else
110110
failed "Request query with response delay failed"
111111
echo $REQUEST_WITH_SLEEP_MS
112112
exit 1
113113
fi
114114

115115
REQUEST_WITH_INVALID_SLEEP_MS=$(curl -o /dev/null -Ss -H "x-set-response-delay-ms: XXXX" -k https://localhost:8443/ -w '%{time_total}')
116-
if [[ $(echo "$REQUEST_WITH_INVALID_SLEEP_MS<2" | bc -l) == 1 ]]; then
116+
if [[ $(echo "$REQUEST_WITH_INVALID_SLEEP_MS<2" | bc -l) == 1 ]]; then
117117
passed "Request with invalid response delay passed"
118-
else
118+
else
119119
failed "Request with invalid response delay failed"
120120
echo $REQUEST_WITH_INVALID_SLEEP_MS
121121
exit 1
@@ -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,6 +443,46 @@ message " Stop containers "
442443
docker stop http-echo-tests
443444
sleep 5
444445

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
448+
sleep 5
449+
curl -s -k -X POST -d "tiramisu" https://localhost:8443/ > /dev/null
450+
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"* ]]
456+
then
457+
failed "PROMETHEUS metrics are enabled"
458+
exit 1
459+
else
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"
479+
exit 1
480+
fi
481+
482+
message " Stop containers "
483+
docker stop http-echo-tests
484+
sleep 5
485+
445486
popd
446487
rm -rf testarea
447488
message "DONE"

0 commit comments

Comments
 (0)