diff --git a/commons/src/main/java/org/restheart/utils/PluginUtils.java b/commons/src/main/java/org/restheart/utils/PluginUtils.java index 30105ef870..2ce44db896 100644 --- a/commons/src/main/java/org/restheart/utils/PluginUtils.java +++ b/commons/src/main/java/org/restheart/utils/PluginUtils.java @@ -226,13 +226,12 @@ public static

String defaultURI(Class

serviceClass) { /** * * @param

- * @param conf the plugin configuration got from @InjectConfiguration + * @param conf the plugin configuration got from @Inject("conf") * @param serviceClass the class of the service * @return the actual service uri set in cofiguration or the defaultURI */ @SuppressWarnings("rawtypes") public static

String actualUri(Map conf, Class

serviceClass) { - if (conf != null && conf.get("uri") != null && conf.get("uri") instanceof String) { return (String) conf.get("uri"); } else { diff --git a/core/src/main/java/org/restheart/graal/PluginsReflectionRegistrationFeature.java b/core/src/main/java/org/restheart/graal/PluginsReflectionRegistrationFeature.java index 2adeee4558..863a44023e 100644 --- a/core/src/main/java/org/restheart/graal/PluginsReflectionRegistrationFeature.java +++ b/core/src/main/java/org/restheart/graal/PluginsReflectionRegistrationFeature.java @@ -102,8 +102,7 @@ private Field[] annotated(Field... fields) { * selects methods annotated with @OnInit * * @param fields - * @return an array of methods that are annotated - * with @OnInit, @InjectMongoClient, @InjectConfiguration, @InjectPluginsRegistry + * @return an array of methods that are annotated with @OnInit */ private Method[] annotated(Method... methods) { var list = Arrays.stream(methods) diff --git a/core/src/main/resources/restheart-default-config-no-mongodb.yml b/core/src/main/resources/restheart-default-config-no-mongodb.yml index b7ba9b75f4..163fe94602 100644 --- a/core/src/main/resources/restheart-default-config-no-mongodb.yml +++ b/core/src/main/resources/restheart-default-config-no-mongodb.yml @@ -460,11 +460,13 @@ logging: # - traceparent # vv opencensus.io headers, see https://github.com/w3c/distributed-tracing/blob/master/trace_context/HTTP_HEADER_FORMAT.md # - tracestate # ^^ -# Metrics -# see https://restheart.org/docs/metrics +# Metrics see https://restheart.org/docs/metrics +metrics: + enabled: true + uri: /metrics + requestsMetricsCollector: enabled: false - uri: /metrics include: ["/*"] exclude: ["/metrics", "/metrics/*"] diff --git a/core/src/main/resources/restheart-default-config.yml b/core/src/main/resources/restheart-default-config.yml index dbeafaa05a..29ff5000f2 100644 --- a/core/src/main/resources/restheart-default-config.yml +++ b/core/src/main/resources/restheart-default-config.yml @@ -445,11 +445,14 @@ logging: # - traceparent # vv opencensus.io headers, see https://github.com/w3c/distributed-tracing/blob/master/trace_context/HTTP_HEADER_FORMAT.md # - tracestate # ^^ -# Metrics -# see https://restheart.org/docs/metrics +# Metrics see https://restheart.org/docs/metrics + +metrics: + enabled: true + uri: /metrics + requestsMetricsCollector: enabled: false - uri: /metrics include: [ "/*" ] exclude: [ "/metrics", "/metrics/*" ] diff --git a/metrics/src/main/java/org/restheart/metrics/MetricsService.java b/metrics/src/main/java/org/restheart/metrics/MetricsService.java index df5597eb64..d3582a2cd5 100644 --- a/metrics/src/main/java/org/restheart/metrics/MetricsService.java +++ b/metrics/src/main/java/org/restheart/metrics/MetricsService.java @@ -20,9 +20,14 @@ */ package org.restheart.metrics; +import java.io.IOException; import java.io.StringWriter; +import java.util.Map; + import org.restheart.exchange.StringRequest; import org.restheart.exchange.StringResponse; +import org.restheart.plugins.Inject; +import org.restheart.plugins.OnInit; import org.restheart.plugins.RegisterPlugin; import org.restheart.plugins.StringService; import static org.restheart.utils.BsonUtils.array; @@ -38,7 +43,7 @@ * * @author Andrea Di Cesare {@literal } */ -@RegisterPlugin(name = "metrics", description = "returns requests metrics", secure = true) +@RegisterPlugin(name = "metrics", description = "returns requests metrics", secure = true, defaultURI="/metrics") public class MetricsService implements StringService { /** * @@ -46,13 +51,22 @@ public class MetricsService implements StringService { */ public static String METRICS_REGISTRIES_PREFIX = "METRICS-"; + @Inject("config") + private Map config; + private String serviceUri = "/metrics"; + + @OnInit + public void onInit() { + this.serviceUri = argOrDefault(config, "uri", "/metrics"); + } + /** - * - * @param exchange - * @throws Exception + * @param request + * @param response + * @throws java.io.IOException */ @Override - public void handle(StringRequest request, StringResponse response) throws Exception { + public void handle(StringRequest request, StringResponse response) throws IOException { if (request.isOptions()) { handleOptions(request); return; @@ -61,7 +75,7 @@ public void handle(StringRequest request, StringResponse response) throws Except return; } - var params = request.getPathParams("/{servicename}/{*}"); + var params = request.getPathParams(serviceUri.concat("/{*}")); if (!params.containsKey("*")) { var content = array(); @@ -82,7 +96,6 @@ public void handle(StringRequest request, StringResponse response) throws Except response.setContent(writer.toString()); } else { response.setInError(HttpStatus.SC_NOT_FOUND, "metric not found"); - return; } } } diff --git a/metrics/src/main/java/org/restheart/metrics/RequestsMetricsCollector.java b/metrics/src/main/java/org/restheart/metrics/RequestsMetricsCollector.java index 612b017074..06f64abf83 100644 --- a/metrics/src/main/java/org/restheart/metrics/RequestsMetricsCollector.java +++ b/metrics/src/main/java/org/restheart/metrics/RequestsMetricsCollector.java @@ -54,12 +54,9 @@ public class RequestsMetricsCollector implements WildcardInterceptor { @Inject("config") private Map config; - @Inject("registry") - private PluginsRegistry pluginsRegistry; - // include is a set because we want to check all path templates that match the request - private Set> include = new HashSet<>(); - private PathTemplateMatcher exclude = new PathTemplateMatcher<>(); + private final Set> include = new HashSet<>(); + private final PathTemplateMatcher exclude = new PathTemplateMatcher<>(); @OnInit public void onInit() {