|
| 1 | +# Tomcat Servlet Example |
| 2 | + |
| 3 | +## Build |
| 4 | + |
| 5 | +This example is built as part of the `client_java` project. |
| 6 | + |
| 7 | +``` |
| 8 | +./mvnw package |
| 9 | +``` |
| 10 | + |
| 11 | +## Run |
| 12 | + |
| 13 | +The build creates a JAR file with the example application in `./examples/tomcat-servlet-example/target/`. |
| 14 | + |
| 15 | +``` |
| 16 | +java -jar ./examples/tomcat-servlet-example/target/tomcat-servlet-example-1.0.0-alpha-2-SNAPSHOT.jar |
| 17 | +``` |
| 18 | + |
| 19 | +Accessing [http://localhost:8080/](http://localhost:8080/) with a Web browser should yield `Hello, World!`. |
| 20 | + |
| 21 | +## Manually testing the Metrics Endpoint |
| 22 | + |
| 23 | +Metrics are available on [http://localhost:8080/metrics](http://localhost:8080/metrics). The default Prometheus text format looks like this: |
| 24 | + |
| 25 | +``` |
| 26 | +# HELP request_duration_seconds request duration in seconds |
| 27 | +# TYPE request_duration_seconds histogram |
| 28 | +request_duration_seconds_bucket{http_status="200",le="0.005"} 0 |
| 29 | +request_duration_seconds_bucket{http_status="200",le="0.01"} 1 |
| 30 | +request_duration_seconds_bucket{http_status="200",le="0.025"} 1 |
| 31 | +request_duration_seconds_bucket{http_status="200",le="0.05"} 2 |
| 32 | +request_duration_seconds_bucket{http_status="200",le="0.1"} 5 |
| 33 | +request_duration_seconds_bucket{http_status="200",le="0.25"} 11 |
| 34 | +request_duration_seconds_bucket{http_status="200",le="0.5"} 12 |
| 35 | +request_duration_seconds_bucket{http_status="200",le="1.0"} 12 |
| 36 | +request_duration_seconds_bucket{http_status="200",le="2.5"} 12 |
| 37 | +request_duration_seconds_bucket{http_status="200",le="5.0"} 12 |
| 38 | +request_duration_seconds_bucket{http_status="200",le="10.0"} 12 |
| 39 | +request_duration_seconds_bucket{http_status="200",le="+Inf"} 12 |
| 40 | +request_duration_seconds_count{http_status="200"} 12 |
| 41 | +request_duration_seconds_sum{http_status="200"} 1.513772412 |
| 42 | +# HELP requests_total total number of requests |
| 43 | +# TYPE requests_total counter |
| 44 | +requests_total{http_status="200"} 12.0 |
| 45 | +``` |
| 46 | + |
| 47 | +The exporter servlet supports a `debug` URL parameter to quickly view other formats in your Web browser: |
| 48 | + |
| 49 | +* [http://localhost:8080/metrics?debug=text](http://localhost:8080/metrics?debug=text): Prometheus text format, same as without the `debug` option. |
| 50 | +* [http://localhost:8080/metrics?debug=openmetrics](http://localhost:8080/metrics?debug=openmetrics): OpenMetrics text format. |
| 51 | +* [http://localhost:8080/metrics?debug=prometheus-protobuf](http://localhost:8080/metrics?debug=prometheus-protobuf): Text representation of the Prometheus protobuf format. |
| 52 | + |
| 53 | +## Testing with the Prometheus Server |
| 54 | + |
| 55 | +1. Download the latest Prometheus server release from [https://github.com/prometheus/prometheus/releases](https://github.com/prometheus/prometheus/releases). |
| 56 | +2. Extract the archive |
| 57 | +3. Edit `prometheus.yml` and append the following snippet at the end: |
| 58 | + ```yaml |
| 59 | + job_name: "tomcat-servlet-example" |
| 60 | + static_configs: |
| 61 | + - targets: ["localhost:8080"] |
| 62 | + ``` |
| 63 | +4. Run with native histograms and examplars enabled: |
| 64 | + ```shell |
| 65 | + ./prometheus --enable-feature=native-histograms --enable-feature=exemplar-storage |
| 66 | + ``` |
| 67 | + |
| 68 | +Verify that the `tomcat-servlet-example` target is up on [http://localhost:9090/targets](http://localhost:9090/targets). |
| 69 | + |
| 70 | +Prometheus is now scraping metrics in Protobuf format. If you type the name `request_duration_seconds` you will see a non-human-readable representation of the histogram including the native buckets: |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | +Note: You have to run at least one GET request on the Hello World endpoint [http://localhost:8080](http://localhost:8080) before you see the metric. |
| 75 | + |
| 76 | +Use the `histogram_quantile()` function to calculate quantiles from the native histogram: |
| 77 | + |
| 78 | +``` |
| 79 | +histogram_quantile(0.95, rate(request_duration_seconds[10m])) |
| 80 | +``` |
| 81 | + |
| 82 | + |
0 commit comments