英文:
Counter with LoggingMeterRegistry not showing expected count
问题
我正在尝试用micormeter的LoggingMeterRegistry
替换PrometheusMeterRegistry
,以便以特定格式将指标导出为日志事件。在执行单元测试时,我注意到使用Meter#measure()
检索测量值后,Counter
计数器显示的计数不正确。在这里,您可以找到一个最小的示例来重现所描述的行为。测试用例work5
调用一个方法5
次,在方法内部注册并递增一个计数器。预期的计数器值应为5
,使用PrometheusMeterRegistry
时,实际值也为5
,而使用LoggingMeterRegistry
时,实际值为0
。
- 我的测试设置或对
LoggingMeterRegistry
的使用有问题吗?因此,期望值是错误的吗? - 如果没有问题,那么如何使
LoggingMeterRegistry
正确工作?
英文:
I am trying to replace PrometheusMeterRegistry
with micormeter's LoggingMeterRegistry
in order to export metrics as log events in a specific format. When executing my unittests I have noticed that the Counter
meters are not showing the correct counts after retrieving the measurments with Meter#measure()
. Here you can find a minimal example reproducing the behaviour described. The test case work5
calls a methode 5
times, withing the method a counter is registered and incremented. The expected counter value is then 5
, with the PrometheusMeterRegistry
the actual value is also 5
, with LoggingMeterRegistry
the actual value is 0
.
- Is something wrong with my test setup or usage of the
LoggingMeterRegistry
and thus the expectation is wrong? - If not, then how to make
LoggingMeterRegistry
behave correctly?
答案1
得分: 2
由于我需要计数器以与示例项目中的Prometheus Meter Registry 相同的方式反映计数,并且注册表将测量推送到其接收器,因此我决定创建一个自定义的日志计量注册表 CustomLoggingMeterRegistry
,扩展 LoggingMeterRegistry
,在其中可以通过重写 newCounter
方法来控制使用哪种 Counter
的实现。
@Override
protected Counter newCounter(Id id) {
CountingMode countingMode = getCountingMode();
switch (countingMode) {
case CUMULATIVE:
return new CumulativeCounter(id);
case STEP:
return new StepCounter(id, clock, config.step().toMillis());
default:
throw new UnsupportedOperationException(String
.format("Unsupported counting mode '%s'",
countingMode.name()));
}
}
StepCounter
只是问题的一个实例,对于任何是 StepMeter
的东西,都可能是同样的情况,需要相应地处理。
示例 项目 已更新,包含一个实现草稿。
编辑(2023-08-18):
由于在其他 Meter
(例如 Timer
)中使用 Counter
用于统计数据,上述实现存在一些问题,因此我放弃了 CustomLoggingMeterRegistry
,转而使用 SimplePushMeterRegistry
,它是一个将新的 Meter
实例的创建委托给 SimpleMeterRegistry
实现的 PushMeterRegistry
。如果继续使用 CustomLoggingMeterRegistry
,则需要为所有的 newXxx(...)
方法提供实现,根据可用的实现来看,这并不是一件容易的事情,就像 Counter
的情况一样。
示例 项目 已更新,包含一个实现草稿。
英文:
As I need the counter to reflect the count the same way the prometheus meter registry does for the usecase shown in the sample project and also the fact that the registry pushes the measurments to its sink, I have decided to create a custom logging meter registry CustomLoggingMeterRegistry
extending LoggingMeterRegistry
where I can control what implementation of Counter
is used by overriding the newCounter
method.
@Override
protected Counter newCounter(Id id) {
CountingMode countingMode = getCountingMode();
switch (countingMode) {
case CUMULATIVE:
return new CumulativeCounter(id);
case STEP:
return new StepCounter(id, clock, config.step().toMillis());
default:
throw new UnsupportedOperationException(String
.format("Unsupported counting mode '%s'",
countingMode.name()));
}
}
StepCounter
is only one instance of the issue, it may technically be the case for anything that is a StepMeter
and needs to be handled accordingly.
Sample project updated with an impelemntation draft.
EDIT (2023-08-18):
As some issues have arised with the implementation above, because Counter
s are used for statistics in other Meter
s, for example Timer
, I have abandoned the CustomLoggingMeterRegistry
in favour of a SimplePushMeterRegistry
which is a PushMeterRegistry
that delegates the creation of new Meter
instances to the implementation SimpleMeterRegistry
. Pursuing CustomLoggingMeterRegistry
would have required providing an implementation for all newXxx(...)
methods, which by looking at available implementations is not trivial as in the case of Counter
.
Sample project updated with an impelemntation draft.
答案2
得分: 1
请阅读我最近写的与InfluxDB和相关注册表有关的这个相关的SO答案,我认为其中提供的解释背后的推理对于LoggingMeterRegistry
也是相同的。
正如答案中所指出的,我认为您描述的行为可以解释为LoggingMeterRegistry
在客户端使用了与InfluxDB类似的基于步骤的方法来执行速率聚合。请注意,Prometheus注册表提供了一个完整的实现(而不是基于步骤的),并且聚合是在服务器端执行的。
英文:
Please, consider read this related SO answer I wrote recently, it is related to InfluxDB and the associated registry but I think the reasoning behind the explanation provided is the same for LoggingMeterRegistry
.
As indicated in the answer, I think the behavior you are describing could be explained because LoggingMeterRegistry
uses as in the case of InfluxDB a step based approach for performing rate aggregation, in client side. Note that the Prometheus registry provides a full fledged implementation - not step based - and the aggregation is being performed on server side.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论