我们正试图在高流量网站上减少AppStats的开销。
AppStats for Python的配置设置为appstats_RECORD_FRACTION
,可以限制记录的请求数。这允许您仅记录所有请求的一小部分的统计数据,例如1%。
我在文档中找不到AppStats for Java的类似配置设置的任何参考。有谁知道如何配置?
答案 0 :(得分:0)
似乎经过多年的努力仍然没有支持"记录分数"在appstats for Java中。我找到的解决方法是实现自定义过滤器:
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);
}
}
}
然后在web.xml中使用此过滤器而不是AppstatsFilter:
<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>