From 7c0d1c0446eaca1b169ff6d6295fd024dbbbd498 Mon Sep 17 00:00:00 2001 From: Fabien Crespel Date: Sun, 1 Dec 2024 23:40:35 +0000 Subject: [PATCH] Add CustomMetricFilter --- .../web/filter/CustomMetricFilter.java | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/main/java/me/crespel/karaplan/web/filter/CustomMetricFilter.java diff --git a/src/main/java/me/crespel/karaplan/web/filter/CustomMetricFilter.java b/src/main/java/me/crespel/karaplan/web/filter/CustomMetricFilter.java new file mode 100644 index 0000000..b3c63cd --- /dev/null +++ b/src/main/java/me/crespel/karaplan/web/filter/CustomMetricFilter.java @@ -0,0 +1,38 @@ +package me.crespel.karaplan.web.filter; + +import java.io.IOException; + +import org.springframework.stereotype.Component; +import org.springframework.web.filter.GenericFilterBean; + +import io.opentelemetry.api.OpenTelemetry; +import io.opentelemetry.api.common.Attributes; +import io.opentelemetry.api.metrics.LongCounter; +import io.opentelemetry.api.metrics.Meter; +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.ServletRequest; +import jakarta.servlet.ServletResponse; +import jakarta.servlet.http.HttpServletRequest; + +@Component +public class CustomMetricFilter extends GenericFilterBean { + + private final LongCounter apiCallsCounter; + + public CustomMetricFilter(OpenTelemetry otel) { + Meter meter = otel.getMeter("karaplan"); + this.apiCallsCounter = meter.counterBuilder("karaplan.api.calls").setDescription("Number of KaraPlan API calls").build(); + } + + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { + HttpServletRequest httpRequest = (HttpServletRequest) request; + if (httpRequest.getRequestURI() != null && httpRequest.getRequestURI().startsWith("/api/")) { + Attributes attrs = Attributes.builder().put("method", httpRequest.getMethod()).build(); + apiCallsCounter.add(1, attrs); + } + chain.doFilter(request, response); + } + +}