Skip to content

Commit 9b5fcf5

Browse files
hsdmirandabrian-brazil
authored andcommitted
Complete the Spring Actuator support
1 parent a4781a5 commit 9b5fcf5

File tree

4 files changed

+37
-3
lines changed

4 files changed

+37
-3
lines changed

simpleclient_spring_boot/src/main/java/io/prometheus/client/spring/boot/PrometheusEndpointConfiguration.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package io.prometheus.client.spring.boot;
22

33
import io.prometheus.client.CollectorRegistry;
4+
import org.springframework.boot.actuate.condition.ConditionalOnEnabledEndpoint;
5+
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
46
import org.springframework.context.annotation.Bean;
57
import org.springframework.context.annotation.Configuration;
68

@@ -11,4 +13,11 @@ class PrometheusEndpointConfiguration {
1113
public PrometheusEndpoint prometheusEndpoint() {
1214
return new PrometheusEndpoint(CollectorRegistry.defaultRegistry);
1315
}
16+
17+
@Bean
18+
@ConditionalOnBean(PrometheusEndpoint.class)
19+
@ConditionalOnEnabledEndpoint("prometheus")
20+
public PrometheusMVCEndpoint prometheusEndpointFix(PrometheusEndpoint prometheusEndpoint) {
21+
return new PrometheusMVCEndpoint(prometheusEndpoint);
22+
}
1423
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package io.prometheus.client.spring.boot;
2+
3+
import io.prometheus.client.exporter.common.TextFormat;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.boot.actuate.endpoint.mvc.AbstractEndpointMvcAdapter;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RequestMethod;
8+
9+
public class PrometheusMVCEndpoint extends AbstractEndpointMvcAdapter<PrometheusEndpoint> {
10+
11+
@Autowired
12+
public PrometheusMVCEndpoint(PrometheusEndpoint delegate) {
13+
super(delegate);
14+
}
15+
16+
@RequestMapping(value = "", method = RequestMethod.GET, produces = TextFormat.CONTENT_TYPE_004)
17+
public Object invoke() {
18+
if (!getDelegate().isEnabled()) {
19+
// Shouldn't happen because the request mapping should not be registered
20+
return getDisabledResponse();
21+
} else {
22+
return getDelegate().invoke();
23+
}
24+
}
25+
}

simpleclient_spring_boot/src/test/java/io/prometheus/client/spring/boot/PrometheusEndpointTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package io.prometheus.client.spring.boot;
22

33
import io.prometheus.client.Counter;
4+
import io.prometheus.client.exporter.common.TextFormat;
45
import io.prometheus.client.matchers.CustomMatchers;
6+
import org.apache.commons.lang3.StringUtils;
57
import org.junit.Test;
68
import org.junit.runner.RunWith;
79
import org.springframework.beans.factory.annotation.Value;
810
import org.springframework.boot.test.SpringApplicationConfiguration;
911
import org.springframework.boot.test.TestRestTemplate;
1012
import org.springframework.boot.test.WebIntegrationTest;
1113
import org.springframework.http.HttpStatus;
12-
import org.springframework.http.MediaType;
1314
import org.springframework.http.ResponseEntity;
1415
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
1516
import org.springframework.web.client.RestTemplate;
@@ -48,7 +49,7 @@ public void testMetricsExportedThroughPrometheusEndpoint() {
4849

4950
// then:
5051
assertEquals(HttpStatus.OK, metricsResponse.getStatusCode());
51-
assertTrue(MediaType.TEXT_PLAIN.isCompatibleWith(metricsResponse.getHeaders().getContentType()));
52+
assertTrue(StringUtils.deleteWhitespace(TextFormat.CONTENT_TYPE_004).equals(metricsResponse.getHeaders().getContentType().toString()));
5253

5354
List<String> responseLines = Arrays.asList(metricsResponse.getBody().split("\n"));
5455
assertThat(responseLines, CustomMatchers.<String>exactlyNItems(1,

simpleclient_spring_web/src/test/java/io/prometheus/client/spring/web/MethodTimerTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import org.junit.Assert;
66
import org.junit.Test;
77
import org.springframework.aop.aspectj.annotation.AspectJProxyFactory;
8-
import org.springframework.context.annotation.Import;
98

109
import java.util.Enumeration;
1110

0 commit comments

Comments
 (0)