Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for histograms #740

Merged
merged 3 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright 2017-2024 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.configuration.metrics.binder.web;

import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.config.MeterFilter;
import io.micrometer.core.instrument.distribution.DistributionStatisticConfig;
import io.micronaut.configuration.metrics.annotation.RequiresMetrics;
import io.micronaut.context.annotation.*;
import jakarta.inject.Singleton;

import static io.micronaut.configuration.metrics.micrometer.MeterRegistryFactory.MICRONAUT_METRICS_BINDERS;
import static io.micronaut.core.util.StringUtils.FALSE;

/**
* Optional filter for adding histograms to HTTP metrics.
*
* @author lcavadas
*/
@Factory
@RequiresMetrics
@Requires(property = WebMetricsPublisher.ENABLED, notEquals = FALSE)
@Primary
public class HttpHistogramMeterFilterFactory {

/**
* Configure new MeterFilter for http.server.requests metrics.
*
* @param histogram If a histogram should be published
* @return A MeterFilter
*/
@Bean
@Singleton
@Requires(property = MICRONAUT_METRICS_BINDERS + ".web.server.histogram")
MeterFilter addServerPercentileMeterFilter(@Value("${" + MICRONAUT_METRICS_BINDERS + ".web.server.histogram}") Boolean histogram) {
return getMeterFilter(histogram, WebMetricsPublisher.METRIC_HTTP_SERVER_REQUESTS);
}

/**
* Configure new MeterFilter for http.client.requests metrics.
*
* @param histogram If a histogram should be published
* @return A MeterFilter
*/
@Bean
@Singleton
@Requires(property = MICRONAUT_METRICS_BINDERS + ".web.client.histogram")
MeterFilter addClientPercentileMeterFilter(@Value("${" + MICRONAUT_METRICS_BINDERS + ".web.client.histogram}") Boolean histogram) {
return getMeterFilter(histogram, WebMetricsPublisher.METRIC_HTTP_CLIENT_REQUESTS);
}

private MeterFilter getMeterFilter(Boolean histogram, String metricNamePrefix) {
return new MeterFilter() {
@Override
public DistributionStatisticConfig configure(Meter.Id id, DistributionStatisticConfig config) {
if (id.getName().startsWith(metricNamePrefix)) {
return DistributionStatisticConfig.builder()
.percentilesHistogram(histogram)
.build()
.merge(config);
}
return config;
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
import io.micronaut.core.util.ArrayUtils;
import jakarta.inject.Singleton;

import static io.micronaut.configuration.metrics.binder.web.WebMetricsPublisher.METRIC_HTTP_CLIENT_REQUESTS;
import static io.micronaut.configuration.metrics.binder.web.WebMetricsPublisher.METRIC_HTTP_SERVER_REQUESTS;
import static io.micronaut.configuration.metrics.micrometer.MeterRegistryFactory.MICRONAUT_METRICS_BINDERS;
import static io.micronaut.core.util.StringUtils.FALSE;

Expand All @@ -39,43 +37,46 @@
@Factory
@RequiresMetrics
@Requires(property = WebMetricsPublisher.ENABLED, notEquals = FALSE)
public class HttpMeterFilterFactory {
public class HttpPercentilesMeterFilterFactory {
lcavadas marked this conversation as resolved.
Show resolved Hide resolved

/**
* Configure new MeterFilter for http.server.requests metrics.
*
* @param percentiles The percentiles
* @param histogram If a histogram should be published
* @return A MeterFilter
*/
@Bean
@Singleton
@Requires(property = MICRONAUT_METRICS_BINDERS + ".web.server.percentiles")
MeterFilter addServerPercentileMeterFilter(@Value("${" + MICRONAUT_METRICS_BINDERS + ".web.server.percentiles}") Double[] percentiles) {
return getMeterFilter(percentiles, METRIC_HTTP_SERVER_REQUESTS);
MeterFilter addServerPercentileMeterFilter(@Value("${" + MICRONAUT_METRICS_BINDERS + ".web.server.percentiles}") Double[] percentiles, @Value("${" + MICRONAUT_METRICS_BINDERS + ".web.server.histogram:false}") Boolean histogram) {
return getMeterFilter(percentiles, histogram, WebMetricsPublisher.METRIC_HTTP_SERVER_REQUESTS);
}

/**
* Configure new MeterFilter for http.client.requests metrics.
*
* @param percentiles The percentiles
* @param histogram If a histogram should be published
* @return A MeterFilter
*/
@Bean
@Singleton
@Requires(property = MICRONAUT_METRICS_BINDERS + ".web.client.percentiles")
MeterFilter addClientPercentileMeterFilter(@Value("${" + MICRONAUT_METRICS_BINDERS + ".web.client.percentiles}") Double[] percentiles) {
return getMeterFilter(percentiles, METRIC_HTTP_CLIENT_REQUESTS);
MeterFilter addClientPercentileMeterFilter(@Value("${" + MICRONAUT_METRICS_BINDERS + ".web.client.percentiles}") Double[] percentiles, @Value("${" + MICRONAUT_METRICS_BINDERS + ".web.client.histogram:false}") Boolean histogram) {
return getMeterFilter(percentiles, histogram, WebMetricsPublisher.METRIC_HTTP_CLIENT_REQUESTS);
}

private MeterFilter getMeterFilter(Double[] percentiles, String metricNamePrefix) {
private MeterFilter getMeterFilter(Double[] percentiles, Boolean histogram, String metricNamePrefix) {
return new MeterFilter() {
@Override
public DistributionStatisticConfig configure(Meter.Id id, DistributionStatisticConfig config) {
if (id.getName().startsWith(metricNamePrefix)) {
return DistributionStatisticConfig.builder()
.percentiles((double[]) ArrayUtils.toPrimitiveArray(percentiles))
.build()
.merge(config);
.percentiles((double[]) ArrayUtils.toPrimitiveArray(percentiles))
.percentilesHistogram(histogram)
.build()
.merge(config);
}
return config;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class HttpMetricsSpec extends Specification {
clientTimer.count() == 1
serverSnapshot.percentileValues().length == serverPercentilesCount
clientSnapshot.percentileValues().length == clientPercentilesCount
serverTimer.getMetaPropertyValues().find { it.name.equals('distributionStatisticConfig') }.value.percentileHistogram == serverHistogram
clientTimer.getMetaPropertyValues().find { it.name.equals('distributionStatisticConfig') }.value.percentileHistogram == clientHistogram

when: "A request is sent to the root route"

Expand Down Expand Up @@ -135,9 +137,13 @@ class HttpMetricsSpec extends Specification {
embeddedServer.close()

where:
cfg | setting | serverPercentilesCount | clientPercentilesCount
MICRONAUT_METRICS_BINDERS + ".web.client.percentiles" | "0.95,0.99" | 0 | 2
MICRONAUT_METRICS_BINDERS + ".web.server.percentiles" | "0.95,0.99" | 2 | 0
cfg | setting | serverPercentilesCount | clientPercentilesCount| serverHistogram | clientHistogram
MICRONAUT_METRICS_BINDERS + ".web.client.percentiles" | "0.95,0.99" | 0 | 2 | null | false
MICRONAUT_METRICS_BINDERS + ".web.server.percentiles" | "0.95,0.99" | 2 | 0 | false | null
MICRONAUT_METRICS_BINDERS + ".web.server.histogram" | "true" | 0 | 0 | true | null
MICRONAUT_METRICS_BINDERS + ".web.server.histogram" | "false" | 0 | 0 | false | null
MICRONAUT_METRICS_BINDERS + ".web.client.histogram" | "true" | 0 | 0 | null | true
MICRONAUT_METRICS_BINDERS + ".web.client.histogram" | "false" | 0 | 0 | null | false
}

void "test client / server metrics ignored uris for client errors"() {
Expand Down Expand Up @@ -303,7 +309,10 @@ class HttpMetricsSpec extends Specification {
String root() { "root" }

@Get('/test-http-metrics')
String index() { "ok" }
String index() {
Thread.sleep(1000)
"ok"
}

@Get("/test-http-metrics/{id}")
String template(String id) { "ok " + id }
Expand Down