Dropwizard 指标获取每个 REST 请求的平均项目

Dropwizard Metrics Get Average Items Per REST Request

我想使用 Dropwizard 指标来获取列表中每个请求我的 spring 引导 REST 控制器服务的平均项目数。我的控制器接受一个序列化为 RequestCollection 的字符串。 RequestCollection 有一个由 ID 列表组成的 List<Integer> ids 字段。我想知道 ids 字段中所有请求的平均项目数是多少。

控制器

@RestController
@RequestMapping("/api")
public class ApiController {

    private MetricRegistry metricRegistry;

    public ApiController(MetricRegistry metricRegistry) {
        this.metricRegistry = metricRegistry;
    }

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

        // Add metric here to metricRegistry for average number of items 
        // in RequestCollection across all requests.
        // RequestCollection has a single property which is a List<Integer> ids

        return "foobar";
    }
}

这是 RequestCollection class 我想在所有请求中测量其 ids 字段。

请求集合

public class RequestCollection {
    private List<Integer> ids;

    public RequestCollection() {
        this.ids = new ArrayList<>();
    }

    public List<Integer> getIds() {
        return ids;
    }
}

5 main metric types, Guages, Counters, Histograms, Meters, and Timers, the one that suits your need the best would probably be the Histogram,其中"measures the distribution of values in a stream of data: e.g., the number of results returned by a search".

Histogram itemCount
    = metrics.histogram(MetricRegistry.name(RequestCollection.class, "id-count"));

在每个请求中,只需使用集合计数更新直方图。

itemCount.update(collection.getIds().size());

报告会给你一些不同的统计数据,以及你想要的平均值

-- Histograms ----------------------------
com.example.RequestCollection.id-count
             count = 100
               min = 0
               max = 99
              mean = 45.47
            stddev = 31.65
            median = 45.00
              75% <= 73.00
              95% <= 94.00
              98% <= 99.00
              99% <= 99.00
            99.9% <= 99.00