Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

/**
* Uploads all Micrometer metrics with full tag dimensions to a Cosmos DB container.
Expand Down Expand Up @@ -82,6 +83,7 @@ public class CosmosMetricsReporter {
private final CpuMemoryReader cpuReader;
private final ScheduledExecutorService scheduler;
private final boolean enabled;
private final AtomicBoolean isStopped;

private CosmosMetricsReporter(
MeterRegistry micrometerRegistry,
Expand All @@ -95,6 +97,7 @@ private CosmosMetricsReporter(
this.commitId = reporterConfig.getCommitId();
this.concurrency = concurrency;
this.cpuReader = new CpuMemoryReader();
this.isStopped = new AtomicBoolean(false);

this.micrometerRegistry = micrometerRegistry;
this.cosmosClient = new CosmosClientBuilder()
Expand Down Expand Up @@ -128,8 +131,12 @@ public void start(long interval, TimeUnit unit) {
}

public void report() {
if (!enabled) return;
if (!enabled || isStopped.get()) return;

reportCore();
}

private void reportCore() {
String timestamp = TIMESTAMP_FORMATTER.format(Instant.now());
double cpuPercent = round(cpuReader.getSystemWideCpuUsage() * 100);

Comment on lines 140 to 142
Expand All @@ -153,7 +160,9 @@ public void report() {
}

public void stop() {
if (!enabled) return;
if (!enabled || !isStopped.compareAndSet(false, true)) return;

reportCore();

scheduler.shutdown();
try {
Expand All @@ -162,8 +171,6 @@ public void stop() {
Thread.currentThread().interrupt();
}

report();

if (cosmosClient != null) {
cosmosClient.close();
}
Expand Down