querying data with micrometer

試著忘記壹切 提交于 2020-01-06 14:54:28

问题


We have this fancy monitoring system to which our spring-boot services are posting metrics to an influx DB with micrometer. There's a nice grafana frontend, but the problem is that we're now at a stage where we have to have some of these metrics available in other services to reason on. The whole system was set up by my predecessor, and my current understanding of it is practically zero. I can add and post new metrics, but I can't for the life of me get anything out of it.

Here's a short example: Our gateway increments the counter for each image that a camera posts to it. The definition of the counter looks like this:

private val imageCounters = mutableMapOf<String, Counter>()
private val imageCounter = { camera: String ->
    imageCounters.getOrPut(camera) {
        registry.counter("gateway.image.counter", "camera", camera)
    }

And the counter is incremented in the code like this:

imageCounter("placeholder-id").increment()

Now we're improving our billing, and the billing service needs to know how many images for a certain camera went through the gateway. So naturally the first thing I try looks like this:

class MonitoringService(val metrics: MeterRegistry) {

    private val log = logger()
    private val imageCounters = mutableMapOf<String, Counter>()
    private val imageCounter = { camera: String ->
        imageCounters.getOrPut(camera) {
            metrics.counter("gateway.image.counter", "camera", camera)
        }
    }

    fun test() {
        val test = imageCounter("16004").count()
        val bugme = true
        log.info("influx test: $test")
    }
}

There's two problems with this: First off it always returns zero, so obviously I'm doing it wrong. I just can't figure out what it is. Second, even if it would return a reasonable value, I don't see a way to limit this by time (I'll usually need the number of images uploaded during the current month).

What worries me is that while I can find a lot of documentation on how to post data with micrometer, I can't seem to find any documentation on how to query. Is Micrometer only designed to post monitoring data, but not query it? the .getOrPut() method would indicate it can do both, but since querying data seems undocumented as far as I can tell, that might be a misconception on my part.

There is an influx-db client for Java, which I'll try next, but at the end of the day I don't want multiple components in my application doing the same thing just because I'm not familiar with the tools I inherited.


回答1:


InfluxMeterRegistry is a StepMeterRegistry, so the created Counter from it is a StepCounter. StepCounter.increment() increments the count in the current step but StepCounter.count() will return the count in the previous step. That's why you're seeing 0 with count() although you've already invoked increment() several times. You can see it in the next step and the default step is 1 minute, so you have to wait for 1 minute to see it.

See the following test to get an idea on how it works: https://github.com/izeye/sample-micrometer-spring-boot/blob/influx/src/test/java/com/izeye/sample/InfluxMeterRegistryTests.java



来源:https://stackoverflow.com/questions/52661701/querying-data-with-micrometer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!