Skip to content

Commit

Permalink
Add metrics observation
Browse files Browse the repository at this point in the history
Signed-off-by: Florent MILLOT <[email protected]>
  • Loading branch information
flomillot committed Dec 13, 2023
1 parent 2cb0e40 commit 7cf52ad
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 11 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,11 @@
<artifactId>spring-boot-starter-actuator</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<scope>runtime</scope>
</dependency>

<!-- Test dependencies -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Copyright (c) 2023, 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 org.gridsuite.sensitivityanalysis.server.service;

import io.micrometer.observation.Observation;
import io.micrometer.observation.ObservationRegistry;
import lombok.NonNull;
import org.springframework.stereotype.Service;

/**
* @author Florent MILLOT <florent.millot at rte-france.com>
*/
@Service
public class SensitivityAnalysisObserver {

private final ObservationRegistry observationRegistry;

private static final String OBSERVATION_PREFIX = "app.sensitivity.analysis.";
private static final String PROVIDER_TAG_NAME = "provider";

public SensitivityAnalysisObserver(@NonNull ObservationRegistry observationRegistry) {
this.observationRegistry = observationRegistry;
}

public <E extends Throwable> void observe(String name, SensitivityAnalysisRunContext runContext, Observation.CheckedRunnable<E> callable) throws E {
createSensitivityAnalysisObservation(name, runContext).observeChecked(callable);
}

public <T, E extends Throwable> T observe(String name, SensitivityAnalysisRunContext runContext, Observation.CheckedCallable<T, E> callable) throws E {
return createSensitivityAnalysisObservation(name, runContext).observeChecked(callable);
}

private Observation createSensitivityAnalysisObservation(String name, SensitivityAnalysisRunContext runContext) {
return Observation.createNotStarted(OBSERVATION_PREFIX + name, observationRegistry)
.lowCardinalityKeyValue(PROVIDER_TAG_NAME, runContext.getProvider());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,16 @@ public class SensitivityAnalysisWorkerService {

private final SensitivityAnalysisInputBuilderService sensitivityAnalysisInputBuilderService;

private final SensitivityAnalysisObserver sensitivityAnalysisObserver;

private Function<String, SensitivityAnalysis.Runner> sensitivityAnalysisFactorySupplier;

public SensitivityAnalysisWorkerService(NetworkStoreService networkStoreService, ReportService reportService, NotificationService notificationService,
SensitivityAnalysisInputBuilderService sensitivityAnalysisInputBuilderService,
SensitivityAnalysisExecutionService sensitivityAnalysisExecutionService,
SensitivityAnalysisResultRepository resultRepository, ObjectMapper objectMapper,
SensitivityAnalysisRunnerSupplier sensitivityAnalysisRunnerSupplier) {
SensitivityAnalysisRunnerSupplier sensitivityAnalysisRunnerSupplier,
SensitivityAnalysisObserver sensitivityAnalysisObserver) {
this.networkStoreService = Objects.requireNonNull(networkStoreService);
this.reportService = Objects.requireNonNull(reportService);
this.notificationService = notificationService;
Expand All @@ -89,6 +92,7 @@ public SensitivityAnalysisWorkerService(NetworkStoreService networkStoreService,
this.resultRepository = Objects.requireNonNull(resultRepository);
this.objectMapper = Objects.requireNonNull(objectMapper);
sensitivityAnalysisFactorySupplier = sensitivityAnalysisRunnerSupplier::getRunner;
this.sensitivityAnalysisObserver = sensitivityAnalysisObserver;
}

public void setSensitivityAnalysisFactorySupplier(Function<String, SensitivityAnalysis.Runner> sensitivityAnalysisFactorySupplier) {
Expand Down Expand Up @@ -119,29 +123,31 @@ public SensitivityAnalysisResult run(SensitivityAnalysisRunContext context) {
}
}

private SensitivityAnalysisResult run(SensitivityAnalysisRunContext context, UUID resultUuid) throws ExecutionException, InterruptedException {
private SensitivityAnalysisResult run(SensitivityAnalysisRunContext context, UUID resultUuid) throws Exception {
Objects.requireNonNull(context);

LOGGER.info("Run sensitivity analysis");

SensitivityAnalysis.Runner sensitivityAnalysisRunner = sensitivityAnalysisFactorySupplier.apply(context.getProvider());

Reporter rootReporter = Reporter.NO_OP;
AtomicReference<Reporter> rootReporter = new AtomicReference<>(Reporter.NO_OP);
Reporter reporter = Reporter.NO_OP;
if (context.getReportUuid() != null) {
final String reportType = context.getReportType();
String rootReporterId = context.getReporterId() == null ? reportType : context.getReporterId() + "@" + reportType;
rootReporter = new ReporterModel(rootReporterId, rootReporterId);
reporter = rootReporter.createSubReporter(reportType, reportType + " (${providerToUse})", "providerToUse", sensitivityAnalysisRunner.getName());
rootReporter.set(new ReporterModel(rootReporterId, rootReporterId));
reporter = rootReporter.get().createSubReporter(reportType, reportType + " (${providerToUse})", "providerToUse", sensitivityAnalysisRunner.getName());
// Delete any previous sensi computation logs
reportService.deleteReport(context.getReportUuid(), reportType);
sensitivityAnalysisObserver.observe("report.delete", context, () ->
reportService.deleteReport(context.getReportUuid(), reportType));
}

CompletableFuture<SensitivityAnalysisResult> future = runSensitivityAnalysisAsync(context, sensitivityAnalysisRunner, reporter, resultUuid);

SensitivityAnalysisResult result = future == null ? null : future.get();
SensitivityAnalysisResult result = future == null ? null : sensitivityAnalysisObserver.observe("run", context, () -> future.get());
if (context.getReportUuid() != null) {
reportService.sendReport(context.getReportUuid(), rootReporter);
sensitivityAnalysisObserver.observe("report.send", context, () ->
reportService.sendReport(context.getReportUuid(), rootReporter.get()));
}
return result;
}
Expand Down Expand Up @@ -172,7 +178,8 @@ private CompletableFuture<SensitivityAnalysisResult> runSensitivityAnalysisAsync
return null;
}
SensitivityAnalysisParameters sensitivityAnalysisParameters = buildParameters(context);
Network network = getNetwork(context.getNetworkUuid(), context.getVariantId());
Network network = sensitivityAnalysisObserver.observe("network.load", context, () ->
getNetwork(context.getNetworkUuid(), context.getVariantId()));
sensitivityAnalysisInputBuilderService.build(context, network, reporter);

CompletableFuture<SensitivityAnalysisResult> future = sensitivityAnalysisRunner.runAsync(
Expand Down Expand Up @@ -228,7 +235,8 @@ public Consumer<Message<String>> consumeRun() {
long nanoTime = System.nanoTime();
LOGGER.info("Just run in {}s", TimeUnit.NANOSECONDS.toSeconds(nanoTime - startTime.getAndSet(nanoTime)));

resultRepository.insert(resultContext.getResultUuid(), result, SensitivityAnalysisStatus.COMPLETED.name());
sensitivityAnalysisObserver.observe("results.save", resultContext.getRunContext(), () ->
resultRepository.insert(resultContext.getResultUuid(), result, SensitivityAnalysisStatus.COMPLETED.name()));
long finalNanoTime = System.nanoTime();
LOGGER.info("Stored in {}s", TimeUnit.NANOSECONDS.toSeconds(finalNanoTime - startTime.getAndSet(finalNanoTime)));

Expand Down
11 changes: 10 additions & 1 deletion src/main/resources/application-local.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,13 @@ gridsuite:
base-uri: http://localhost:5027
report-server:
base-uri: http://localhost:5028


management:
metrics:
distribution:
percentiles-histogram:
http.server.requests: true
endpoints:
web:
exposure:
include: prometheus, health, info

0 comments on commit 7cf52ad

Please sign in to comment.