juanmorschrott
juanmorschrott

Reputation: 603

Is a micrometer meter cached when I create an instance?

What happens under the hood when I create a Micrometer Counter or Timer? Is it cached? If it is cached could I use the builder anytime and add tags dinamically?

Counter counter = Counter
    .builder("instance_name")
    .description("bla bla...")
    .tags("tag_name", "tag_value")
    .register(registry);

Upvotes: 0

Views: 626

Answers (1)

Jonatan Ivanov
Jonatan Ivanov

Reputation: 6863

I'm not sure what do you man by cached. Meters in Micrometer are created from and held in a MeterRegistry. Also, a meter is uniquely identified by its combination of name and tags so if you request a meter with the same name and tags, you should get back the same instance:

Counter counter1 = Counter
        .builder("instance_name")
        .description("bla bla...")
        .tags("tag_name", "tag_value")
        .register(registry);

Counter counter2 = Counter
        .builder("instance_name")
        .description("bla bla...")
        .tags("tag_name", "tag_value")
        .register(registry);

System.out.println(counter1 == counter2); // should print true

You can do this any time and add tags dynamically (see: WebMvcMetricsFilter), the only thing you need to pay attention is calling register at the end.

Upvotes: 3

Related Questions