-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding support for histograms (#740)
* Adding support for histograms - Added a new HttpHistogramMeterFilterFactory class to contribute to the configuration of the 'http.server.requests' and 'http.client.requests' meters.
- Loading branch information
Showing
2 changed files
with
88 additions
and
3 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
...n/java/io/micronaut/configuration/metrics/binder/web/HttpHistogramMeterFilterFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters