com.powsybl.ws.commons.computation.service.AbstractComputationObserver Maven / Gradle / Ivy
/**
* Copyright (c) 2024, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package com.powsybl.ws.commons.computation.service;
import io.micrometer.core.instrument.Counter;
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;
/**
* @author Mathieu Deharbe
* @param powsybl Result class specific to the computation
* @param powsybl and gridsuite parameters specifics to the computation
*/
@Getter(AccessLevel.PROTECTED)
public abstract class AbstractComputationObserver {
protected static final String OBSERVATION_PREFIX = "app.computation.";
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";
private final ObservationRegistry observationRegistry;
private final MeterRegistry meterRegistry;
protected AbstractComputationObserver(@NonNull ObservationRegistry observationRegistry, @NonNull MeterRegistry meterRegistry) {
this.observationRegistry = observationRegistry;
this.meterRegistry = meterRegistry;
}
protected abstract String getComputationType();
protected Observation createObservation(String name, AbstractComputationRunContext runContext) {
Observation observation = Observation.createNotStarted(OBSERVATION_PREFIX + name, observationRegistry)
.lowCardinalityKeyValue(TYPE_TAG_NAME, getComputationType());
if (runContext.getProvider() != null) {
observation.lowCardinalityKeyValue(PROVIDER_TAG_NAME, runContext.getProvider());
}
return observation;
}
public void observe(String name, AbstractComputationRunContext runContext, Observation.CheckedRunnable callable) throws E {
createObservation(name, runContext).observeChecked(callable);
}
public T observe(String name, AbstractComputationRunContext runContext, Observation.CheckedCallable callable) throws E {
return createObservation(name, runContext).observeChecked(callable);
}
public T observeRun(
String name, AbstractComputationRunContext runContext, Observation.CheckedCallable callable) throws E {
T result = createObservation(name, runContext).observeChecked(callable);
incrementCount(runContext, result);
return result;
}
private void incrementCount(AbstractComputationRunContext runContext, R result) {
Counter.Builder builder =
Counter.builder(COMPUTATION_COUNTER_NAME);
if (runContext.getProvider() != null) {
builder.tag(PROVIDER_TAG_NAME, runContext.getProvider());
}
builder.tag(TYPE_TAG_NAME, getComputationType())
.tag(STATUS_TAG_NAME, getResultStatus(result))
.register(meterRegistry)
.increment();
}
protected abstract String getResultStatus(R res);
}