My set-up:
- Spring boot app based on version
3.1.3
- Added dependency
spring-boot-starter-actuator
- Added custom metric
MyHealthMetricsExportConfiguration
according to the documentation (shown below) - Under
/actuator/metrics
I can seehealth
metric - I’ve downloaded the latest otel java agent from github
- I’ve added the config in intellij “vm options”:
-javaagent:./opentelemetry-javaagent.jar
-Dotel.metrics.exporter=otlp
- I’ve run otel collector with
receiver
set tootlp
The question
I don’t know how to make otel agent ‘to see’ and export spring metrics like f.e. ‘MyHealthMetricsExportConfiguration’. On that blog it should be as simple as:
// Unregister the OpenTelemetryMeterRegistry from Metrics.globalRegistry and make it available
// as a Spring bean instead.
@Bean
@ConditionalOnClass(name = "io.opentelemetry.javaagent.OpenTelemetryAgent")
public MeterRegistry otelRegistry() {
Optional<MeterRegistry> otelRegistry = Metrics.globalRegistry.getRegistries().stream()
.filter(r -> r.getClass().getName().contains("OpenTelemetryMeterRegistry"))
.findAny();
otelRegistry.ifPresent(Metrics.globalRegistry::remove);
return otelRegistry.orElse(null);
}
But that doesn’t work for me. First of all durig bean creation Metrics.globalRegistry.getRegistries()
is empty. Second of all, no health
metric is available in otel collector. What can I do to make this agent send my custom metric to collector ?
@Configuration(proxyBeanMethods = false)
public class MyHealthMetricsExportConfiguration {
public MyHealthMetricsExportConfiguration(MeterRegistry registry, HealthEndpoint healthEndpoint) {
// This example presumes common tags (such as the app) are applied elsewhere
Gauge.builder("health", healthEndpoint, this::getStatusCode).strongReference(true).register(registry);
}
private int getStatusCode(HealthEndpoint health) {
Status status = health.health().getStatus();
if (Status.UP.equals(status)) {
return 3;
}
if (Status.OUT_OF_SERVICE.equals(status)) {
return 2;
}
if (Status.DOWN.equals(status)) {
return 1;
}
return 0;
}
}