From 41592ec732b691032c1c47b500ad568cb8a93ef2 Mon Sep 17 00:00:00 2001 From: Jey Puget Gil Date: Thu, 7 Nov 2024 10:18:48 +0100 Subject: [PATCH] Moved QuaDer metrics exporter to Singleton --- .../fr/vcity/converg/MetricsSingleton.java | 74 +++++++++++++++++++ .../vcity/converg/QuadsLoaderApplication.java | 18 +---- .../converg/services/QuadImportService.java | 37 ++-------- 3 files changed, 84 insertions(+), 45 deletions(-) create mode 100644 quads-loader/src/main/java/fr/vcity/converg/MetricsSingleton.java diff --git a/quads-loader/src/main/java/fr/vcity/converg/MetricsSingleton.java b/quads-loader/src/main/java/fr/vcity/converg/MetricsSingleton.java new file mode 100644 index 0000000..9a81442 --- /dev/null +++ b/quads-loader/src/main/java/fr/vcity/converg/MetricsSingleton.java @@ -0,0 +1,74 @@ +package fr.vcity.converg; + +import io.prometheus.metrics.core.metrics.Counter; +import io.prometheus.metrics.core.metrics.Summary; +import io.prometheus.metrics.model.snapshots.Unit; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Objects; + +public class MetricsSingleton { + + private static final Logger log = LoggerFactory.getLogger(MetricsSingleton.class); + + private static MetricsSingleton metricsSingleton; + + public Counter counterVersionTotal; + + public Summary summaryVersionBatchingDuration; + + public Summary summaryVersionCatalogDuration; + + public Summary summaryVersionCondensingDuration; + + /** + * Constructor method in order to create db connection & statement + */ + private MetricsSingleton() { + counterVersionTotal = + Counter.builder() + .name("version_count_total") + .help("number of version that have been imported in the database") + .register(); + + summaryVersionBatchingDuration = + Summary.builder() + .name("file_at_version_batching_duration_seconds") + .help("duration of the file batching in seconds at a certain version") + .unit(Unit.SECONDS) + .labelNames("filename", "version") + .register(); + + summaryVersionCatalogDuration = + Summary.builder() + .name("file_at_version_catalog_duration_seconds") + .help("duration of the file catalog in seconds at a certain version") + .unit(Unit.SECONDS) + .labelNames("filename", "version") + .register(); + + summaryVersionCondensingDuration = + Summary.builder() + .name("file_at_version_condensing_duration_seconds") + .help("duration of the file condensing in seconds at a certain version") + .unit(Unit.SECONDS) + .labelNames("filename", "version") + .register(); + } + + /** + * Create the instance of {@link MetricsSingleton} object if it is not created yet and guarantee that there is only one single instance is created for this class. + * + * @return MetricsSingleton created single instance + */ + public static MetricsSingleton getInstance() { + log.info("MetricsSingleton.getInstance()"); + + if (Objects.isNull(metricsSingleton)) { + metricsSingleton = new MetricsSingleton(); + } + + return metricsSingleton; + } +} \ No newline at end of file diff --git a/quads-loader/src/main/java/fr/vcity/converg/QuadsLoaderApplication.java b/quads-loader/src/main/java/fr/vcity/converg/QuadsLoaderApplication.java index c9c359a..1ef139c 100644 --- a/quads-loader/src/main/java/fr/vcity/converg/QuadsLoaderApplication.java +++ b/quads-loader/src/main/java/fr/vcity/converg/QuadsLoaderApplication.java @@ -1,9 +1,7 @@ package fr.vcity.converg; -import io.prometheus.metrics.core.metrics.Counter; -import io.prometheus.metrics.instrumentation.jvm.JvmMetrics; import io.prometheus.metrics.exporter.httpserver.HTTPServer; -import io.prometheus.metrics.model.snapshots.Unit; +import io.prometheus.metrics.instrumentation.jvm.JvmMetrics; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @@ -12,7 +10,7 @@ @SpringBootApplication public class QuadsLoaderApplication { - public static void main(String[] args) throws IOException, InterruptedException { + public static void main(String[] args) throws IOException { SpringApplication.run(QuadsLoaderApplication.class, args); JvmMetrics.builder().register(); @@ -21,19 +19,7 @@ public static void main(String[] args) throws IOException, InterruptedException .port(9400) .buildAndStart(); - Counter counter = - Counter.builder() - .name("uptime_seconds_total") - .help("total number of seconds since this application was started") - .unit(Unit.SECONDS) - .register(); - System.out.println( "HTTPServer listening on port:" + server.getPort() + "/metrics"); - - while (true) { - Thread.sleep(1000); - counter.inc(); - } } } diff --git a/quads-loader/src/main/java/fr/vcity/converg/services/QuadImportService.java b/quads-loader/src/main/java/fr/vcity/converg/services/QuadImportService.java index da40e4e..a437ee1 100644 --- a/quads-loader/src/main/java/fr/vcity/converg/services/QuadImportService.java +++ b/quads-loader/src/main/java/fr/vcity/converg/services/QuadImportService.java @@ -1,5 +1,6 @@ package fr.vcity.converg.services; +import fr.vcity.converg.MetricsSingleton; import fr.vcity.converg.dao.ResourceOrLiteral; import fr.vcity.converg.dao.Version; import fr.vcity.converg.repository.*; @@ -73,35 +74,13 @@ public record QuadValueType(TripleValueType tripleValueType, String namedGraph, Set namedGraphs = new HashSet<>(); List tripleValueTypes = new ArrayList<>(); - Counter counterVersionTotal = - Counter.builder() - .name("version_count_total") - .help("number of version that have been imported in the database") - .register(); - - Summary summaryVersionBatchingDuration = - Summary.builder() - .name("file_at_version_batching_duration_seconds") - .help("duration of the file batching in seconds at a certain version") - .unit(Unit.SECONDS) - .labelNames("filename", "version") - .register(); - - Summary summaryVersionCatalogDuration = - Summary.builder() - .name("file_at_version_catalog_duration_seconds") - .help("duration of the file catalog in seconds at a certain version") - .unit(Unit.SECONDS) - .labelNames("filename", "version") - .register(); - - Summary summaryVersionCondensingDuration = - Summary.builder() - .name("file_at_version_condensing_duration_seconds") - .help("duration of the file condensing in seconds at a certain version") - .unit(Unit.SECONDS) - .labelNames("filename", "version") - .register(); + Counter counterVersionTotal = MetricsSingleton.getInstance().counterVersionTotal; + + Summary summaryVersionBatchingDuration = MetricsSingleton.getInstance().summaryVersionBatchingDuration; + + Summary summaryVersionCatalogDuration = MetricsSingleton.getInstance().summaryVersionCatalogDuration; + + Summary summaryVersionCondensingDuration = MetricsSingleton.getInstance().summaryVersionCondensingDuration; public QuadImportService( IFlatModelQuadRepository flatModelQuadRepository,