Skip to content

Commit

Permalink
🐛 Fix Cannot change the uri of the metrics service
Browse files Browse the repository at this point in the history
  • Loading branch information
ujibang committed Dec 11, 2024
1 parent b4c8c34 commit bdf19b8
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 24 deletions.
3 changes: 1 addition & 2 deletions commons/src/main/java/org/restheart/utils/PluginUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,12 @@ public static <P extends Service> String defaultURI(Class<P> serviceClass) {
/**
*
* @param <P>
* @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 <P extends Service> String actualUri(Map<String, Object> conf, Class<P> serviceClass) {

if (conf != null && conf.get("uri") != null && conf.get("uri") instanceof String) {
return (String) conf.get("uri");
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 7 additions & 5 deletions core/src/main/resources/restheart-default-config-no-mongodb.yml
Original file line number Diff line number Diff line change
Expand Up @@ -452,13 +452,15 @@ 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/*" ]
include: ["/*"]
exclude: ["/metrics", "/metrics/*"]

jvmMetricsCollector:
enabled: false
Expand Down
9 changes: 6 additions & 3 deletions core/src/main/resources/restheart-default-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -431,11 +431,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/*" ]

Expand Down
27 changes: 20 additions & 7 deletions metrics/src/main/java/org/restheart/metrics/MetricsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -38,21 +43,30 @@
*
* @author Andrea Di Cesare {@literal <[email protected]>}
*/
@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 {
/**
*
* prefix for registry names used by retheart-metrics plugins
*/
public static String METRICS_REGISTRIES_PREFIX = "METRICS-";

@Inject("config")
private Map<String, Object> 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;
Expand All @@ -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();
Expand All @@ -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;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,9 @@ public class RequestsMetricsCollector implements WildcardInterceptor {
@Inject("config")
private Map<String, Object> config;

@Inject("registry")
private PluginsRegistry pluginsRegistry;

// include is a set because we want to check all path templates that match the request
private Set<PathTemplateMatcher<Boolean>> include = new HashSet<>();
private PathTemplateMatcher<Boolean> exclude = new PathTemplateMatcher<>();
private final Set<PathTemplateMatcher<Boolean>> include = new HashSet<>();
private final PathTemplateMatcher<Boolean> exclude = new PathTemplateMatcher<>();

@OnInit
public void onInit() {
Expand Down

0 comments on commit bdf19b8

Please sign in to comment.