io.opentelemetry.contrib.metrics.micrometer.MicrometerMeterProviderBuilder Maven / Gradle / Ivy
Show all versions of opentelemetry-micrometer-meter-provider Show documentation
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.contrib.metrics.micrometer;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import io.micrometer.core.instrument.MeterRegistry;
import io.opentelemetry.contrib.metrics.micrometer.internal.PollingMeterCallbackRegistrar;
import java.util.function.Supplier;
import javax.annotation.Nullable;
/** Builder utility class for creating instances of {@link MicrometerMeterProvider}. */
public class MicrometerMeterProviderBuilder {
private final Supplier meterRegistrySupplier;
@Nullable private CallbackRegistrar callbackRegistrar;
MicrometerMeterProviderBuilder(Supplier meterRegistrySupplier) {
this.meterRegistrySupplier = meterRegistrySupplier;
}
/**
* Sets the {@link CallbackRegistrar} used to poll asynchronous instruments for measurements.
*
* If this is not set the {@link MicrometerMeterProvider} will create a {@link
* io.micrometer.core.instrument.Meter} which will poll asynchronous instruments when that meter
* is measured.
*/
@CanIgnoreReturnValue
public MicrometerMeterProviderBuilder setCallbackRegistrar(CallbackRegistrar callbackRegistrar) {
this.callbackRegistrar = callbackRegistrar;
return this;
}
/**
* Constructs a new instance of the provider based on the builder's values.
*
* @return a new provider's instance.
*/
public MicrometerMeterProvider build() {
CallbackRegistrar callbackRegistrar = this.callbackRegistrar;
if (callbackRegistrar == null) {
callbackRegistrar = new PollingMeterCallbackRegistrar(meterRegistrySupplier);
}
return new MicrometerMeterProvider(meterRegistrySupplier, callbackRegistrar);
}
}