|
| 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