聊聊springboot的http.server.request
序
本文主要研究一下springboot的http.server.requests
http.server.requests
org/springframework/boot/actuate/autoconfigure/metrics/MetricsProperties.java
public static class Server {
private final ServerRequest request = new ServerRequest();
/**
* Maximum number of unique URI tag values allowed. After the max number of
* tag values is reached, metrics with additional tag values are denied by
* filter.
*/
private int maxUriTags = 100;
public ServerRequest getRequest() {
return this.request;
}
public int getMaxUriTags() {
return this.maxUriTags;
}
public void setMaxUriTags(int maxUriTags) {
this.maxUriTags = maxUriTags;
}
public static class ServerRequest {
/**
* Name of the metric for received requests.
*/
private String metricName = "http.server.requests";
/**
* Whether the trailing slash should be ignored when recording metrics.
*/
private boolean ignoreTrailingSlash = true;
//......
}
}
MetricsProperties.Server.ServerRequest定义了http.server.requests这个metrics名
WebMvcMetricsAutoConfiguration
org/springframework/boot/actuate/autoconfigure/metrics/web/servlet/WebMvcMetricsAutoConfiguration.java
@Configuration(proxyBeanMethods = false)
@AutoConfigureAfter({ MetricsAutoConfiguration.class, CompositeMeterRegistryAutoConfiguration.class,
SimpleMetricsExportAutoConfiguration.class })
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
@ConditionalOnClass(DispatcherServlet.class)
@ConditionalOnBean(MeterRegistry.class)
@EnableConfigurationProperties(MetricsProperties.class)
public class WebMvcMetricsAutoConfiguration {
private final MetricsProperties properties;
public WebMvcMetricsAutoConfiguration(MetricsProperties properties) {
this.properties = properties;
}
@Bean
@ConditionalOnMissingBean(WebMvcTagsProvider.class)
public DefaultWebMvcTagsProvider webMvcTagsProvider(ObjectProvider<WebMvcTagsContributor> contributors) {
return new DefaultWebMvcTagsProvider(this.properties.getWeb().getServer().getRequest().isIgnoreTrailingSlash(),
contributors.orderedStream().collect(Collectors.toList()));
}
@Bean
public FilterRegistrationBean<WebMvcMetricsFilter> webMvcMetricsFilter(MeterRegistry registry,
WebMvcTagsProvider tagsProvider) {
ServerRequest request = this.properties.getWeb().getServer().getRequest();
WebMvcMetricsFilter filter = new WebMvcMetricsFilter(registry, tagsProvider, request.getMetricName(),
request.getAutotime());
FilterRegistrationBean<WebMvcMetricsFilter> registration = new FilterRegistrationBean<>(filter);
registration.setOrder(Ordered.HIGHEST_PRECEDENCE + 1);
registration.setDispatcherTypes(DispatcherType.REQUEST, DispatcherType.ASYNC);
return registration;
}
@Bean
@Order(0)
public MeterFilter metricsHttpServerUriTagFilter() {
String metricName = this.properties.getWeb().getServer().getRequest().getMetricName();
MeterFilter filter = new OnlyOnceLoggingDenyMeterFilter(
() -> String.format("Reached the maximum number of URI tags for '%s'.", metricName));
return MeterFilter.maximumAllowableTags(metricName, "uri", this.properties.getWeb().getServer().getMaxUriTags(),
filter);
}
@Bean
public MetricsWebMvcConfigurer metricsWebMvcConfigurer(MeterRegistry meterRegistry,
WebMvcTagsProvider tagsProvider) {
return new MetricsWebMvcConfigurer(meterRegistry, tagsProvider);
}
}
WebMvcMetricsAutoConfiguration定义了webMvcTagsProvider,可以接收WebMvcTagsContributor对tag进行定制,还注册了WebMvcMetricsFilter、MeterFilter、metricsWebMvcConfigurer
DefaultWebMvcTagsProvider
org/springframework/boot/actuate/metrics/web/servlet/DefaultWebMvcTagsProvider.java
public class DefaultWebMvcTagsProvider implements WebMvcTagsProvider {
private final boolean ignoreTrailingSlash;
private final List<WebMvcTagsContributor> contributors;
public DefaultWebMvcTagsProvider() {
this(false);
}
/**
* Creates a new {@link DefaultWebMvcTagsProvider} that will provide tags from the
* given {@code contributors} in addition to its own.
* @param contributors the contributors that will provide additional tags
* @since 2.3.0
*/
public DefaultWebMvcTagsProvider(List<WebMvcTagsContributor> contributors) {
this(false, contributors);
}
public DefaultWebMvcTagsProvider(boolean ignoreTrailingSlash) {
this(ignoreTrailingSlash, Collections.emptyList());
}
/**
* Creates a new {@link DefaultWebMvcTagsProvider} that will provide tags from the
* given {@code contributors} in addition to its own.
* @param ignoreTrailingSlash whether trailing slashes should be ignored when
* determining the {@code uri} tag.
* @param contributors the contributors that will provide additional tags
* @since 2.3.0
*/
public DefaultWebMvcTagsProvider(boolean ignoreTrailingSlash, List<WebMvcTagsContributor> contributors) {
this.ignoreTrailingSlash = ignoreTrailingSlash;
this.contributors = contributors;
}
@Override
public Iterable<Tag> getTags(HttpServletRequest request, HttpServletResponse response, Object handler,
Throwable exception) {
Tags tags = Tags.of(WebMvcTags.method(request), WebMvcTags.uri(request, response, this.ignoreTrailingSlash),
WebMvcTags.exception(exception), WebMvcTags.status(response), WebMvcTags.outcome(response));
for (WebMvcTagsContributor contributor : this.contributors) {
tags = tags.and(contributor.getTags(request, response, handler, exception));
}
return tags;
}
@Override
public Iterable<Tag> getLongRequestTags(HttpServletRequest request, Object handler) {
Tags tags = Tags.of(WebMvcTags.method(request), WebMvcTags.uri(request, null, this.ignoreTrailingSlash));
for (WebMvcTagsContributor contributor : this.contributors) {
tags = tags.and(contributor.getLongRequestTags(request, handler));
}
return tags;
}
}
DefaultWebMvcTagsProvider实现了WebMvcTagsProvider接口,其构造器可以接收WebMvcTagsContributor,对tag进行定制;默认的tag为WebMvcTags.method(
method
)、WebMvcTags.uri(uri
)、WebMvcTags.exception(exception
)、WebMvcTags.status(status
)、WebMvcTags.outcome(outcome
)
WebMvcMetricsFilter
org/springframework/boot/actuate/metrics/web/servlet/WebMvcMetricsFilter.java
public class WebMvcMetricsFilter extends OncePerRequestFilter {
private final MeterRegistry registry;
private final WebMvcTagsProvider tagsProvider;
private final String metricName;
private final AutoTimer autoTimer;
/**
* Create a new {@link WebMvcMetricsFilter} instance.
* @param registry the meter registry
* @param tagsProvider the tags provider
* @param metricName the metric name
* @param autoTimer the auto-timers to apply or {@code null} to disable auto-timing
* @since 2.2.0
*/
public WebMvcMetricsFilter(MeterRegistry registry, WebMvcTagsProvider tagsProvider, String metricName,
AutoTimer autoTimer) {
this.registry = registry;
this.tagsProvider = tagsProvider;
this.metricName = metricName;
this.autoTimer = autoTimer;
}
@Override
protected boolean shouldNotFilterAsyncDispatch() {
return false;
}
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
TimingContext timingContext = TimingContext.get(request);
if (timingContext == null) {
timingContext = startAndAttachTimingContext(request);
}
try {
filterChain.doFilter(request, response);
if (!request.isAsyncStarted()) {
// Only record when async processing has finished or never been started.
// If async was started by something further down the chain we wait
// until the second filter invocation (but we'll be using the
// TimingContext that was attached to the first)
Throwable exception = (Throwable) request.getAttribute(DispatcherServlet.EXCEPTION_ATTRIBUTE);
record(timingContext, request, response, exception);
}
}
catch (NestedServletException ex) {
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
record(timingContext, request, response, ex.getCause());
throw ex;
}
catch (ServletException | IOException | RuntimeException ex) {
record(timingContext, request, response, ex);
throw ex;
}
}
//......
}
WebMvcMetricsFilter继承了OncePerRequestFilter,其doFilterInternal方法通过TimingContext来维护timerSample,然后在filterChain.doFilter(request, response)之后进行record
record
private void record(TimingContext timingContext, HttpServletRequest request, HttpServletResponse response,
Throwable exception) {
Object handler = getHandler(request);
Set<Timed> annotations = getTimedAnnotations(handler);
Timer.Sample timerSample = timingContext.getTimerSample();
if (annotations.isEmpty()) {
if (this.autoTimer.isEnabled()) {
Builder builder = this.autoTimer.builder(this.metricName);
timerSample.stop(getTimer(builder, handler, request, response, exception));
}
}
else {
for (Timed annotation : annotations) {
Builder builder = Timer.builder(annotation, this.metricName);
timerSample.stop(getTimer(builder, handler, request, response, exception));
}
}
}
private Timer getTimer(Builder builder, Object handler, HttpServletRequest request, HttpServletResponse response,
Throwable exception) {
return builder.tags(this.tagsProvider.getTags(request, response, handler, exception)).register(this.registry);
}
record方法主要执行timerSample.stop(getTimer(builder, handler, request, response, exception))
timerSample
micrometer-core-1.5.9.jar!/io/micrometer/core/instrument/Timer.class
public static class Sample {
private Tags tags = Tags.empty();
private final long startTime;
private final Clock clock;
Sample(Clock clock) {
this.clock = clock;
this.startTime = clock.monotonicTime();
}
public long stop(Timer timer) {
long durationNs = this.clock.monotonicTime() - this.startTime;
timer.record(durationNs, TimeUnit.NANOSECONDS);
return durationNs;
}
@Incubating(
since = "1.4.0"
)
public long stop(MeterRegistry registry, Builder timerBuilder) {
return this.stop(timerBuilder.tags((Iterable)this.tags).register(registry));
}
@Incubating(
since = "1.4.0"
)
public Sample tags(String... tags) {
return this.tags((Iterable)Tags.of(tags));
}
@Incubating(
since = "1.4.0"
)
public Sample tags(Iterable<Tag> tags) {
this.tags = this.tags.and(tags);
return this;
}
}
Timer.Sample的stop方法主要执行timer.record(durationNs, TimeUnit.NANOSECONDS)
PrometheusTimer
io/micrometer/prometheus/PrometheusTimer.java
public class PrometheusTimer extends AbstractTimer {
private static final CountAtBucket[] EMPTY_HISTOGRAM = new CountAtBucket[0];
private final LongAdder count = new LongAdder();
private final LongAdder totalTime = new LongAdder();
private final TimeWindowMax max;
private final HistogramFlavor histogramFlavor;
@Nullable
private final Histogram histogram;
PrometheusTimer(Id id, Clock clock, DistributionStatisticConfig distributionStatisticConfig, PauseDetector pauseDetector, HistogramFlavor histogramFlavor) {
super(id, clock,
DistributionStatisticConfig.builder()
.percentilesHistogram(false)
.serviceLevelObjectives()
.build()
.merge(distributionStatisticConfig),
pauseDetector, TimeUnit.SECONDS, false);
this.histogramFlavor = histogramFlavor;
this.max = new TimeWindowMax(clock, distributionStatisticConfig);
if (distributionStatisticConfig.isPublishingHistogram()) {
switch (histogramFlavor) {
case Prometheus:
histogram = new TimeWindowFixedBoundaryHistogram(clock, DistributionStatisticConfig.builder()
.expiry(Duration.ofDays(1825)) // effectively never roll over
.bufferLength(1)
.build()
.merge(distributionStatisticConfig), true);
break;
case VictoriaMetrics:
histogram = new FixedBoundaryVictoriaMetricsHistogram();
break;
default:
histogram = null;
break;
}
} else {
histogram = null;
}
}
@Override
protected void recordNonNegative(long amount, TimeUnit unit) {
count.increment();
long nanoAmount = TimeUnit.NANOSECONDS.convert(amount, unit);
totalTime.add(nanoAmount);
max.record(nanoAmount, TimeUnit.NANOSECONDS);
if (histogram != null)
histogram.recordLong(TimeUnit.NANOSECONDS.convert(amount, unit));
}
@Override
public long count() {
return count.longValue();
}
@Override
public double totalTime(TimeUnit unit) {
return TimeUtils.nanosToUnit(totalTime.doubleValue(), unit);
}
@Override
public double max(TimeUnit unit) {
return max.poll(unit);
}
public HistogramFlavor histogramFlavor() {
return histogramFlavor;
}
/**
* For Prometheus we cannot use the histogram counts from HistogramSnapshot, as it is based on a
* rolling histogram. Prometheus requires a histogram that accumulates values over the lifetime of the app.
*
* @return Cumulative histogram buckets.
*/
public CountAtBucket[] histogramCounts() {
return histogram == null ? EMPTY_HISTOGRAM : histogram.takeSnapshot(0, 0, 0).histogramCounts();
}
@Override
public HistogramSnapshot takeSnapshot() {
HistogramSnapshot snapshot = super.takeSnapshot();
if (histogram == null) {
return snapshot;
}
return new HistogramSnapshot(snapshot.count(),
snapshot.total(),
snapshot.max(),
snapshot.percentileValues(),
histogramCounts(),
snapshot::outputSummary);
}
}
PrometheusTimer继承了AbstractTimer,默认histogram为null
PrometheusMeterRegistry
io/micrometer/prometheus/PrometheusMeterRegistry.java
protected io.micrometer.core.instrument.Timer newTimer(Meter.Id id, DistributionStatisticConfig distributionStatisticConfig, PauseDetector pauseDetector) {
PrometheusTimer timer = new PrometheusTimer(id, clock, distributionStatisticConfig, pauseDetector, prometheusConfig.histogramFlavor());
applyToCollector(id, (collector) ->
addDistributionStatisticSamples(distributionStatisticConfig, collector, timer, tagValues(id), false));
return timer;
}
PrometheusMeterRegistry的newTimer创建的是PrometheusTimer,同时applyToCollector调用了addDistributionStatisticSamples
addDistributionStatisticSamples
private void addDistributionStatisticSamples(DistributionStatisticConfig distributionStatisticConfig, MicrometerCollector collector,
HistogramSupport histogramSupport, List<String> tagValues, boolean forLongTaskTimer) {
collector.add(tagValues, (conventionName, tagKeys) -> {
Stream.Builder<Collector.MetricFamilySamples.Sample> samples = Stream.builder();
HistogramSnapshot histogramSnapshot = histogramSupport.takeSnapshot();
ValueAtPercentile[] percentileValues = histogramSnapshot.percentileValues();
CountAtBucket[] histogramCounts = histogramSnapshot.histogramCounts();
double count = histogramSnapshot.count();
if (percentileValues.length > 0) {
List<String> quantileKeys = new LinkedList<>(tagKeys);
quantileKeys.add("quantile");
// satisfies https://prometheus.io/docs/concepts/metric_types/#summary
for (ValueAtPercentile v : percentileValues) {
List<String> quantileValues = new LinkedList<>(tagValues);
quantileValues.add(Collector.doubleToGoString(v.percentile()));
samples.add(new Collector.MetricFamilySamples.Sample(
conventionName, quantileKeys, quantileValues, v.value(TimeUnit.SECONDS)));
}
}
Collector.Type type = distributionStatisticConfig.isPublishingHistogram() ? Collector.Type.HISTOGRAM : Collector.Type.SUMMARY;
if (histogramCounts.length > 0) {
// Prometheus doesn't balk at a metric being BOTH a histogram and a summary
type = Collector.Type.HISTOGRAM;
List<String> histogramKeys = new LinkedList<>(tagKeys);
String sampleName = conventionName + "_bucket";
switch (prometheusConfig.histogramFlavor()) {
case Prometheus:
histogramKeys.add("le");
// satisfies https://prometheus.io/docs/concepts/metric_types/#histogram
for (CountAtBucket c : histogramCounts) {
final List<String> histogramValues = new LinkedList<>(tagValues);
histogramValues.add(Collector.doubleToGoString(c.bucket(TimeUnit.SECONDS)));
samples.add(new Collector.MetricFamilySamples.Sample(
sampleName, histogramKeys, histogramValues, c.count()));
}
// the +Inf bucket should always equal `count`
final List<String> histogramValues = new LinkedList<>(tagValues);
histogramValues.add("+Inf");
samples.add(new Collector.MetricFamilySamples.Sample(
sampleName, histogramKeys, histogramValues, count));
break;
case VictoriaMetrics:
histogramKeys.add("vmrange");
for (CountAtBucket c : histogramCounts) {
final List<String> histogramValuesVM = new LinkedList<>(tagValues);
histogramValuesVM.add(FixedBoundaryVictoriaMetricsHistogram.getRangeTagValue(c.bucket()));
samples.add(new Collector.MetricFamilySamples.Sample(
sampleName, histogramKeys, histogramValuesVM, c.count()));
}
break;
default:
break;
}
}
samples.add(new Collector.MetricFamilySamples.Sample(
conventionName + (forLongTaskTimer ? "_active_count" : "_count"), tagKeys, tagValues, count));
samples.add(new Collector.MetricFamilySamples.Sample(
conventionName + (forLongTaskTimer ? "_duration_sum" : "_sum"), tagKeys, tagValues, histogramSnapshot.total(TimeUnit.SECONDS)));
return Stream.of(new MicrometerCollector.Family(type, conventionName, samples.build()),
new MicrometerCollector.Family(Collector.Type.GAUGE, conventionName + "_max", Stream.of(
new Collector.MetricFamilySamples.Sample(conventionName + "_max", tagKeys, tagValues,
histogramSnapshot.max(getBaseTimeUnit())))));
});
}
addDistributionStatisticSamples会判断isPublishingHistogram,是则发布HISTOGRAM,否则发布SUMMARY(
默认为否
),之后给samples添加_count及_sum的sample,最后额外添加一个_max的gauge
示例
示例1
Timer timer = meterRegistry.timer("abc", "a", "b");
timer.record(Duration.of(100, ChronoUnit.SECONDS));
最后产生的metrics如下
# HELP abc_seconds_max
# TYPE abc_seconds_max gauge
abc_seconds_max{a="b",} 0.0
# HELP abc_seconds
# TYPE abc_seconds summary
abc_seconds_count{a="b",} 1.0
abc_seconds_sum{a="b",} 100.0
示例2
http.server.requests的prometheus指标
# HELP http_server_requests_seconds_max
# TYPE http_server_requests_seconds_max gauge
http_server_requests_seconds_max{}
# TYPE http_server_requests_seconds summary
http_server_requests_seconds_count{}
http_server_requests_seconds_sum{}
grafana展示
HTTP Server Requests Count
{
"expr": "http_server_requests_seconds_count{instance=\"$instance\", application=\"$application\"}",
"format": "time_series",
"interval": "",
"intervalFactor": 1,
"legendFormat": "{{method}} [{{status}}] - {{uri}}",
"refId": "A"
}
HTTP Server Requests Sum
{
"expr": "http_server_requests_seconds_sum{instance=\"$instance\", application=\"$application\"}",
"format": "time_series",
"interval": "",
"intervalFactor": 1,
"legendFormat": "{{method}} [{{status}}] - {{uri}}",
"refId": "A"
}
HTTP Server Requests Max
{
"expr": "http_server_requests_seconds_max{instance=\"$instance\", application=\"$application\"}",
"format": "time_series",
"interval": "",
"intervalFactor": 1,
"legendFormat": "{{method}} [{{status}}] - {{uri}}",
"refId": "A"
}
Total Requests
{
"expr": "sum(increase(http_server_requests_seconds_count{application=\"$application\", instance=~\"$instance\"}[150s]))",
"format": "time_series",
"instant": false,
"interval": "",
"intervalFactor": 1,
"legendFormat": "",
"refId": "A"
}
Request Count
{
"expr": "irate(http_server_requests_seconds_count{instance=\"$instance\", application=\"$application\", uri!~\".*actuator.*\"}[5m])",
"format": "time_series",
"interval": "",
"intervalFactor": 1,
"legendFormat": "{{method}} [{{status}}] - {{uri}} -{{reqPath}}",
"refId": "A"
}
Failed Requests
{
"expr": "sum(increase(http_server_requests_seconds_count{application=\"$application\", instance=~\"$instance\", uri=~\"$uri\", status!=\"200\"}[$__interval]))",
"format": "time_series",
"intervalFactor": 1,
"refId": "A"
}
Req / sec
{
"expr": "sum(irate(http_server_requests_seconds_count{application=\"$application\", instance=~\"$instance\"}[1m]))",
"format": "time_series",
"interval": "",
"intervalFactor": 1,
"legendFormat": "",
"refId": "A"
}
Requests per second
{
"expr": "rate(http_server_requests_seconds_count{application=\"$application\", instance=\"$instance\"}[1m])",
"format": "time_series",
"intervalFactor": 1,
"legendFormat": "{{method}}-{{status}}-{{uri}}",
"refId": "A"
}
Top 10 Most Used API endpoints
{
"expr": "topk(10, sum by(uri, method) (rate(http_server_requests_seconds_count{application=\"$application\", instance=~\"$instance\"}[1m])))",
"format": "time_series",
"instant": false,
"interval": "",
"intervalFactor": 1,
"legendFormat": "",
"refId": "A"
}
Error Rate
{
"expr": "sum(increase(http_server_requests_seconds_count{application=\"$application\", instance=~\"$instance\", uri=~\"$uri\", status!=\"200\"}[$__interval])) / sum(increase(http_server_requests_seconds_count{application=\"$application\", instance=~\"$instance\", uri=~\"$uri\"}[$__interval])) * 100",
"format": "time_series",
"intervalFactor": 1,
"refId": "A"
}
Mean response time
{
"expr": "rate(http_server_requests_seconds_sum{application=\"$application\", instance=\"$instance\"}[1m])/rate(http_server_requests_seconds_count{application=\"$application\", instance=\"$instance\"}[1m])",
"format": "time_series",
"instant": false,
"intervalFactor": 1,
"legendFormat": "{{method}}-{{status}}-{{uri}}",
"refId": "A"
}
Response Time
{
"expr": "irate(http_server_requests_seconds_sum{instance=\"$instance\", application=\"$application\", exception=\"None\", uri!~\".*actuator.*\"}[5m]) / irate(http_server_requests_seconds_count{instance=\"$instance\", application=\"$application\", exception=\"None\", uri!~\".*actuator.*\"}[5m])",
"format": "time_series",
"intervalFactor": 1,
"legendFormat": "{{method}} [{{status}}] - {{uri}}",
"refId": "A"
}
Response time of 50%, 75%, 90%, 95% of requests
{
"expr": "histogram_quantile(0.95, sum(rate(http_server_requests_seconds_bucket{application=\"$application\", instance=\"$instance\"}[1m])) by (le))",
"format": "time_series",
"instant": false,
"interval": "",
"intervalFactor": 1,
"legendFormat": "95%",
"refId": "A"
},
{
"expr": "histogram_quantile(0.9, sum(rate(http_server_requests_seconds_bucket{application=\"$application\", instance=\"$instance\"}[1m])) by (le))",
"format": "time_series",
"intervalFactor": 1,
"legendFormat": "90%",
"refId": "B"
},
{
"expr": "histogram_quantile(0.75, sum(rate(http_server_requests_seconds_bucket{application=\"$application\", instance=\"$instance\"}[1m])) by (le))",
"format": "time_series",
"intervalFactor": 1,
"legendFormat": "75%",
"refId": "C"
},
{
"expr": "histogram_quantile(0.5, sum(rate(http_server_requests_seconds_bucket{application=\"$application\", instance=\"$instance\"}[1m])) by (le))",
"format": "time_series",
"intervalFactor": 1,
"legendFormat": "50%",
"refId": "D"
}
小结
springboot的WebMvcMetricsAutoConfiguration给mvc提供了http.server.requests指标,类型为timer,当输出到prometheus的时候,默认是没有开启Histogram,输出的是一个_max的gauge,以及summary类型(_count及_sum)。