Skip to content

Commit

Permalink
Document how to define a custom meter registry (#112)
Browse files Browse the repository at this point in the history
Adds a documentation section explaining how Micrometer users can create their own MeterRegistry implementation.

Closes gh-111
  • Loading branch information
izeye authored and shakuzen committed Dec 9, 2019
1 parent f566862 commit dee5482
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/components/DocRoot/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ export default function DocRoot() {
<ul>
<li><NavLink className="doc-section" to="/docs/guide/httpSenderResilience4jRetry">HttpSender with Resilience4j retry</NavLink></li>
</ul>
<ul>
<li><NavLink className="doc-section" to="/docs/guide/customMeterRegistry">Custom meter registry</NavLink></li>
</ul>
</li>
<li>
<NavLink className="doc-section" to="/docs/support">Support policy</NavLink>. Micrometer's support policy for releases.
Expand Down
5 changes: 5 additions & 0 deletions src/components/DocRoutes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ let docsOkHttpClient = require('!asciidoc-loader!../../docs/okhttpclient/index.a
let docsConsoleReporter = require('!asciidoc-loader!../../docs/guide/console-reporter.adoc');
let docsHealthCheck = require('!asciidoc-loader!../../docs/guide/health-check.adoc');
let docsHttpSenderResilience4jRetry = require('!asciidoc-loader!../../docs/guide/http-sender-resilience4j-retry.adoc');
let docsCustomMeterRegistry = require('!asciidoc-loader!../../docs/guide/custom-meter-registry.adoc');
let docsSupport = require('!asciidoc-loader!../../docs/support/index.adoc');

const systems = ['appOptics', 'atlas', 'azure-monitor', 'datadog', 'dynatrace', 'elastic', 'ganglia', 'graphite', 'humio', 'influx', 'instana', 'jmx', 'kairos', 'new-relic', 'prometheus', 'signalFx', 'stackdriver', 'statsD', 'wavefront'];
Expand Down Expand Up @@ -67,6 +68,10 @@ export default function DocRoutes() {
<DocSection title="HttpSender with Resilience4j retry" content={docsHttpSenderResilience4jRetry}/>
}/>

<Route path="/docs/guide/customMeterRegistry" render={() =>
<DocSection title="Custom meter registry" content={docsCustomMeterRegistry}/>
}/>

<Route path="/docs/support" render={() =>
<DocSection title="Micrometer Support Policy" content={docsSupport}/>
}/>
Expand Down
72 changes: 72 additions & 0 deletions src/docs/guide/custom-meter-registry.adoc
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);
}
}
----

0 comments on commit dee5482

Please sign in to comment.