= Métricas con MicroProfile `@Histogram`
: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á un histograma del clima de las más recientes temperaturas en la Ciudad de Nueva York.

== Request:

[source,bash]
----
$ curl -X GET http://localhost:8080/mp-metrics-histogram/weather/histogram
----

== Response:

[source,javascript]
----
{
    "count":15,
    "max":55,
    "mean":44.4,
    "min":27,
    "p50":45.0,
    "p75":46.0,
    "p95":54.0,
    "p98":54.0,
    "p99":54.0,
    "p999":54.0,
    "stddev":7.0710678118654755,
    "unit":"degrees F"
}
----

== Usando `@Histogram`

Las métricas de MicroProfile tienen una función que te permite crear un histograma datos.

Para utilizar esta función, injecta un objeto `MetricRegistry`, registra el Histograma, and agrega datos al histograma como se muestra a continuación.

[source,java]
----
@Inject
private MetricRegistry registry;

@Inject
@Metric(name = "temperatures", description = "A histogram metrics example.",
    displayName = "Histogram of Recent New York Temperatures")
private Histogram histogram;

@Path("/histogram")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Histogram getTemperatures() {
    Metadata metadata = new Metadata("temperatures", MetricType.HISTOGRAM, "degrees F");
    metadata.setDescription("A histogram of recent New York temperatures.");
    final int[] RECENT_NEW_YORK_TEMPS = { 46, 45, 50, 46, 45, 27, 30, 48, 55, 54, 45, 41, 45, 43, 46 };
    histogram = registry.histogram(metadata);
    for(int temp : RECENT_NEW_YORK_TEMPS) {
        histogram.update(temp);
    }
    return histogram;
}
----

Hay algunas configuraciones definidas en la anotación `@Histogram`:

*String name* Opcional. Establece el nombre de la métrica. Si no se proporciona
explícitamente, se utiliza el nombre del objeto anotado.

*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.

*boolean reusable* Indica si una métrica con un nombre determinado se puede
registrar en más de un lugar. No se aplica a `@Histogram`.

=== GET /histogram/status:

[source,bash]
----
$ curl -X GET http://localhost:8080/mp-metrics-histogram/weather/histogram/status
----

=== Respuesta:

[source,bash]
----
 Here are the most recent New York City temperatures.
----

=== Formato Prometheus:

[source,text]
----
    # TYPE application:temperatures_degrees F summary histogram
    # TYPE application:temperatures_degrees F_count histogram
    application:temperatures_degrees F_count 15.0
    # TYPE application:temperatures_min_degrees F histogram
    application:temperatures_min_degrees F 27.0
    # TYPE application:temperatures_max_degrees F histogram
    application:temperatures_max_degrees F 55.0
    # TYPE application:temperatures_mean_degrees F histogram
    application:temperatures_mean_degrees F 44.4
    # TYPE application:temperatures_stddev_degrees F histogram
    application:temperatures_stddev_degrees F 7.0710678118654755
    # TYPE application:temperatures_degrees F histogram
    application:temperatures_degrees F{quantile="0.5"} 45.0
    # TYPE application:temperatures_degrees F histogram
    application:temperatures_degrees F{quantile="0.75"} 46.0
    # TYPE application:temperatures_degrees F histogram
    application:temperatures_degrees F{quantile="0.95"} 54.0
    # TYPE application:temperatures_degrees F histogram
    application:temperatures_degrees F{quantile="0.98"} 54.0
    # TYPE application:temperatures_degrees F histogram
    application:temperatures_degrees F{quantile="0.99"} 54.0
    # TYPE application:temperatures_degrees F histogram
    application:temperatures_degrees F{quantile="0.999"} 54.0
    # TYPE application:org_superbiz_histogram_weather_service_temperatures summary histogram
    # TYPE application:org_superbiz_histogram_weather_service_temperatures_count histogram
    application:org_superbiz_histogram_weather_service_temperatures_count 0.0
    # TYPE application:org_superbiz_histogram_weather_service_temperatures_min histogram
    application:org_superbiz_histogram_weather_service_temperatures_min 0.0
    # TYPE application:org_superbiz_histogram_weather_service_temperatures_max histogram
    application:org_superbiz_histogram_weather_service_temperatures_max 0.0
    # TYPE application:org_superbiz_histogram_weather_service_temperatures_mean histogram
    application:org_superbiz_histogram_weather_service_temperatures_mean NaN
    # TYPE application:org_superbiz_histogram_weather_service_temperatures_stddev histogram
    application:org_superbiz_histogram_weather_service_temperatures_stddev 0.0
    # TYPE application:org_superbiz_histogram_weather_service_temperatures histogram
    application:org_superbiz_histogram_weather_service_temperatures{quantile="0.5"} 0.0
    # TYPE application:org_superbiz_histogram_weather_service_temperatures histogram
    application:org_superbiz_histogram_weather_service_temperatures{quantile="0.75"} 0.0
    # TYPE application:org_superbiz_histogram_weather_service_temperatures histogram
    application:org_superbiz_histogram_weather_service_temperatures{quantile="0.95"} 0.0
    # TYPE application:org_superbiz_histogram_weather_service_temperatures histogram
    application:org_superbiz_histogram_weather_service_temperatures{quantile="0.98"} 0.0
    # TYPE application:org_superbiz_histogram_weather_service_temperatures histogram
    application:org_superbiz_histogram_weather_service_temperatures{quantile="0.99"} 0.0
    # TYPE application:org_superbiz_histogram_weather_service_temperatures histogram
    application:org_superbiz_histogram_weather_service_temperatures{quantile="0.999"} 0.0
----

=== Formato JSON:

[source,bash]
----
$ curl -X GET http://localhost:8080/mp-metrics-histogram/metrics/application
----

=== Respuesta JSON:

[source,javascript]
----
{
    "org.superbiz.histogram.WeatherService.temperatures": {
        "count":0,
        "max":0,
        "min":0,
        "p50":0.0,
        "p75":0.0,
        "p95":0.0,
        "p98":0.0,
        "p99":0.0,
        "p999":0.0,
        "stddev":0.0,
        "unit":"none"
    }
}
----

== Metadatos de la Métrica:

Una métrica tendrá metadatos para que pueda conocer más información al
respecto, como `displayName`,`description`, `tags`, etc.

=== Solicitud HTTP _OPTIONS_:

[source,bash]
----
$ curl -X OPTIONS http://localhost:8080/mp-metrics-histogram/metrics/application
----

=== Respuesta:

[source,javascript]
----
{
    "org.superbiz.histogram.WeatherService.temperatures": {
        "description": "A histogram metrics example.",
        "displayName":"Histogram of Recent New York Temperatures",
        "name":"org.superbiz.histogram.WeatherService.temperatures",
        "reusable":false,
        "tags":"",
        "type":"histogram",
        "typeRaw":"HISTOGRAM",
        "unit":"none"
    }
}
----

=== Prueba la aplicación

[source,bash]
----
$ mvn test
----
