Considering the following code that defines a simple counter:
OpenTelemetry otel = ...
var meter = otel.getMeter("com.my.app");
LongCounter MyCounter = meter
.counterBuilder("my_app_my_counter")
.build();
As long as we don't increment the value of the counter (with .add(1, ...)
for instance), the counter value is not visible to Prometheus scraping.
How to have the initial value (zero) visible to Prometheus from the beginning?
For completeness, here's the code we use to expose metrics to Prometheus thanks to OpenTelemetry Prometheus module:
import io.opentelemetry.exporter.prometheus.PrometheusHttpServer
PrometheusHttpServer prometheusServer = ...
SdkMeterProvider meterProvider = SdkMeterProvider
.builder()
.setResource(...)
.registerMetricReader(prometheusServer)
.build();
OpenTelemetrySdk openTelemetrySdk = OpenTelemetrySdk
.builder()
.setMeterProvider(meterProvider)
.buildAndRegisterGlobal();
Most likely counter is initialized only once first access is happening.
So, the easiest way to initialize it is to invoke
.add
method after metric creation with value equal to 0.