querying data with micrometer

2019-09-01 13:12发布

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条回答
祖国的老花朵
2楼-- · 2019-09-01 13:44

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

查看更多
登录 后发表回答