Skip to content

Commit

Permalink
Add CustomMetricFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
fcrespel committed Dec 1, 2024
1 parent 8610e5b commit 7c0d1c0
Showing 1 changed file with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}

}

0 comments on commit 7c0d1c0

Please sign in to comment.