ScalaConsultants / mesmer

OpenTelemetry agent for Scala applications
https://mesmer.io
Apache License 2.0
71 stars 12 forks source link

Attach openTelemetry metrics to zio.metric.Histogram #423

Open lgajowy opened 2 years ago

lgajowy commented 2 years ago

Proposal Link with code snippets on how to do that (see Part 2): here

ZIO defines Histograms internally (eg fibersStarted up/down counter). Similarily to Gauges and Counters we should collect histogram data. The caveat is that there is no async version of a histogram in opentelemetry yet, so periodic collection has to be implemented too (see proposal).

Goals:

NON-goals:

lgajowy commented 2 years ago

What's described in the proposal won't work due to the following:

import com.google.auto.service.AutoService;
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizer;
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider;
import io.opentelemetry.sdk.metrics.Aggregation;
import io.opentelemetry.sdk.metrics.InstrumentSelector;
import io.opentelemetry.sdk.metrics.View;

import java.util.Arrays;

@AutoService(AutoConfigurationCustomizerProvider.class)
public class HistogramConfigurer implements AutoConfigurationCustomizerProvider {

    @Override
    public void customize(AutoConfigurationCustomizer autoConfiguration) {
        System.err.println("*** HistogramConfigurer.customize() is called ***");
        autoConfiguration.addMeterProviderCustomizer((sdkMeterProviderBuilder, configProperties) -> sdkMeterProviderBuilder.registerView(
                InstrumentSelector.builder()
                        .setMeterName("http.server.duration")
                        .build(),
                View.builder()
                        .setAggregation(Aggregation.explicitBucketHistogram(Arrays.asList(1.0, 2.0, 3.0)))
                        .build()));
    }
}

If we want to instrument the Metric.histogram() method, we don't have a way of passing the histogram.Boundaries to the HistogramConfigurer (it will be already invoked). That problem could be mitigated with passing a proper config during agent startup that would allow users defining the boundaries for otel instruments. This however is not good user experience.

I'm currently looking for ways of attaching the synchronous histogram not to the histogram constructor but to the update function (otel's "record" equivalent).