Skip to content

Commit

Permalink
Add a metric for current computation count (#96)
Browse files Browse the repository at this point in the history
Signed-off-by: Slimane AMAR <[email protected]>
  • Loading branch information
SlimaneAmar authored Jan 7, 2025
1 parent 59e62cc commit 7904666
Showing 1 changed file with 35 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@
package com.powsybl.ws.commons.computation.service;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.observation.Observation;
import io.micrometer.observation.ObservationRegistry;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NonNull;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
* @author Mathieu Deharbe <mathieu.deharbe at rte-france.com>
* @param <R> powsybl Result class specific to the computation
Expand All @@ -25,11 +29,14 @@ public abstract class AbstractComputationObserver<R, P> {
protected static final String PROVIDER_TAG_NAME = "provider";
protected static final String TYPE_TAG_NAME = "type";
protected static final String STATUS_TAG_NAME = "status";
protected static final String COMPUTATION_COUNTER_NAME = OBSERVATION_PREFIX + "count";
protected static final String COMPUTATION_TOTAL_COUNTER_NAME = OBSERVATION_PREFIX + "count";
protected static final String COMPUTATION_CURRENT_COUNTER_NAME = OBSERVATION_PREFIX + "current.count";

private final ObservationRegistry observationRegistry;
private final MeterRegistry meterRegistry;

private final Map<String, Integer> currentComputationsCount = new ConcurrentHashMap<>();

protected AbstractComputationObserver(@NonNull ObservationRegistry observationRegistry, @NonNull MeterRegistry meterRegistry) {
this.observationRegistry = observationRegistry;
this.meterRegistry = meterRegistry;
Expand All @@ -56,14 +63,37 @@ public <T, E extends Throwable> T observe(String name, AbstractComputationRunCon

public <T extends R, E extends Throwable> T observeRun(
String name, AbstractComputationRunContext<P> runContext, Observation.CheckedCallable<T, E> callable) throws E {
T result = createObservation(name, runContext).observeChecked(callable);
incrementCount(runContext, result);
T result;
try {
incrementCurrentCount(runContext.getProvider());
result = createObservation(name, runContext).observeChecked(callable);
} finally {
decrementCurrentCount(runContext.getProvider());
}
incrementTotalCount(runContext, result);
return result;
}

private void incrementCount(AbstractComputationRunContext<P> runContext, R result) {
private void incrementCurrentCount(String provider) {
currentComputationsCount.compute(provider, (k, v) -> (v == null) ? 1 : v + 1);
updateCurrentCountMetric(provider);
}

private void decrementCurrentCount(String provider) {
currentComputationsCount.compute(provider, (k, v) -> v > 1 ? v - 1 : 0);
updateCurrentCountMetric(provider);
}

private void updateCurrentCountMetric(String provider) {
Gauge.builder(COMPUTATION_CURRENT_COUNTER_NAME, () -> currentComputationsCount.get(provider))
.tag(TYPE_TAG_NAME, getComputationType())
.tag(PROVIDER_TAG_NAME, provider)
.register(meterRegistry);
}

private void incrementTotalCount(AbstractComputationRunContext<P> runContext, R result) {
Counter.Builder builder =
Counter.builder(COMPUTATION_COUNTER_NAME);
Counter.builder(COMPUTATION_TOTAL_COUNTER_NAME);
if (runContext.getProvider() != null) {
builder.tag(PROVIDER_TAG_NAME, runContext.getProvider());
}
Expand Down

0 comments on commit 7904666

Please sign in to comment.