-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
38 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
src/main/java/me/crespel/karaplan/web/filter/CustomMetricFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |