Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a metric for current computation count #96

Merged
merged 3 commits into from
Jan 7, 2025
Merged
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 @@ -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
Loading