首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Dropwizard度量获取每个REST请求的平均项

问Dropwizard度量获取每个REST请求的平均项
EN

Stack Overflow用户
提问于 2018-05-11 02:06:27
回答 1查看 977关注 0票数 1

我希望使用Dropwizard度量来获取每个请求(我的spring引导REST控制器服务)列表中的平均项目数。我的控制器接收一个字符串,该字符串被序列化为一个RequestCollection。RequestCollection有一个List<Integer> ids字段,该字段包含一个ids列表。我想知道ids字段中所有请求的平均条目数。

控制器

代码语言:javascript
复制
@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字段。

RequestCollection

代码语言:javascript
复制
public class RequestCollection {
    private List<Integer> ids;

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

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

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-11 06:30:28

在5种主要度量类型、Guage、计数器、直方图、仪表和计时器中,最适合您需要的可能是Histogram,它“测量数据流中值的分布:例如,搜索返回的结果数量”。

代码语言:javascript
复制
Histogram itemCount
    = metrics.histogram(MetricRegistry.name(RequestCollection.class, "id-count"));

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

代码语言:javascript
复制
itemCount.update(collection.getIds().size());

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

代码语言:javascript
复制
-- 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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50283974

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档