Skip to content

Commit b22ad1b

Browse files
committed
Add README to the tomcat-servlet-example #850
1 parent 694b4f8 commit b22ad1b

File tree

3 files changed

+86
-3
lines changed

3 files changed

+86
-3
lines changed
+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
![Screenshot showing a Prometheus Native Histogram in Text Format](https://github.com/prometheus/client_java/assets/330535/863efe0b-a9eb-40ae-a078-72497abc6f04)
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+
![Screenshot showing the 95th Percentile Calculated from a Prometheus Native Histogram](https://github.com/prometheus/client_java/assets/330535/889fb769-9445-4f6f-8540-2b1ddffca55e)

examples/tomcat-servlet-example/src/main/java/io/prometheus/metrics/examples/tomcat_servlet/HelloWorldServlet.java

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212

1313
import static io.prometheus.metrics.model.snapshots.Unit.nanosToSeconds;
1414

15+
/**
16+
* Hello World REST servlet, with an example counter and an example histogram.
17+
*/
1518
public class HelloWorldServlet extends HttpServlet {
1619

1720
private final Random random = new Random(0);

examples/tomcat-servlet-example/src/main/java/io/prometheus/metrics/examples/tomcat_servlet/Main.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
package io.prometheus.metrics.examples.tomcat_servlet;
22

3-
import io.prometheus.metrics.config.PrometheusProperties;
43
import io.prometheus.metrics.exporter.servlet.jakarta.PrometheusMetricsServlet;
5-
import io.prometheus.metrics.model.registry.PrometheusRegistry;
64
import org.apache.catalina.Context;
75
import org.apache.catalina.LifecycleException;
86
import org.apache.catalina.startup.Tomcat;
97

108
import java.io.File;
119

1210
/**
13-
* Simple example using embedded Tomcat an the {@link PrometheusMetricsServlet}.
11+
* Simple example using embedded Tomcat and the {@link PrometheusMetricsServlet}.
1412
*/
1513
public class Main {
1614

0 commit comments

Comments
 (0)