| = Métricas con MicroProfile `@Gauge` (Medida) |
| :index-group: MicroProfile |
| :jbake-type: page |
| :jbake-status: published |
| |
| Este es un ejemplo sobre cómo utilizar las métricas de MicroProfile en TomEE. |
| |
| == Ejecute la aplicación: |
| |
| [source,bash] |
| ---- |
| $ mvn clean install tomee:run |
| ---- |
| |
| Dentro de la aplicación, hay un endpoint que te dará la temperatura en celsios |
| para el día. |
| |
| == Obtenga la temperatura para el día: |
| |
| [source,bash] |
| ---- |
| $ curl -X GET http://localhost:8080/mp-metrics-gauge/weather/day/temperature |
| ---- |
| |
| == Response: |
| 30 |
| |
| == Usando `@Gauge` |
| |
| Las métricas de MicroProfile tienen una función para medir. El valor y tipo |
| de la medida son iguales al valor y tipo del método anotado. |
| |
| Para utilizar esta función, debe anotar los métodos de los recursos JAX-RS con |
| `@Gauge`. |
| |
| [source,java] |
| ---- |
| @Path("/weather") |
| @Produces(MediaType.APPLICATION_JSON) |
| @Consumes(MediaType.APPLICATION_JSON) |
| @ApplicationScoped |
| public class WeatherService { |
| |
| @Path("/day/temperature") |
| @Gauge(name = "weather_day_temperature", absolute = true, unit = "celsius", |
| displayName = "Weather Day Temperature", |
| description = "This metric shows the day temperature.", |
| tags = {"weather=temperature"}) |
| @GET |
| @Produces(MediaType.TEXT_PLAIN) |
| public Integer dayTemperature() { |
| return 30; |
| } |
| } |
| ---- |
| |
| Hay algunas configuraciones, como parte de `@Gauge`, que necesita saber: |
| |
| *String name* |
| Opcional. Establece el nombre de la métrica. Si no se proporciona |
| explícitamente, se utiliza el nombre del objeto anotado. |
| |
| *boolean absolute* |
| Si es verdadero, usa el nombre dado como el nombre absoluto de la métrica. Si |
| es falso, antepone el nombre del paquete y el nombre de la clase antes del |
| nombre dado. El valor predeterminado es falso. |
| |
| *String displayName* |
| Opcional. Un nombre para mostrar legible para los metadatos. |
| |
| *String description* |
| Opcional. Una descripción de la métrica. |
| |
| *String[] tags* |
| Opcional. Matriz de cadenas en el formato = para suministrar etiquetas |
| especiales a una métrica. |
| |
| *String unit* |
| Unidad de la métrica. Mira la clase MetricUnits para una lista de unidades |
| predeterminadas. |
| |
| == Datos de la Métrica |
| |
| Verifique la medida haciendo una solicitud _GET_: |
| |
| === Formato Prometheus: |
| |
| [source,bash] |
| ---- |
| $ curl -X GET http://localhost:8080/mp-metrics-gauge/metrics/application/weather_day_temperature |
| ---- |
| |
| === Respuesta Prometheus: |
| |
| [source] |
| ---- |
| # TYPE application:weather_day_temperature_celsius gauge |
| application:weather_day_temperature_celsius{weather="temperature"} 30.0 |
| ---- |
| |
| === Formato JSON: |
| |
| For json format add the header _Accept=application/json_ to the request. |
| |
| === Respuesta JSON |
| |
| [source,javascript] |
| ---- |
| { |
| "weather_day_temperature": 30 |
| } |
| ---- |
| |
| == Metadatos Métrica |
| |
| Una métrica tendrá metadatos para que pueda conocer más información al |
| respecto, como `displayName`,`description`, `tags`, etc. |
| |
| Verifique los metadatos de la métrica haciendo una solicitud HTTP _OPTIONS_: |
| |
| === Solicitud HTTP _OPTIONS_ |
| |
| [source,bash] |
| ---- |
| $ curl -X OPTIONS http://localhost:8080/mp-metrics-gauge/metrics/application/weather_day_temperature |
| ---- |
| |
| === Respuesta: |
| |
| [source,javascript] |
| ---- |
| { |
| "weather_day_temperature": { |
| "unit": "celsius", |
| "displayName": "Weather Day Temperature", |
| "name": "weather_day_temperature", |
| "typeRaw": "GAUGE", |
| "description": "This metric shows the day temperature.", |
| "type": "gauge", |
| "value": { |
| "unit": "celsius", |
| "displayName": "Weather Day Temperature", |
| "name": "weather_day_temperature", |
| "tagsAsString": "weather=\"temperature\"", |
| "typeRaw": "GAUGE", |
| "description": "This metric shows the day temperature.", |
| "type": "gauge", |
| "reusable": false, |
| "tags": { |
| "weather": "temperature" |
| } |
| }, |
| "reusable": false, |
| "tags": "weather=temperature" |
| } |
| } |
| ---- |
| |
| También puede probarlo utilizando WeatherServiceTest.java disponible en el |
| proyecto. |