|
| 1 | +# Metrics middleware using Swift Metrics and Swift Prometheus |
| 2 | + |
| 3 | +In this example we'll implement a `ClientMiddleware` and `ServerMiddleware` |
| 4 | +that use `swift-metrics` and `swift-prometheus` to collect and emit metrics |
| 5 | +respectively. |
| 6 | + |
| 7 | +## Overview |
| 8 | + |
| 9 | +This example extends the [HelloWorldVaporServer](../HelloWorldVaporServer) |
| 10 | +with a new target, `MetricsMiddleware`, which is then used when creating |
| 11 | +the `Server`. |
| 12 | + |
| 13 | +The metrics can now be accessed my making a HTTP GET request to `/metrics`. |
| 14 | + |
| 15 | +## Testing |
| 16 | + |
| 17 | +### Running the server and querying the metrics endpoint |
| 18 | + |
| 19 | +First, in one terminal, start the server. |
| 20 | + |
| 21 | +```console |
| 22 | +% swift run |
| 23 | +``` |
| 24 | + |
| 25 | +Then, in another terminal, make some requests: |
| 26 | + |
| 27 | +```console |
| 28 | +% xargs -n1 -I% curl "localhost:8080/api/greet?name=%" <<< "Juan Mei Tom Bill Anne Ravi Maria" |
| 29 | +{ |
| 30 | + "message" : "Hello, Juan!" |
| 31 | +} |
| 32 | +{ |
| 33 | + "message" : "Hello, Mei!" |
| 34 | +} |
| 35 | +{ |
| 36 | + "message" : "Hello, Tom!" |
| 37 | +} |
| 38 | +{ |
| 39 | + "message" : "Hello, Bill!" |
| 40 | +} |
| 41 | +{ |
| 42 | + "message" : "Hello, Anne!" |
| 43 | +} |
| 44 | +{ |
| 45 | + "message" : "Hello, Ravi!" |
| 46 | +} |
| 47 | +{ |
| 48 | + "message" : "Hello, Maria!" |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +Now you can query the `/metrics` endpoint of the server: |
| 53 | + |
| 54 | +```console |
| 55 | +% curl "localhost:8080/metrics" |
| 56 | +# TYPE http_requests_total counter |
| 57 | +http_requests_total{method="GET",path="/metrics",status="200"} 1 |
| 58 | +http_requests_total{method="GET",path="//api/greet",status="200"} 7 |
| 59 | +# TYPE http_request_duration_seconds histogram |
| 60 | +http_request_duration_seconds_bucket{method="GET",path="//api/greet",status="200",le="0.005"} 5 |
| 61 | +http_request_duration_seconds_bucket{method="GET",path="//api/greet",status="200",le="0.01"} 6 |
| 62 | +http_request_duration_seconds_bucket{method="GET",path="//api/greet",status="200",le="0.025"} 7 |
| 63 | +http_request_duration_seconds_bucket{method="GET",path="//api/greet",status="200",le="0.05"} 7 |
| 64 | +http_request_duration_seconds_bucket{method="GET",path="//api/greet",status="200",le="0.1"} 7 |
| 65 | +http_request_duration_seconds_bucket{method="GET",path="//api/greet",status="200",le="0.25"} 7 |
| 66 | +http_request_duration_seconds_bucket{method="GET",path="//api/greet",status="200",le="0.5"} 7 |
| 67 | +http_request_duration_seconds_bucket{method="GET",path="//api/greet",status="200",le="1.0"} 7 |
| 68 | +http_request_duration_seconds_bucket{method="GET",path="//api/greet",status="200",le="2.5"} 7 |
| 69 | +http_request_duration_seconds_bucket{method="GET",path="//api/greet",status="200",le="5.0"} 7 |
| 70 | +http_request_duration_seconds_bucket{method="GET",path="//api/greet",status="200",le="10.0"} 7 |
| 71 | +http_request_duration_seconds_bucket{method="GET",path="//api/greet",status="200",le="+Inf"} 7 |
| 72 | +http_request_duration_seconds_sum{method="GET",path="//api/greet",status="200"} 0.025902709 |
| 73 | +http_request_duration_seconds_count{method="GET",path="//api/greet",status="200"} 7 |
| 74 | +http_request_duration_seconds_bucket{method="GET",path="/metrics",status="200",le="0.005"} 1 |
| 75 | +http_request_duration_seconds_bucket{method="GET",path="/metrics",status="200",le="0.01"} 1 |
| 76 | +http_request_duration_seconds_bucket{method="GET",path="/metrics",status="200",le="0.025"} 1 |
| 77 | +http_request_duration_seconds_bucket{method="GET",path="/metrics",status="200",le="0.05"} 1 |
| 78 | +http_request_duration_seconds_bucket{method="GET",path="/metrics",status="200",le="0.1"} 1 |
| 79 | +http_request_duration_seconds_bucket{method="GET",path="/metrics",status="200",le="0.25"} 1 |
| 80 | +http_request_duration_seconds_bucket{method="GET",path="/metrics",status="200",le="0.5"} 1 |
| 81 | +http_request_duration_seconds_bucket{method="GET",path="/metrics",status="200",le="1.0"} 1 |
| 82 | +http_request_duration_seconds_bucket{method="GET",path="/metrics",status="200",le="2.5"} 1 |
| 83 | +http_request_duration_seconds_bucket{method="GET",path="/metrics",status="200",le="5.0"} 1 |
| 84 | +http_request_duration_seconds_bucket{method="GET",path="/metrics",status="200",le="10.0"} 1 |
| 85 | +http_request_duration_seconds_bucket{method="GET",path="/metrics",status="200",le="+Inf"} 1 |
| 86 | +http_request_duration_seconds_sum{method="GET",path="/metrics",status="200"} 0.001705458 |
| 87 | +http_request_duration_seconds_count{method="GET",path="/metrics",status="200"} 1 |
| 88 | +# TYPE HelloWorldServer.getGreeting.200 counter |
| 89 | +HelloWorldServer.getGreeting.200 7 |
| 90 | +``` |
| 91 | + |
| 92 | +The response contains the Prometheus metrics. You should see `http_requests_total{status="200", path="/api/greet", method="GET"} 7` for the seven requests made in the previous step. |
| 93 | + |
| 94 | +### Visualizing the metrics with Prometheus |
| 95 | + |
| 96 | +We'll use [Compose](https://docs.docker.com/compose) to run a set of containers |
| 97 | +to collect and visualize the metrics. |
| 98 | + |
| 99 | +The Compose file defines two services: `api` and `prometheus`. The `api` |
| 100 | +service uses an image built using the `Dockerfile` in the current directory. |
| 101 | +The `prometheus` service uses a public Prometheus image. |
| 102 | + |
| 103 | +The `prometheus` service is configured using the `prometheus.yml` file in the |
| 104 | +current directory. This configures Prometheus to scrape the /metrics endpoint |
| 105 | +of the API server every 5 seconds. |
| 106 | + |
| 107 | +Build and run the Compose application. You should see logging in the console |
| 108 | +from the API server and Prometheus. |
| 109 | + |
| 110 | +> NOTE: You need to keep this terminal window open for the remaining steps. Pressing Ctrl-C will shut down the application. |
| 111 | +
|
| 112 | +```console |
| 113 | +% docker compose up |
| 114 | +[+] Building 12/122 |
| 115 | +... |
| 116 | +[+] Running 2/0 |
| 117 | + ⠿ Container metricsmiddleware-prometheus-1 Created 0.0s |
| 118 | + ⠿ Container metricsmiddleware-api-1 Created 0.0s |
| 119 | +... |
| 120 | +metricsmiddleware-api-1 | 2023-06-08T14:34:24+0000 notice codes.vapor.application : [Vapor] Server starting on http://0.0.0.0:8080 |
| 121 | +... |
| 122 | +metricsmiddleware-prometheus-1 | ts=2023-06-08T14:34:24.914Z caller=web.go:562 level=info component=web msg="Start listening for connections" address=0.0.0.0:9090 |
| 123 | +... |
| 124 | +``` |
| 125 | + |
| 126 | +At this point you can make requests to the server, as before: |
| 127 | + |
| 128 | +```console |
| 129 | +% % echo "Juan Mei Tom Bill Anne Ravi Maria" | xargs -n1 -I% curl "localhost:8080/api/greet?name=%" |
| 130 | +{ |
| 131 | + "message" : "Hello, Juan!" |
| 132 | +} |
| 133 | +{ |
| 134 | + "message" : "Hello, Mei!" |
| 135 | +} |
| 136 | +{ |
| 137 | + "message" : "Hello, Tom!" |
| 138 | +} |
| 139 | +{ |
| 140 | + "message" : "Hello, Bill!" |
| 141 | +} |
| 142 | +{ |
| 143 | + "message" : "Hello, Anne!" |
| 144 | +} |
| 145 | +{ |
| 146 | + "message" : "Hello, Ravi!" |
| 147 | +} |
| 148 | +{ |
| 149 | + "message" : "Hello, Maria!" |
| 150 | +} |
| 151 | +``` |
| 152 | + |
| 153 | +Now open the Prometheus UI in your web browser by visiting [localhost:9090](http://localhost:9090). Click the graph tab and update the query to `http_requests_total`, or use [this pre-canned link](http://localhost:9090/graph?g0.expr=http_requests_total&g0.tab=0&g0.stacked=0&g0.show_exemplars=0&g0.range_input=5m). |
| 154 | + |
| 155 | +You should see the graph showing the seven recent requests. |
0 commit comments