javagoogle-app-engineappstats

Record only a fraction of all requests with AppStats for App Engine in Java


We're trying to reduce the overhead of AppStats on a high traffic website. AppStats for Python has a configuration setting appstats_RECORD_FRACTION which limits the number of requests that are recorded. This allow you to record the stats for only a small percentage of all request, e.g. 1%.

I cannot find any reference in the documentation for a similar configuration setting for AppStats for Java. Does anyone know how to configure this?


Solution

  • It seems that after years there still isn't support for "Record Fraction" in appstats for Java. The workaround I found for it was implementing a custom filter:

    package com.example.server.servlet;
    
    import com.google.appengine.tools.appstats.AppstatsFilter;
    
    import java.io.IOException;
    import java.util.Random;
    
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    
    public class AppstatsImprovedFilter extends AppstatsFilter {
    
        private double recordFraction;
        private Random random;
    
        @Override
        public synchronized void init(FilterConfig config) {
            super.init(config);
            String recordFractionConfig = config.getInitParameter("recordFraction");
            recordFraction = recordFractionConfig == null ? 1.0 : Double.parseDouble(recordFractionConfig);
            random = new Random();
        }
    
        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain filters)
                throws IOException, ServletException {
            if (recordFraction < 1.0 && random.nextDouble() > recordFraction) {
                filters.doFilter(request, response);
            } else {
                super.doFilter(request, response, filters);
            }
        }
    }
    

    Then you use this filter instead of AppstatsFilter one in web.xml:

    <filter>
        <filter-name>appstats</filter-name>
        <filter-class>com.example.server.servlet.AppstatsImprovedFilter</filter-class>
        <init-param>
            <param-name>calculateRpcCosts</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>recordFraction</param-name>
            <param-value>0.05</param-value>
        </init-param>
    </filter>