You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Dropwizard Metrics统计Spring Boot REST接口请求的平均ids条目数

Solution: Track Average List Size with Dropwizard Metrics

To track the average number of entries in the ids list across all requests, we'll use a Histogram metric from Dropwizard Metrics. Histograms are ideal here because they collect and summarize the distribution of numeric values (like list sizes), including calculating the mean (average), count, min/max, and percentiles.

Step-by-Step Implementation

  1. Integrate the Histogram Metric into Your Controller
    After deserializing the RequestCollection object, retrieve the size of the ids list and update the histogram metric. Here's the modified controller code:
@RestController
@RequestMapping("/api")
public class ApiController {
    private final MetricRegistry metricRegistry;
    // Pre-create the histogram in the constructor for consistent access
    private final Histogram idsSizeHistogram;

    public ApiController(MetricRegistry metricRegistry) {
        this.metricRegistry = metricRegistry;
        // Create a namespaced metric name to avoid conflicts
        this.idsSizeHistogram = metricRegistry.histogram(
            MetricRegistry.name(ApiController.class, "request-ids-list-size")
        );
    }

    @GetMapping
    public String getFooBarred(@RequestParam(value = "params") String requestItem) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper()
            .registerModule(new JavaTimeModule());
        RequestCollection request = objectMapper.readValue(requestItem, RequestCollection.class);

        // Update the histogram with the size of the ids list
        idsSizeHistogram.update(request.getIds().size());

        return "foobar";
    }
}

Key Details Explained

  • Histogram Naming: Using MetricRegistry.name() generates a structured metric name (e.g., your.package.ApiController.request-ids-list-size) which prevents naming collisions with other metrics in your application.
  • Metric Update: Every request triggers idsSizeHistogram.update() with the length of the ids list. The histogram automatically accumulates these values and computes statistics in the background.
  • Edge Case Handling: Since your RequestCollection constructor initializes ids as an empty ArrayList, even if the incoming JSON doesn't include the ids field, the size will be 0—this is safely recorded in the histogram.

Accessing the Average Value

Dropwizard Metrics' Histogram provides a Snapshot object with all computed statistics. To get the average programmatically, you can call:

double averageListSize = idsSizeHistogram.getSnapshot().getMean();

If you're using a metrics reporter (like ConsoleReporter, GraphiteReporter, or PrometheusReporter), these tools will automatically expose the average (along with count, min/max, and percentiles) in your monitoring system. For example, a console reporter might output:

your.package.ApiController.request-ids-list-size: count=10, mean=4.2, min=0, max=10, p50=4, p95=8, p99=10


内容的提问来源于stack exchange,提问作者Always Learning

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:11:17