【问题标题】:Dropwizard Metrics Get Average Items Per REST RequestDropwizard Metrics 获取每个 REST 请求的平均项目
【发布时间】:2018-05-11 02:06:27
【问题描述】:

我想使用 Dropwizard 指标来获取我的 Spring Boot 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 类,我想在所有请求中测量其 ids 字段。

请求集合

public class RequestCollection {
    private List<Integer> ids;

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

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

【问题讨论】:

    标签: java spring spring-boot dropwizard metrics


    【解决方案1】:

    5 main metric types、量规、计数器、直方图、计量器和计时器中,最适合您需要的可能是Histogram,它“测量数据流中值的分布:例如,搜索返回的结果数”。

    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
    

    【讨论】:

    • 这太好了,谢谢!直方图数据保存多长时间?即,它是跨特定时间跨度的移动窗口吗?
    • 它应该在应用程序的生命周期内存储。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-05
    • 2022-01-23
    • 1970-01-01
    • 2010-12-17
    • 1970-01-01
    • 2021-01-30
    • 1970-01-01
    相关资源
    最近更新 更多