Skip to content

Commit

Permalink
Merge pull request #1 from csankhala/metrix_exposure_prefix
Browse files Browse the repository at this point in the history
Added metricx.exposure.prefix configuration
  • Loading branch information
csankhala authored Nov 23, 2019
2 parents 4025db5 + 22d1f63 commit a71e8ec
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 7 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
plugins {
id 'java'
id 'maven'
}

group 'org.springframework.boot'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package org.springframework.metricx.controller;
package org.springframework.metricx;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Predicate;
import java.util.regex.Pattern;

import org.springframework.http.MediaType;
import org.springframework.metricx.config.MetricxConfig;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -16,22 +18,26 @@
public class MetricxEndpoint {

private MeterRegistry registry;
private MetricxConfig metricxConfig;

public MetricxEndpoint(MeterRegistry registry) {
public MetricxEndpoint(MeterRegistry registry,
MetricxConfig metricxConfig) {
this.registry = registry;
this.metricxConfig = metricxConfig;
}

@GetMapping("/metricx")
@GetMapping(value = "/metricx", produces = MediaType.APPLICATION_JSON_VALUE)
public Map<String, Double> metricx() {
Map<String, Double> matricKeyValue = new LinkedHashMap<>();
collectMetrics(matricKeyValue, this.registry, (String) -> true);
collectMetrics(matricKeyValue, this.registry,
metricName -> metricName.startsWith(metricxConfig.getExposurePrefix()));
return matricKeyValue;
}

private void collectMetrics(Map<String, Double> matricKeyValue, MeterRegistry registry, Predicate<String> filter) {
if (registry instanceof CompositeMeterRegistry) {
((CompositeMeterRegistry) registry).getRegistries()
.forEach((member) -> collectMetrics(matricKeyValue, member, filter));
.forEach(member -> collectMetrics(matricKeyValue, member, filter));
} else {
registry.getMeters()
.stream()
Expand All @@ -42,10 +48,13 @@ private void collectMetrics(Map<String, Double> matricKeyValue, MeterRegistry re
}
}

@GetMapping("/metricx/{metricPattern}")
@GetMapping(value = "/metricx/{metricPattern}", produces = MediaType.APPLICATION_JSON_VALUE)
public Map<String, Double> metricPattern(@PathVariable("metricPattern") String metricPattern) {
Map<String, Double> matricsKeyValue = new LinkedHashMap<>();
collectMetrics(matricsKeyValue, this.registry, metricName -> Pattern.matches(metricPattern, metricName));
collectMetrics(matricsKeyValue, this.registry,
metricName ->
metricName.startsWith(metricxConfig.getExposurePrefix())
&& Pattern.matches(metricPattern, metricName));
return matricsKeyValue;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.springframework.metricx.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;

@Configuration
@Getter
@Setter
public class MetricxConfig {

/**
* IF exposurePrefix is configured 'counter' THEN
* '/metricx' endpoint will publish metrics starting with 'counter only'
* Default: empty, means publish all
*/
@NonNull
@Value("${metricx.exposure.prefix:}")
private String exposurePrefix;
}
57 changes: 57 additions & 0 deletions src/test/java/org/springframework/metricx/MetricxEndpointTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.springframework.metricx;

import java.util.Map;

import org.junit.Before;
import org.junit.Test;
import org.springframework.metricx.config.MetricxConfig;

import io.micrometer.core.instrument.simple.SimpleMeterRegistry;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

public class MetricxEndpointTest {

private MetricxEndpoint metricxEndpoint;
private SimpleMeterRegistry registry;
private MetricxConfig metricxConfig;

@Before
public void setUp() throws Exception {
registry = new SimpleMeterRegistry();
metricxConfig = new MetricxConfig();
metricxConfig.setExposurePrefix("counter");
metricxEndpoint = new MetricxEndpoint(registry, metricxConfig);
}

@Test
public void metricx_shouldReturnEmptyMap_When_NoMetricRegistered() {
assertTrue(metricxEndpoint.metricx().isEmpty());
}

@Test
public void metricx_shouldReturnAllMetricsRegistered() {
registry.counter("counter.request.success").increment();
registry.counter("counter.request.fail").increment();
registry.counter("jvm.memory.used").increment();

Map<String, Double> metricx = metricxEndpoint.metricx();
assertSame(2, metricx.size());
assertEquals(1, metricx.get("counter.request.success").intValue());
assertEquals(1, metricx.get("counter.request.fail").intValue());
}

@Test
public void metricPattern() {
registry.counter("counter.request.success").increment();
registry.counter("counter.response.success").increment();
registry.counter("jvm.memory.used").increment();

Map<String, Double> metricx = metricxEndpoint.metricPattern("counter.response.*");

assertSame(1, metricx.size());
assertEquals(1, metricx.get("counter.response.success").intValue());
}
}

0 comments on commit a71e8ec

Please sign in to comment.