-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Document how to define a custom meter registry (#112)
Adds a documentation section explaining how Micrometer users can create their own MeterRegistry implementation. Closes gh-111
- Loading branch information
Showing
3 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
= Custom meter registry | ||
|
||
Micrometer supports popular meter registries out of the box, so please check them first. | ||
For an existing meter registry, if you think that your requirements are generally useful, please consider creating an issue or PR against the Micrometer issue tracker. | ||
For a non-existing meter registry, if it's widely-used, please consider creating an issue or PR for it. | ||
|
||
For some reasons such as unusual requirements, if you want to create your own custom meter registry, you can create it by extending one of base classes for meter registries: `MeterRegistry`, `PushMeterRegistry`, and `StepMeterRegistry`. | ||
|
||
Most common way is to extend `StepMeterRegistry`. | ||
You can create your own custom `StepMeterRegistry` as follows. | ||
|
||
First, define your own meter registry configuration by extending `StepRegistryConfig` as follows: | ||
|
||
[source,java] | ||
---- | ||
public interface CustomRegistryConfig extends StepRegistryConfig { | ||
CustomRegistryConfig DEFAULT = k -> null; | ||
@Override | ||
default String prefix() { | ||
return "custom"; | ||
} | ||
} | ||
---- | ||
|
||
Second, define your own meter registry by extending `StepMeterRegistry` as follows: | ||
|
||
[source,java] | ||
---- | ||
public class CustomMeterRegistry extends StepMeterRegistry { | ||
public CustomMeterRegistry(CustomRegistryConfig config, Clock clock) { | ||
super(config, clock); | ||
start(new NamedThreadFactory("custom-metrics-publisher")); | ||
} | ||
@Override | ||
protected void publish() { | ||
getMeters().stream().forEach(meter -> System.out.println("Publishing " + meter.getId())); | ||
} | ||
@Override | ||
protected TimeUnit getBaseTimeUnit() { | ||
return TimeUnit.MILLISECONDS; | ||
} | ||
} | ||
---- | ||
|
||
Finally, create the meter registry configuration and the meter registry. | ||
If you are using Spring Boot, you can do as follows: | ||
|
||
[source,java] | ||
---- | ||
@Configuration | ||
public class MetricsConfig { | ||
@Bean | ||
public CustomRegistryConfig customRegistryConfig() { | ||
return CustomRegistryConfig.DEFAULT; | ||
} | ||
@Bean | ||
public CustomMeterRegistry customMeterRegistry(CustomRegistryConfig customRegistryConfig, Clock clock) { | ||
return new CustomMeterRegistry(customRegistryConfig, clock); | ||
} | ||
} | ||
---- |