Skip to content

Commit d2a317b

Browse files
authored
Enable reactor netty metrics (#124)
Signed-off-by: Seddik Yengui <[email protected]>
1 parent 5f4a119 commit d2a317b

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@
110110
<groupId>org.springframework.cloud</groupId>
111111
<artifactId>spring-cloud-starter-gateway</artifactId>
112112
</dependency>
113+
<dependency>
114+
<groupId>io.micrometer</groupId>
115+
<artifactId>micrometer-core</artifactId>
116+
</dependency>
113117

114118
<!-- runtime dependencies -->
115119
<dependency>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* Copyright (c) 2024, RTE (http://www.rte-france.com)
3+
* This Source Code Form is subject to the terms of the Mozilla Public
4+
* License, v. 2.0. If a copy of the MPL was not distributed with this
5+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
6+
*/
7+
8+
package org.gridsuite.gateway;
9+
10+
import io.micrometer.core.instrument.config.MeterFilter;
11+
import org.springframework.boot.web.embedded.netty.NettyServerCustomizer;
12+
import org.springframework.context.annotation.Bean;
13+
import org.springframework.context.annotation.Configuration;
14+
import reactor.netty.http.server.HttpServer;
15+
16+
import java.util.List;
17+
import java.util.function.Function;
18+
19+
/**
20+
* @author Seddik Yengui <seddik.yengui_externe at rte-france.com>
21+
*/
22+
23+
// Enable Netty metrics that are not enabled by default in Spring Boot.
24+
@Configuration
25+
public class NettyMetricsConfiguration implements NettyServerCustomizer {
26+
27+
private static final String REACTOR_NETTY_PREFIX = "reactor.netty";
28+
// If additional metrics are added, ensure the URIs are provided in a template-like format.
29+
// Without this, each unique URI generates a separate tag, which takes a lot of memory
30+
private static final List<String> ALLOWED_REACTOR_NETTY_METRICS = List.of(
31+
"reactor.netty.http.server.connections.total",
32+
"reactor.netty.http.server.connections.active"
33+
);
34+
35+
@Override
36+
public HttpServer apply(HttpServer httpServer) {
37+
return httpServer.metrics(true, Function.identity());
38+
}
39+
40+
@Bean
41+
public MeterFilter meterFilter() {
42+
return MeterFilter.denyUnless(id -> {
43+
String name = id.getName();
44+
// Allow all non-reactor metrics
45+
if (!name.startsWith(REACTOR_NETTY_PREFIX)) {
46+
return true;
47+
}
48+
// Allow only the specific reactor metrics that we use
49+
return ALLOWED_REACTOR_NETTY_METRICS.contains(name);
50+
});
51+
}
52+
}

0 commit comments

Comments
 (0)