All Downloads are FREE. Search and download functionalities are using the official Maven repository.

no.skatteetaten.aurora.prometheus.ServerMetricsCollector Maven / Gradle / Ivy

There is a newer version: 3.7.4
Show newest version
package no.skatteetaten.aurora.prometheus;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import no.skatteetaten.aurora.prometheus.collector.HttpMetricsCollector;

@Component
public class ServerMetricsCollector  implements Filter {

    private HttpMetricsCollector collector;

    public ServerMetricsCollector(@Qualifier("server") HttpMetricsCollector collector) {
        this.collector = collector;
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
        throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        long start = System.nanoTime();
        try {
            filterChain.doFilter(request, response);
        } finally {
            collector.record(request.getMethod(), request.getRequestURI(), response.getStatus(), start);
        }
    }

    @Override
    public void destroy() {
        //we do not need to destroy
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        //nothing to init

    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy