Skip to content

Commit

Permalink
Support synchronously metrics publishing
Browse files Browse the repository at this point in the history
  • Loading branch information
evg-tso committed Sep 19, 2024
1 parent 5c15a1f commit 0533c26
Showing 1 changed file with 13 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import java.util.concurrent.ForkJoinPool;
Expand Down Expand Up @@ -45,6 +46,13 @@ public OtelMetricPublisher(OpenTelemetry openTelemetry, String metricPrefix) {
}

public OtelMetricPublisher(OpenTelemetry openTelemetry, String metricPrefix, Executor executor) {
Objects.requireNonNull(metricPrefix, "metricPrefix must not be null");
Objects.requireNonNull(openTelemetry, "openTelemetry must not be null");

if (executor == null) {
log.warn("An executor is not provided. The metrics will be published synchronously on the calling thread.");
}

this.metricPrefix = metricPrefix + ".";
this.executor = executor;

Expand All @@ -57,6 +65,11 @@ public OtelMetricPublisher(OpenTelemetry openTelemetry, String metricPrefix, Exe

@Override
public void publish(MetricCollection metricCollection) {
if (executor == null) {
publishInternal(metricCollection);
return;
}

try {
executor.execute(() -> publishInternal(metricCollection));
} catch (RejectedExecutionException ex) {
Expand Down

0 comments on commit 0533c26

Please sign in to comment.