Skip to content

Commit

Permalink
Only setup metrics once
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-richardson committed Nov 27, 2024
1 parent 5610c02 commit 232c600
Showing 1 changed file with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.time.Duration;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

public class OTELHelperImpl implements OTELHelper {
static Logger LOG = Logger.getLogger(OTELHelperImpl.class.getName());
Expand All @@ -34,14 +35,14 @@ public class OTELHelperImpl implements OTELHelper {
private final ConcurrentHashMap<String, Span> spanMap;
private final SdkTracerProvider sdkTracerProvider;
private final String helperName;
private static final AtomicBoolean metricsConfigured = new AtomicBoolean(false);

public OTELHelperImpl(
@NotNull SpanProcessor spanProcessor,
@Nullable MetricExporter metricExporter,
@NotNull String helperName) {
this.helperName = helperName;
Resource serviceNameResource = Resource
.create(Attributes.of(ResourceAttributes.SERVICE_NAME, PluginConstants.SERVICE_NAME));
var serviceNameResource = Resource.create(Attributes.of(ResourceAttributes.SERVICE_NAME, PluginConstants.SERVICE_NAME));

configureMetricsExport(metricExporter, serviceNameResource);

Expand All @@ -57,7 +58,10 @@ public OTELHelperImpl(
this.spanMap = new ConcurrentHashMap<>();
}

private static void configureMetricsExport(@Nullable MetricExporter metricExporter, Resource serviceNameResource) {
public static void configureMetricsExport(@Nullable MetricExporter metricExporter, Resource serviceNameResource) {
if (metricsConfigured.get()) return;
metricsConfigured.set(true);

var loggingMetricExporter = LoggingMetricExporter.create();
var meterProviderBuilder = SdkMeterProvider.builder()
.setResource(Resource.getDefault().merge(serviceNameResource))
Expand Down

0 comments on commit 232c600

Please sign in to comment.