Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add custom micrometer registries via NeonBeeConfig
With this change the config parameter micrometerRegistries is introduced which allows the user to specify a list of (full qualified) class names, which must implement the load() method of the functional interface MicrometerRegistryLoader. The load method must return a MeterRegistry, which will be added to the micrometer registries by NeonBee. The PrometheusMeterRegistry will be the default registry, which is only available when the MetricsEndpoint is loaded. Co-authored-by: Michael Halberstadt <michael.halberstadt@sap.com>
- Loading branch information
Showing
22 changed files
with
797 additions
and
108 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# Metrics Concept | ||
|
||
## Content | ||
|
||
- [Metrics](#Metrics) | ||
- [configure additional metrics registries](#configure-additional-metrics-registries) | ||
- [when launching NeonBee](#when-launching-NeonBee) | ||
- [during runtime](#during-runtime) | ||
- [add metrics to your code](#add-metrics-to-your-code) | ||
|
||
|
||
## Metrics | ||
In NeonBee, Micrometer is used to provide reporting to various backends. Micrometer provides a facade for the most | ||
common monitoring systems. | ||
Micrometer `MeterRegistry` objects can be registered at runtime. For this purpose, a `CompositeMeterRegistry` is added in | ||
the `VertxOptions`. Additional `MeterRegistry` can be added to this `CompositeMeterRegistry` at runtime. | ||
|
||
The `MetricsEndpoint`, which by default provides the Prometheus metrics under the /metrics path, registers the | ||
`PrometheusMeterRegistry` when the `MetricsEndpoint` router is created. | ||
|
||
## configure additional metrics registries | ||
### when launching NeonBee | ||
To register own Micrometer MeterRegistry interface `MicrometerRegistryLoader` must be implemented | ||
and the implementing class must be specified in the configuration `io.neonbee.NeonBee.yaml` in the `micrometerRegistries` | ||
array. | ||
|
||
Example MicrometerRegistryLoader implementation: | ||
```java | ||
package io.neonbee.config.examples; | ||
|
||
import io.micrometer.core.instrument.logging.LoggingMeterRegistry; | ||
import io.neonbee.config.metrics.MicrometerRegistryLoader; | ||
|
||
public static class LoggingMeterMicrometerRegistryLoader implements MicrometerRegistryLoader { | ||
@Override | ||
public MeterRegistry load(JsonObject config) { | ||
return new LoggingMeterRegistry(); | ||
} | ||
} | ||
``` | ||
|
||
Example io.neonbee.NeonBee.yaml configuration: | ||
```yaml | ||
--- | ||
// Omitted other configuration values | ||
micrometerRegistries: | ||
- className: io.neonbee.config.examples.LoggingMeterMicrometerRegistryLoader | ||
config: | ||
key: value | ||
``` | ||
|
||
### during runtime | ||
If you want to add a MeterRegistry during runtime, you can do it using the `NeonBeeConfig#getCompositeMeterRegistry()` | ||
method. | ||
|
||
```java | ||
MeterRegistry yourRegistry; // Your registry to be added. | ||
CompositeMeterRegistry compositeMeterRegistry = NeonBee.get(vertx).getConfig().getCompositeMeterRegistry(); | ||
compositeMeterRegistry.add(yourRegistry); | ||
``` | ||
## add metrics to your code | ||
|
||
To provide metrics in your code, here is an example of a counter: | ||
```java | ||
MeterRegistry registry = BackendRegistries.getDefaultNow(); | ||
Counter counter = registry.counter("TestEndpointCounter", "TestTag1", "TestValue"); | ||
counter.increment(); | ||
count = counter.count(); | ||
``` | ||
For more information, see [user defined metrics](https://vertx.io/docs/vertx-micrometer-metrics/java/#_user_defined_metrics) |
47 changes: 47 additions & 0 deletions
47
src/generated/java/io/neonbee/config/MicrometerRegistryConfigConverter.java
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,47 @@ | ||
package io.neonbee.config; | ||
|
||
import java.util.Base64; | ||
|
||
import io.vertx.core.json.JsonObject; | ||
import io.vertx.core.json.impl.JsonUtil; | ||
|
||
/** | ||
* Converter and mapper for {@link io.neonbee.config.MicrometerRegistryConfig}. NOTE: This class has been automatically | ||
* generated from the {@link io.neonbee.config.MicrometerRegistryConfig} original class using Vert.x codegen. | ||
*/ | ||
public class MicrometerRegistryConfigConverter { | ||
|
||
private static final Base64.Decoder BASE64_DECODER = JsonUtil.BASE64_DECODER; | ||
|
||
private static final Base64.Encoder BASE64_ENCODER = JsonUtil.BASE64_ENCODER; | ||
|
||
static void fromJson(Iterable<java.util.Map.Entry<String, Object>> json, MicrometerRegistryConfig obj) { | ||
for (java.util.Map.Entry<String, Object> member : json) { | ||
switch (member.getKey()) { | ||
case "className": | ||
if (member.getValue() instanceof String) { | ||
obj.setClassName((String) member.getValue()); | ||
} | ||
break; | ||
case "config": | ||
if (member.getValue() instanceof JsonObject) { | ||
obj.setConfig(((JsonObject) member.getValue()).copy()); | ||
} | ||
break; | ||
} | ||
} | ||
} | ||
|
||
static void toJson(MicrometerRegistryConfig obj, JsonObject json) { | ||
toJson(obj, json.getMap()); | ||
} | ||
|
||
static void toJson(MicrometerRegistryConfig obj, java.util.Map<String, Object> json) { | ||
if (obj.getClassName() != null) { | ||
json.put("className", obj.getClassName()); | ||
} | ||
if (obj.getConfig() != null) { | ||
json.put("config", obj.getConfig()); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.