Registry
Meters in Micrometer are created from and held in a MeterRegistry. Each supported monitoring system has an implementation of MeterRegistry. How a registry is created varies for each implementation.
Micrometer includes a SimpleMeterRegistry that holds the latest value of each meter in memory and does not export the data anywhere. If you do not yet have a preferred monitoring system, you can get started playing with metrics by using the simple registry:
MeterRegistry registry = new SimpleMeterRegistry();
A SimpleMeterRegistry is autowired for you in Spring-based applications.
|
Composite Registries
Micrometer provides a CompositeMeterRegistry to which you can add multiple registries, letting you publish metrics to more than one monitoring system simultaneously:
CompositeMeterRegistry composite = new CompositeMeterRegistry();
Counter compositeCounter = composite.counter("counter");
compositeCounter.increment(); (1)
SimpleMeterRegistry simple = new SimpleMeterRegistry();
composite.add(simple); (2)
compositeCounter.increment(); (3)
| 1 | Increments are NOOP’d until there is a registry in the composite. The counter’s count still yields 0 at this point. |
| 2 | A counter named counter is registered to the simple registry. |
| 3 | The simple registry counter is incremented, along with counters for any other registries in the composite. |
Global Registry
Micrometer provides a static global registry called Metrics.globalRegistry and a set of static builders for generating meters based on this registry (note that globalRegistry is a composite registry):
class MyComponent {
Counter featureCounter = Metrics.counter("feature", "region", "test"); (1)
void feature() {
featureCounter.increment();
}
void feature2(String type) {
Metrics.counter("feature.2", "type", type).increment(); (2)
}
}
class MyApplication {
void start() {
// wire your monitoring system to global static state
Metrics.addRegistry(new SimpleMeterRegistry()); (3)
}
}
| 1 | Wherever possible (and especially where instrumentation performance is critical), store Meter instances in fields to avoid a lookup on their name or tags on each use. |
| 2 | When tags need to be determined from local context, you have no choice but to construct or lookup the Meter inside your method body. The lookup cost is just a single hash lookup, so it is acceptable for most use cases. |
| 3 | It is OK to add registries after meters have been created with code like Metrics.counter(…). These meters are added to each registry, as it is bound to the global composite. |