如何以千分尺更新导出指标的标签值?

How to update tag value for exported metric in micrometer?

我正在使用千分尺导出第三方 api 消费量。 现在我想精确计算失败的请求并导出每个失败的请求 ID。 为每个 restTemplate 交换调用调用以下方法。

private DistributionSummary incFailedCounter(String requestId) {

        this.registry = beanProvider.getRegistry();

        DistributionSummary summary = summarys.get(myCounter);
        if (summary == null) {
            Builder tags = DistributionSummary.builder("failed.test").tags("req_id", requestId, "count", "1");
            summary = tags.register(registry);
            summarys.put(myCounter, summary);
        } else {

            String tag = summary.getId().getTag("req_id");
            String[] split = tag.split(",");

            summary.close();

            summarys.put(myCounter,
                    DistributionSummary.builder("failed.test")
                            .tags("req_id", tag + ", " + requestId, "count", String.valueOf(split.length + 1))
                            .register(registry));
        }
        return summary;
    }

此代码为每个请求的指标插入新行。

failed_test_count{count="1",instance="localhost:8080",job="monitor-app",req_id="1157408321"}
failed_test_count{count="2",instance="localhost:8080",job="monitor-app",req_id="1157408321, 1157408321"}
failed_test_count{count="3",instance="localhost:8080",job="monitor-app",req_id="1157408321, 1157408321, 1157408321"}

问题是这个指标的大小随着请求的增加而增加。 有没有办法删除或替换相同的标签并仅导出一个具有更新的 req_ids 的动态指标?

无法删除或更新 标签,因为它们是不可变的。一种方法是注销电流表。使用以下方法删除注册仪表并应用新仪表。

registry.remove(summary.getId());

这会产生一个线度量。

failed_test_count{count="4",instance="localhost:8080",job="monitor-app",req_id="1157408321, 58500184, 58500184, 58500184"}