From a62af2a05cb895cf6611a2bd3794c2a4b7b30554 Mon Sep 17 00:00:00 2001
From: elopezcastro <82950460+elopezcastro@users.noreply.github.com>
Date: Fri, 27 Oct 2023 14:49:55 +0200
Subject: [PATCH] explicitely set resource metrics for better perfomance (#413)
---
.../guice/CoreDataResourcesModule.java | 13 +++++-
.../pom.xml | 16 --------
.../api/metrics/PrometheusMetricsHandler.java | 2 +-
.../metrics/VoidPrometheusMetricsHandler.java | 2 +-
.../DepotPrometheusMetricsHandler.java | 41 +++++--------------
.../tracing/resources/TracingResource.java | 1 -
.../metrics/TestResourceApiTracing.java | 5 +--
.../guice/EntitiesResourcesModule.java | 13 +++++-
.../guice/GenerationsResourcesModule.java | 11 ++++-
.../PureModelContextResourcesModule.java | 11 ++++-
10 files changed, 55 insertions(+), 60 deletions(-)
diff --git a/legend-depot-core-data-services/src/main/java/org/finos/legend/depot/server/resources/guice/CoreDataResourcesModule.java b/legend-depot-core-data-services/src/main/java/org/finos/legend/depot/server/resources/guice/CoreDataResourcesModule.java
index 39446f137..a3a321418 100644
--- a/legend-depot-core-data-services/src/main/java/org/finos/legend/depot/server/resources/guice/CoreDataResourcesModule.java
+++ b/legend-depot-core-data-services/src/main/java/org/finos/legend/depot/server/resources/guice/CoreDataResourcesModule.java
@@ -18,8 +18,7 @@
import com.google.inject.PrivateModule;
import com.google.inject.Provides;
import com.google.inject.Singleton;
-import com.google.inject.name.Named;
-import org.finos.legend.depot.core.services.tracing.resources.TracingResource;
+import org.finos.legend.depot.core.services.api.metrics.PrometheusMetricsHandler;
import org.finos.legend.depot.server.resources.projects.ProjectsResource;
import org.finos.legend.depot.server.resources.versions.ProjectsVersionsResource;
import org.finos.legend.depot.server.resources.dependencies.DependenciesResource;
@@ -38,4 +37,14 @@ protected void configure()
expose(ProjectsVersionsResource.class);
expose(DependenciesResource.class);
}
+
+ @Singleton
+ @Provides
+ boolean registerResourceMetrics(PrometheusMetricsHandler metricsHandler)
+ {
+ metricsHandler.registerResourceSummaries(ProjectsResource.class);
+ metricsHandler.registerResourceSummaries(ProjectsVersionsResource.class);
+ metricsHandler.registerResourceSummaries(DependenciesResource.class);
+ return true;
+ }
}
\ No newline at end of file
diff --git a/legend-depot-core-schedules-store-mongo/pom.xml b/legend-depot-core-schedules-store-mongo/pom.xml
index 7046901bf..163b335ea 100644
--- a/legend-depot-core-schedules-store-mongo/pom.xml
+++ b/legend-depot-core-schedules-store-mongo/pom.xml
@@ -65,20 +65,4 @@
-
-
-
-
- org.apache.maven.plugins
- maven-jar-plugin
-
-
-
- test-jar
-
-
-
-
-
-
diff --git a/legend-depot-core-tracing/src/main/java/org/finos/legend/depot/core/services/api/metrics/PrometheusMetricsHandler.java b/legend-depot-core-tracing/src/main/java/org/finos/legend/depot/core/services/api/metrics/PrometheusMetricsHandler.java
index 714547104..5cf4b3730 100644
--- a/legend-depot-core-tracing/src/main/java/org/finos/legend/depot/core/services/api/metrics/PrometheusMetricsHandler.java
+++ b/legend-depot-core-tracing/src/main/java/org/finos/legend/depot/core/services/api/metrics/PrometheusMetricsHandler.java
@@ -49,5 +49,5 @@ public interface PrometheusMetricsHandler
void increaseGauge(String name, int value);
- void registerResourceSummaries(TracingResource baseResource);
+ void registerResourceSummaries(Class extends TracingResource> baseResource);
}
diff --git a/legend-depot-core-tracing/src/main/java/org/finos/legend/depot/core/services/api/metrics/VoidPrometheusMetricsHandler.java b/legend-depot-core-tracing/src/main/java/org/finos/legend/depot/core/services/api/metrics/VoidPrometheusMetricsHandler.java
index edf07692b..49dff72ff 100644
--- a/legend-depot-core-tracing/src/main/java/org/finos/legend/depot/core/services/api/metrics/VoidPrometheusMetricsHandler.java
+++ b/legend-depot-core-tracing/src/main/java/org/finos/legend/depot/core/services/api/metrics/VoidPrometheusMetricsHandler.java
@@ -48,7 +48,7 @@ public void registerSummary(String summaryName, String helpMessage)
}
@Override
- public void registerResourceSummaries(TracingResource baseResource)
+ public void registerResourceSummaries(Class extends TracingResource> baseResource)
{
}
diff --git a/legend-depot-core-tracing/src/main/java/org/finos/legend/depot/core/services/metrics/DepotPrometheusMetricsHandler.java b/legend-depot-core-tracing/src/main/java/org/finos/legend/depot/core/services/metrics/DepotPrometheusMetricsHandler.java
index 34aea4de3..3eb34f5d5 100644
--- a/legend-depot-core-tracing/src/main/java/org/finos/legend/depot/core/services/metrics/DepotPrometheusMetricsHandler.java
+++ b/legend-depot-core-tracing/src/main/java/org/finos/legend/depot/core/services/metrics/DepotPrometheusMetricsHandler.java
@@ -16,7 +16,6 @@
package org.finos.legend.depot.core.services.metrics;
import com.fasterxml.jackson.annotation.JsonCreator;
-import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.sun.istack.NotNull;
import io.prometheus.client.Counter;
@@ -25,10 +24,7 @@
import io.prometheus.client.Summary;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
-import org.eclipse.collections.api.factory.Maps;
-import org.eclipse.collections.api.map.ConcurrentMutableMap;
import org.eclipse.collections.impl.map.mutable.ConcurrentHashMap;
-import org.eclipse.collections.impl.map.mutable.SynchronizedMutableMap;
import org.finos.legend.depot.core.services.api.metrics.PrometheusMetricsHandler;
import org.finos.legend.depot.core.services.tracing.resources.TracingResource;
@@ -44,20 +40,13 @@ public class DepotPrometheusMetricsHandler implements PrometheusMetricsHandler
private static final String DURATION = " duration";
private static final String GAUGE = " gauge";
private static final String HISTOGRAM = " histogram";
- private static final String WHITE_SPACE = " ";
- private static final String BLANK = "";
- private static final String SLASH = "/";
- private static final String HYPHEN = "-";
- private static final String OPEN_CURLY = "{";
- private static final String CLOSE_CURLY = "}";
- private static final String DOT = ".";
-
- final SynchronizedMutableMap allCounters = new SynchronizedMutableMap(Maps.mutable.empty());
- final SynchronizedMutableMap allErrorCounters = new SynchronizedMutableMap(Maps.mutable.empty());
- final SynchronizedMutableMap allSummaries = new SynchronizedMutableMap(Maps.mutable.empty());
- final SynchronizedMutableMap allGauges = new SynchronizedMutableMap(Maps.mutable.empty());
- final SynchronizedMutableMap allHistograms = new SynchronizedMutableMap(Maps.mutable.empty());
- final SynchronizedMutableMap resourceMetricsRegistration = new SynchronizedMutableMap(Maps.mutable.empty());
+ private static final String WHITE_SPACE = "\\s";
+
+ final ConcurrentHashMap allCounters = new ConcurrentHashMap<>();
+ final ConcurrentHashMap allErrorCounters = new ConcurrentHashMap();
+ final ConcurrentHashMap allSummaries = new ConcurrentHashMap();
+ final ConcurrentHashMap allGauges = new ConcurrentHashMap();
+ final ConcurrentHashMap allHistograms = new ConcurrentHashMap();
@JsonProperty
@NotNull
@@ -71,7 +60,7 @@ public DepotPrometheusMetricsHandler(@JsonProperty("prefix")String prefix)
private String getKeyName(String name)
{
- return sanitise(this.prefix + METRIC_SEPARATOR + StringUtils.lowerCase(name));
+ return sanitise(new StringBuffer().append(this.prefix).append(METRIC_SEPARATOR).append(StringUtils.lowerCase(name)).toString());
}
private String getHelpMessage(String metricName, String helpMessage)
@@ -81,11 +70,7 @@ private String getHelpMessage(String metricName, String helpMessage)
private String sanitise(String name)
{
- return name.replace(SLASH, METRIC_SEPARATOR)
- .replace(HYPHEN, METRIC_SEPARATOR)
- .replace(DOT, METRIC_SEPARATOR)
- .replace(OPEN_CURLY, BLANK).replace(CLOSE_CURLY, BLANK)
- .replaceAll(WHITE_SPACE, METRIC_SEPARATOR);
+ return name.replaceAll(WHITE_SPACE, METRIC_SEPARATOR);
}
private String buildErrorCounterName(String counterName)
@@ -222,11 +207,9 @@ public void observeHistogram(String name, long start, long end)
}
@Override
- public void registerResourceSummaries(TracingResource baseResource)
+ public void registerResourceSummaries(Class extends TracingResource> baseResource)
{
- resourceMetricsRegistration.getIfAbsentPutWithKey(baseResource.getClass().getCanonicalName(), resourceName ->
- {
- Arrays.stream(baseResource.getClass().getMethods()).forEach(m ->
+ Arrays.stream(baseResource.getMethods()).forEach(m ->
{
if (m.isAnnotationPresent(ApiOperation.class))
{
@@ -235,7 +218,5 @@ public void registerResourceSummaries(TracingResource baseResource)
PrometheusMetricsFactory.getInstance().registerSummary(metricName, metricName);
}
});
- return resourceName;
- });
}
}
diff --git a/legend-depot-core-tracing/src/main/java/org/finos/legend/depot/core/services/tracing/resources/TracingResource.java b/legend-depot-core-tracing/src/main/java/org/finos/legend/depot/core/services/tracing/resources/TracingResource.java
index 8369034ca..e271beb93 100644
--- a/legend-depot-core-tracing/src/main/java/org/finos/legend/depot/core/services/tracing/resources/TracingResource.java
+++ b/legend-depot-core-tracing/src/main/java/org/finos/legend/depot/core/services/tracing/resources/TracingResource.java
@@ -38,7 +38,6 @@ public class TracingResource
public TracingResource()
{
- PrometheusMetricsFactory.getInstance().registerResourceSummaries(this);
}
private Logger getLogger()
diff --git a/legend-depot-core-tracing/src/test/java/org/finos/legend/depot/core/services/metrics/TestResourceApiTracing.java b/legend-depot-core-tracing/src/test/java/org/finos/legend/depot/core/services/metrics/TestResourceApiTracing.java
index 570a439c2..fa1187544 100644
--- a/legend-depot-core-tracing/src/test/java/org/finos/legend/depot/core/services/metrics/TestResourceApiTracing.java
+++ b/legend-depot-core-tracing/src/test/java/org/finos/legend/depot/core/services/metrics/TestResourceApiTracing.java
@@ -26,9 +26,8 @@ public class TestResourceApiTracing
@Test
public void testMetricRegistrationForResource()
{
- new TestTracingResource();
- Assert.assertEquals(1,prometheusMetrics.allSummaries.keySet().size());
- new TestTracingResource();
+ prometheusMetrics.registerResourceSummaries(TestTracingResource.class);
Assert.assertEquals(1,prometheusMetrics.allSummaries.keySet().size());
+
}
}
diff --git a/legend-depot-entities-services/src/main/java/org/finos/legend/depot/server/resources/guice/EntitiesResourcesModule.java b/legend-depot-entities-services/src/main/java/org/finos/legend/depot/server/resources/guice/EntitiesResourcesModule.java
index a4f8f1195..d1e4e3627 100644
--- a/legend-depot-entities-services/src/main/java/org/finos/legend/depot/server/resources/guice/EntitiesResourcesModule.java
+++ b/legend-depot-entities-services/src/main/java/org/finos/legend/depot/server/resources/guice/EntitiesResourcesModule.java
@@ -18,8 +18,7 @@
import com.google.inject.PrivateModule;
import com.google.inject.Provides;
import com.google.inject.Singleton;
-import com.google.inject.name.Named;
-import org.finos.legend.depot.core.services.tracing.resources.TracingResource;
+import org.finos.legend.depot.core.services.api.metrics.PrometheusMetricsHandler;
import org.finos.legend.depot.server.resources.entities.EntitiesDependenciesResource;
import org.finos.legend.depot.server.resources.entities.EntitiesResource;
import org.finos.legend.depot.server.resources.entities.EntityClassifierResource;
@@ -37,4 +36,14 @@ protected void configure()
expose(EntitiesDependenciesResource.class);
expose(EntityClassifierResource.class);
}
+
+ @Singleton
+ @Provides
+ boolean registerResourceMetrics(PrometheusMetricsHandler metricsHandler)
+ {
+ metricsHandler.registerResourceSummaries(EntitiesResource.class);
+ metricsHandler.registerResourceSummaries(EntitiesDependenciesResource.class);
+ metricsHandler.registerResourceSummaries(EntityClassifierResource.class);
+ return true;
+ }
}
\ No newline at end of file
diff --git a/legend-depot-generations-services/src/main/java/org/finos/legend/depot/server/resources/guice/GenerationsResourcesModule.java b/legend-depot-generations-services/src/main/java/org/finos/legend/depot/server/resources/guice/GenerationsResourcesModule.java
index e926faf77..46d2f8626 100644
--- a/legend-depot-generations-services/src/main/java/org/finos/legend/depot/server/resources/guice/GenerationsResourcesModule.java
+++ b/legend-depot-generations-services/src/main/java/org/finos/legend/depot/server/resources/guice/GenerationsResourcesModule.java
@@ -18,8 +18,7 @@
import com.google.inject.PrivateModule;
import com.google.inject.Provides;
import com.google.inject.Singleton;
-import com.google.inject.name.Named;
-import org.finos.legend.depot.core.services.tracing.resources.TracingResource;
+import org.finos.legend.depot.core.services.api.metrics.PrometheusMetricsHandler;
import org.finos.legend.depot.server.resources.generations.FileGenerationsResource;
@@ -31,4 +30,12 @@ protected void configure()
bind(FileGenerationsResource.class);
expose(FileGenerationsResource.class);
}
+
+ @Singleton
+ @Provides
+ boolean registerResourceMetrics(PrometheusMetricsHandler metricsHandler)
+ {
+ metricsHandler.registerResourceSummaries(FileGenerationsResource.class);
+ return true;
+ }
}
diff --git a/legend-depot-pure-model-context/src/main/java/org/finos/legend/depot/server/resources/guice/PureModelContextResourcesModule.java b/legend-depot-pure-model-context/src/main/java/org/finos/legend/depot/server/resources/guice/PureModelContextResourcesModule.java
index 9ffa907bc..9e940749d 100644
--- a/legend-depot-pure-model-context/src/main/java/org/finos/legend/depot/server/resources/guice/PureModelContextResourcesModule.java
+++ b/legend-depot-pure-model-context/src/main/java/org/finos/legend/depot/server/resources/guice/PureModelContextResourcesModule.java
@@ -18,8 +18,7 @@
import com.google.inject.PrivateModule;
import com.google.inject.Provides;
import com.google.inject.Singleton;
-import com.google.inject.name.Named;
-import org.finos.legend.depot.core.services.tracing.resources.TracingResource;
+import org.finos.legend.depot.core.services.api.metrics.PrometheusMetricsHandler;
import org.finos.legend.depot.server.resources.pure.model.context.PureModelContextResource;
public class PureModelContextResourcesModule extends PrivateModule
@@ -30,4 +29,12 @@ protected void configure()
bind(PureModelContextResource.class);
expose(PureModelContextResource.class);
}
+
+ @Singleton
+ @Provides
+ boolean registerResourceMetrics(PrometheusMetricsHandler metricsHandler)
+ {
+ metricsHandler.registerResourceSummaries(PureModelContextResource.class);
+ return true;
+ }
}