/
CoffeesResource.java
executable file
·168 lines (141 loc) · 6.81 KB
/
CoffeesResource.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package com.sebastian_daschner.hello_prometheus;
import org.eclipse.microprofile.metrics.*;
import org.eclipse.microprofile.metrics.annotation.Metered;
import org.eclipse.microprofile.metrics.annotation.Timed;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
@Path("coffee")
public class CoffeesResource {
@Inject
MetricRegistry registry;
@Inject
Coffees coffees;
@GET
public String getCoffee() {
return coffees.retrieveCoffee();
}
// Coffee class needs to be @ApplicationScoped
@GET
@Path("/price")
public Response getItemsPrice() {
return Response.ok(coffees.getPrice()).build();
}
// examples from https://www.tomitribe.com/blog/getting-started-with-microprofile-metrics/
/*
curl http://localhost:8080/hello-prometheus/resources/coffee/sold
curl http://127.0.0.1:9990/metrics/application/itemsSold -H "Accept: application/json"
curl http://127.0.0.1:9990/metrics/application/itemsSold
Output / values diff
curl http://localhost:9080/hello-prometheus/resources/coffee/sold
curl https://localhost:9443/metrics/application/itemsSold -H "Accept: application/json" --insecure -u theUser:thePassword 2>/dev/null | jq
curl https://localhost:9443/metrics/application/itemsSold --insecure -u theUser:thePassword 2>/dev/null | grep -v TYPE | grep -v HELP
OpenLiberty is fine
*/
@Metered(name = "itemsSold",
unit = MetricUnits.MINUTES,
description = "Metrics to monitor sold method - @Metered.",
absolute = true)
@GET
@Path("/sold")
public Response itemSold() {
return Response.ok().build();
}
/*
curl http://localhost:8080/hello-prometheus/resources/coffee/process
curl http://127.0.0.1:9990/metrics/application/itemsProcessed -H "Accept: application/json"
curl http://127.0.0.1:9990/metrics/application/itemsProcessed
Output / values diff
curl http://localhost:9080/hello-prometheus/resources/coffee/process
curl http://localhost:9080/hello-prometheus/resources/coffee/process
curl https://localhost:9443/metrics/application/itemsProcessed -H "Accept: application/json" --insecure -u theUser:thePassword 2>/dev/null | jq
curl https://localhost:9443/metrics/application/itemsProcessed --insecure -u theUser:thePassword 2>/dev/null | grep -v TYPE | grep -v HELP
OpenLiberty is fine
*/
@Timed(name = "itemsProcessed",
description = "Metrics to monitor the times of processItem method. - @Timed(",
unit = MetricUnits.MINUTES,
absolute = true)
@GET
@Path("/process")
public Response processItem() {
return Response.ok().build();
}
/*
curl http://localhost:8080/hello-prometheus/resources/coffee/add/5
curl http://127.0.0.1:9990/metrics/application/itemsAdded -H "Accept: application/json"
curl http://127.0.0.1:9990/metrics/application/itemsAdded
curl http://localhost:8080/hello-prometheus/resources/coffee/add/5
Caused by: java.lang.IllegalArgumentException: Previously registered metric itemsAdded was not flagged as reusable
curl http://localhost:9080/hello-prometheus/resources/coffee/add/5
curl https://localhost:9443/metrics/application/itemsAdded -H "Accept: application/json" --insecure -u theUser:thePassword 2>/dev/null | jq
curl https://localhost:9443/metrics/application/itemsAdded --insecure -u theUser:thePassword 2>/dev/null | grep -v TYPE | grep -v HELP
curl http://localhost:9080/hello-prometheus/resources/coffee/add/5
curl http://localhost:9080/hello-prometheus/resources/coffee/add/2
curl http://localhost:9080/hello-prometheus/resources/coffee/add/1
OpenLiberty is fine
*/
@GET
@Path("/add/{numberOfItems}")
public Response addItems(@PathParam("numberOfItems") String numberOfItems) {
Metadata metadata = new Metadata("itemsAdded", MetricType.HISTOGRAM);
metadata.setReusable(true);
Histogram histogram = registry.histogram(metadata);
histogram.update(Long.valueOf(numberOfItems));
return Response.ok().build();
}
// examples from https://www.ibm.com/support/knowledgecenter/en/SSEQTP_liberty/com.ibm.websphere.wlp.doc/ae/cwlp_mp_metrics_api.html
Metadata statsHitsCounterMetadata = new Metadata(
"statsHits",
"Stats Hits",
"Number of hits on the /stats endpoint",
MetricType.COUNTER,
MetricUnits.NONE);
/*
curl http://localhost:8080/hello-prometheus/resources/coffee/stats
curl http://localhost:8080/hello-prometheus/resources/coffee/stats
Caused by: java.lang.IllegalArgumentException: Previously registered metric statsHits was not flagged as reusable
curl http://localhost:9080/hello-prometheus/resources/coffee/stats
curl http://localhost:9080/hello-prometheus/resources/coffee/stats
curl http://localhost:9080/hello-prometheus/resources/coffee/stats
curl -k -u theUser:thePassword https://localhost:9443/metrics/application/statsHits
curl -k -u theUser:thePassword -H "Accept: application/json" https://localhost:9443/metrics/application/statsHits
OpenLiberty is fine
*/
@GET
@Path("/stats")
public String getTotalDonations() {
// https://github.com/smallrye/smallrye-metrics/issues/43#issuecomment-446498898
statsHitsCounterMetadata.setReusable(true);
registry.counter(statsHitsCounterMetadata).inc();
return "12345";
}
Gauge<Double> progressGauge = new Gauge<Double>() {
public Double getValue() {
return Math.random() * 100;
}
};
Metadata progressMetadata = new Metadata(
"progress",
"Donation Progress",
"The percentage of the goal achieved so far",
MetricType.GAUGE,
MetricUnits.PERCENT);
/*
curl http://localhost:8080/hello-prometheus/resources/coffee/donation
curl http://localhost:8080/hello-prometheus/resources/coffee/donation
Caused by: java.lang.IllegalArgumentException: A metric with name progress already exists
curl http://127.0.0.1:9990/metrics/application/progress
curl http://localhost:9080/hello-prometheus/resources/coffee/donation
curl -k -u theUser:thePassword https://localhost:9443/metrics/application/progress
curl -k -u theUser:thePassword -H "Accept: application/json" https://localhost:9443/metrics/application/progress
*/
@GET
@Path("/donation")
public Response updateGauge() {
registry.register(progressMetadata, progressGauge);
return Response.ok().build();
}
}