Skip to content

Commit

Permalink
explicitely set resource metrics for better perfomance (#413)
Browse files Browse the repository at this point in the history
  • Loading branch information
elopezcastro authored Oct 27, 2023
1 parent 86dc67f commit a62af2a
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
}
16 changes: 0 additions & 16 deletions legend-depot-core-schedules-store-mongo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,4 @@


</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,5 @@ public interface PrometheusMetricsHandler

void increaseGauge(String name, int value);

void registerResourceSummaries(TracingResource baseResource);
void registerResourceSummaries(Class<? extends TracingResource> baseResource);
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void registerSummary(String summaryName, String helpMessage)
}

@Override
public void registerResourceSummaries(TracingResource baseResource)
public void registerResourceSummaries(Class<? extends TracingResource> baseResource)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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<String, Counter> allCounters = new SynchronizedMutableMap(Maps.mutable.empty());
final SynchronizedMutableMap<String, Counter> allErrorCounters = new SynchronizedMutableMap(Maps.mutable.empty());
final SynchronizedMutableMap<String, Summary> allSummaries = new SynchronizedMutableMap(Maps.mutable.empty());
final SynchronizedMutableMap<String, Gauge> allGauges = new SynchronizedMutableMap(Maps.mutable.empty());
final SynchronizedMutableMap<String, Histogram> allHistograms = new SynchronizedMutableMap(Maps.mutable.empty());
final SynchronizedMutableMap<String,String> resourceMetricsRegistration = new SynchronizedMutableMap(Maps.mutable.empty());
private static final String WHITE_SPACE = "\\s";

final ConcurrentHashMap<String, Counter> allCounters = new ConcurrentHashMap<>();
final ConcurrentHashMap<String, Counter> allErrorCounters = new ConcurrentHashMap();
final ConcurrentHashMap<String, Summary> allSummaries = new ConcurrentHashMap();
final ConcurrentHashMap<String, Gauge> allGauges = new ConcurrentHashMap();
final ConcurrentHashMap<String, Histogram> allHistograms = new ConcurrentHashMap();

@JsonProperty
@NotNull
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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))
{
Expand All @@ -235,7 +218,5 @@ public void registerResourceSummaries(TracingResource baseResource)
PrometheusMetricsFactory.getInstance().registerSummary(metricName, metricName);
}
});
return resourceName;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public class TracingResource

public TracingResource()
{
PrometheusMetricsFactory.getInstance().registerResourceSummaries(this);
}

private Logger getLogger()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
}

0 comments on commit a62af2a

Please sign in to comment.