Skip to content

Commit

Permalink
Merge pull request #1753 from akto-api-security/feature/add_api_count…
Browse files Browse the repository at this point in the history
…er_logging

add more logs
  • Loading branch information
ayushaga14 authored Nov 21, 2024
2 parents c323a34 + 54fa1a2 commit 5f63268
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions apps/dashboard/src/main/java/com/akto/filter/LoggingFilter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.akto.filter;

import com.akto.dao.context.Context;
import com.akto.dto.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -9,16 +10,20 @@
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;

public class LoggingFilter implements Filter {

private static final Logger logger = LoggerFactory.getLogger(LoggingFilter.class);
private static AtomicInteger apiCounter = new AtomicInteger();

@Override
public void init(FilterConfig filterConfig) { }

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
int startTs = Context.now();
apiCounter.incrementAndGet();
chain.doFilter(request, response);

try {
Expand All @@ -28,7 +33,7 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
int statusCode = httpServletResponse.getStatus();
String uri = httpServletRequest.getRequestURI();
if (uri.contains("fetchActiveLoaders") ||
uri.contains("fetchActiveTestRunsStatus") || uri.contains("metrics")) {
uri.contains("fetchActiveTestRunsStatus") || uri.contains("metrics") || uri.contains("favicon")) {
return;
}
String method = httpServletRequest.getMethod();
Expand All @@ -46,12 +51,14 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
ip = httpServletRequest.getRemoteAddr();
}

String result = "url="+uri + ";method="+method + ";statusCode="+statusCode + ";username="+username + ";ip="+ ip;

int endTs = Context.now();
String result = "url="+uri + ";method="+method + ";statusCode="+statusCode + ";username="+username + ";ip="+ ip + ";totalTime=" + (endTs - startTs) + ";apiCounterVal=" + apiCounter.get();
logger.info(result);

} catch (Exception e) {
logger.error("Error: ", e);
} finally {
apiCounter.decrementAndGet();
}

}
Expand Down

0 comments on commit 5f63268

Please sign in to comment.