$ mvn clean install tomee:run
MicroProfile Metrics Metered
This is an example on how to use microprofile metrics in TomEE.
Run the application:
Within the application, there is an enpoint that will give you a weather status for the day and week.
For the day status call:
$ curl -X GET http://localhost:8080/mp-metrics-metered/weather/day/status
Response:
Hi, today is a sunny day!
Metered Feature
MicroProfile metrics has a feature that can be used to find the rate of requests to a service.
To use this feature you need to annotate the JAX-RS resource method with
@Metered
.
@Path("/weather")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@ApplicationScoped
public class WeatherService {
@Path("/day/status")
@Metered(name = "dailyStatus",
unit = MetricUnits.MINUTES,
description = "Metrics to daily weather status method",
absolute = true)
@GET
@Produces(MediaType.TEXT_PLAIN)
public String dayStatus() {
return "Hi, today is a sunny day!";
}
...
}
There are some configurations, as part of @Metered
, that you need to know:
String name Optional. Sets the name of the metric. If not explicitly given the name of the annotated object is used.
boolean absolute If true, uses the given name as the absolute name of the metric. If false, prepends the package name and class name before the given name. Default value is false.
String displayName Optional. A human-readable display name for metadata.
String description Optional. A description of the metric.
String[] tags Optional. Array of Strings in the <key>=<value> format to supply special tags to a metric.
boolean reusable Denotes if a metric with a certain name can be registered in more than one place. Does not apply to gauges.
String unit
Unit of the metric. Default for @Metered
is nanoseconds.
Metric data
Check the Metered metric doing a GET request:
Prometheus format:
$ curl -X GET http://localhost:8080/mp-metrics-metered/metrics/application/dailyStatus
Response:
# TYPE application:daily_status_seconds_count meter
application:daily_status_seconds_count 1.2E-7
# TYPE application:daily_status_rate_per_second meter
application:daily_status_rate_per_second 0.0
# TYPE application:daily_status_one_min_rate_per_second meter
application:daily_status_one_min_rate_per_second 1.3376002644204984E-19
# TYPE application:daily_status_five_min_rate_per_second meter
application:daily_status_five_min_rate_per_second 3.5942838529305413E-20
# TYPE application:daily_status_fifteen_min_rate_per_second meter
application:daily_status_fifteen_min_rate_per_second 3.4665766454142955E-21
JSON Format:
For json format add the header Accept: application/json
to the request.
{
"dailyStatus": {
"count": 2,
"fifteenMinRate": 5.77762774235716e-14,
"fiveMinRate": 5.990473088217569e-13,
"meanRate": 0,
"oneMinRate": 2.229333774034164e-12,
"unit": "minutes"
}
}
Metric metadata
A metric will have a metadata so you can know more information about it, like displayName
, description
, tags
, etc.
Check the metric metadata doing a OPTIONS request:
Request
$ curl -X OPTIONS http://localhost:8080/mp-metrics-metered/metrics/application/dailyStatus
Response:
{
"dailyStatus": {
"description": "Metrics to daily weather status method",
"displayName": "",
"name": "dailyStatus",
"reusable": false,
"tags": "",
"type": "meter",
"typeRaw": "METERED",
"unit": "minutes"
}
}
Test the application:
$ mvn test