diff --git a/src/main/java/org/gridsuite/loadflow/server/service/LoadFlowWorkerService.java b/src/main/java/org/gridsuite/loadflow/server/service/LoadFlowWorkerService.java index 5699f803..ba3d49a2 100644 --- a/src/main/java/org/gridsuite/loadflow/server/service/LoadFlowWorkerService.java +++ b/src/main/java/org/gridsuite/loadflow/server/service/LoadFlowWorkerService.java @@ -21,6 +21,8 @@ import com.powsybl.security.LimitViolation; import com.powsybl.security.LimitViolationType; import com.powsybl.security.Security; +import io.micrometer.observation.Observation; +import io.micrometer.observation.ObservationRegistry; import org.apache.commons.lang3.StringUtils; import org.gridsuite.loadflow.server.dto.LimitViolationInfos; import org.gridsuite.loadflow.server.repositories.LoadFlowResultRepository; @@ -34,7 +36,6 @@ import java.util.*; import java.util.concurrent.*; -import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; import java.util.function.Consumer; @@ -62,13 +63,16 @@ public class LoadFlowWorkerService { private LoadFlowResultRepository resultRepository; + private final ObservationRegistry observationRegistry; + public LoadFlowWorkerService(NetworkStoreService networkStoreService, NotificationService notificationService, ReportService reportService, - LoadFlowResultRepository resultRepository, ObjectMapper objectMapper) { + LoadFlowResultRepository resultRepository, ObjectMapper objectMapper, ObservationRegistry observationRegistry) { this.networkStoreService = networkStoreService; this.notificationService = notificationService; this.reportService = reportService; this.resultRepository = resultRepository; this.objectMapper = objectMapper; + this.observationRegistry = observationRegistry; } private Map> futures = new ConcurrentHashMap<>(); @@ -244,20 +248,28 @@ public Consumer> consumeRun() { LoadFlowResultContext resultContext = LoadFlowResultContext.fromMessage(message, objectMapper); try { runRequests.add(resultContext.getResultUuid()); - AtomicReference startTime = new AtomicReference<>(); - - startTime.set(System.nanoTime()); - Network network = getNetwork(resultContext.getRunContext().getNetworkUuid(), resultContext.getRunContext().getVariantId()); - - LoadFlowResult result = run(network, resultContext.getRunContext(), resultContext.getResultUuid()); - long nanoTime = System.nanoTime(); - LOGGER.info("Just run in {}s", TimeUnit.NANOSECONDS.toSeconds(nanoTime - startTime.getAndSet(nanoTime))); - List limitViolationInfos = getLimitViolations(network, resultContext.getRunContext()); - List limitViolationsWithCalculatedOverload = calculateOverloadLimitViolations(limitViolationInfos, network); - resultRepository.insert(resultContext.getResultUuid(), result, LoadFlowService.computeLoadFlowStatus(result), limitViolationsWithCalculatedOverload); - long finalNanoTime = System.nanoTime(); - LOGGER.info("Stored in {}s", TimeUnit.NANOSECONDS.toSeconds(finalNanoTime - startTime.getAndSet(finalNanoTime))); + Network network = Observation.createNotStarted("loadflow.load.network", observationRegistry) + .lowCardinalityKeyValue("userId", resultContext.getRunContext().getUserId()) + .lowCardinalityKeyValue("loadflow-provider", resultContext.getRunContext().getProvider()) + .contextualName("loadflow-load-network") + .observeChecked(() -> getNetwork(resultContext.getRunContext().getNetworkUuid(), resultContext.getRunContext().getVariantId())); + + LoadFlowResult result = Observation.createNotStarted("loadflow.run", observationRegistry) + .lowCardinalityKeyValue("userId", resultContext.getRunContext().getUserId()) + .lowCardinalityKeyValue("loadflow-provider", resultContext.getRunContext().getProvider()) + .contextualName("loadflow-run") + .observeChecked(() -> run(network, resultContext.getRunContext(), resultContext.getResultUuid())); + + Observation.createNotStarted("loadflow.save", observationRegistry) + .lowCardinalityKeyValue("userId", resultContext.getRunContext().getUserId()) + .lowCardinalityKeyValue("loadflow-provider", resultContext.getRunContext().getProvider()) + .contextualName("loadflow-save") + .observe(() -> { + List limitViolationInfos = getLimitViolations(network, resultContext.getRunContext()); + List limitViolationsWithCalculatedOverload = calculateOverloadLimitViolations(limitViolationInfos, network); + resultRepository.insert(resultContext.getResultUuid(), result, LoadFlowService.computeLoadFlowStatus(result), limitViolationsWithCalculatedOverload); + }); if (result != null) { // result available notificationService.sendResultMessage(resultContext.getResultUuid(), resultContext.getRunContext().getReceiver());