diff --git a/moesif-servlet/pom.xml b/moesif-servlet/pom.xml index 7395cd5..e872d82 100644 --- a/moesif-servlet/pom.xml +++ b/moesif-servlet/pom.xml @@ -3,7 +3,7 @@ 4.0.0 com.moesif.servlet moesif-servlet - 1.6.0 + 1.6.1 jar moesif-servlet Moesif SDK for Java Servlet to log and analyze API calls @@ -61,7 +61,7 @@ com.moesif.api moesifapi - 1.6.3 + 1.6.4 com.fasterxml.jackson.core diff --git a/moesif-servlet/src/main/java/com/moesif/servlet/MoesifFilter.java b/moesif-servlet/src/main/java/com/moesif/servlet/MoesifFilter.java index fac1cd5..edd09b0 100644 --- a/moesif-servlet/src/main/java/com/moesif/servlet/MoesifFilter.java +++ b/moesif-servlet/src/main/java/com/moesif/servlet/MoesifFilter.java @@ -18,13 +18,11 @@ import com.moesif.api.models.*; -import com.moesif.api.APIHelper; import com.moesif.api.MoesifAPIClient; import com.moesif.api.http.client.APICallBack; import com.moesif.api.http.client.HttpContext; import com.moesif.api.http.response.HttpResponse; import com.moesif.api.controllers.APIController; -import com.moesif.api.Configuration; import com.moesif.api.IpAddress; import com.moesif.api.BodyParser; @@ -41,8 +39,8 @@ public class MoesifFilter implements Filter { private MoesifAPIClient moesifApi; private boolean debug; private boolean logBody; - private int samplingPercentage; - private Map> configDict; + private AppConfigModel appConfigModel; + private String cachedConfigEtag; private Date lastUpdatedTime; /** @@ -172,14 +170,8 @@ public void init(FilterConfig filterConfig) throws ServletException { this.logBody = false; } } - - // Global dict - this.configDict = new HashMap>(); - try { - this.samplingPercentage = getAppConfig(null); - } catch (Throwable t) { - this.samplingPercentage = 100; - } + + getAndUpdateAppConfig(); } @Override @@ -189,41 +181,26 @@ public void destroy() { } } - // Get Config - public int getAppConfig(String cachedConfigEtag) throws Throwable { - int sampleRate = 100; + // Get Config. called only when configEtagChange is detected + public void getAndUpdateAppConfig() { try { - // Calling the api - HttpResponse configApiResponse = moesifApi.getAPI().getAppConfig(); - // Fetch the response ETag - String responseConfigEtag = configApiResponse.getHeaders().get("x-moesif-config-etag"); - - if(cachedConfigEtag != null && !cachedConfigEtag.isEmpty() && this.configDict.containsKey(cachedConfigEtag)) { - // Remove from the cache - this.configDict.remove(cachedConfigEtag); - } - - // Read the response body - ObjectMapper mapper = new ObjectMapper(); - Map jsonMap = mapper.readValue(configApiResponse.getRawBody(), Map.class); - - // Add to the global dict - this.configDict.put(responseConfigEtag, jsonMap); - - try { - Map appConfig = this.configDict.get(responseConfigEtag); - // Get the sample rate and update last updated time - if (!appConfig.isEmpty() && appConfig.containsKey("sample_rate")) { - sampleRate = (Integer) appConfig.get("sample_rate"); - } - } catch(Exception e) { - logger.warning("getConfig() call failed " + e.toString()); - } - } catch(Exception e) { + // Calling the api + HttpResponse configApiResponse = moesifApi.getAPI().getAppConfig(); + // Fetch the response ETag + String responseConfigEtag = configApiResponse.getHeaders().get("x-moesif-config-etag"); + + // Read the response body + ObjectMapper mapper = new ObjectMapper(); + AppConfigModel newConfig = mapper.readValue(configApiResponse.getRawBody(), AppConfigModel.class); + + this.appConfigModel = newConfig; + this.cachedConfigEtag = responseConfigEtag; + } catch(Throwable e) { logger.warning("getConfig() call failed " + e.toString()); + this.appConfigModel = new AppConfigModel(); + this.appConfigModel.setSampleRate(100); } this.lastUpdatedTime = new Date(); - return sampleRate; } public void updateUser(UserModel userModel) throws Throwable{ @@ -504,13 +481,13 @@ public void onFailure(HttpContext context, Throwable error) { // Generate random number double randomPercentage = Math.random() * 100; - + + int samplingPercentage = getSampleRateToUse(userId, companyId); + // Compare percentage to send event - if (this.samplingPercentage >= randomPercentage) { + if (samplingPercentage >= randomPercentage) { // Send Event Map eventApiResponse = moesifApi.getAPI().createEvent(maskedEvent); - // Get the key from the global dict - String cachedConfigEtag = this.configDict.keySet().iterator().next(); // Get the etag from event api response String eventResponseConfigEtag = eventApiResponse.get("x-moesif-config-etag"); @@ -519,7 +496,7 @@ public void onFailure(HttpContext context, Throwable error) { && !(eventResponseConfigEtag.equals(cachedConfigEtag)) && new Date().after(new Date(this.lastUpdatedTime.getTime() + 5 * 60 * 1000))) { // Call api to update samplingPercentage - this.samplingPercentage = getAppConfig(cachedConfigEtag); + getAndUpdateAppConfig(); } if (debug) { @@ -528,7 +505,7 @@ && new Date().after(new Date(this.lastUpdatedTime.getTime() + 5 * 60 * 1000))) { } else { if(debug) { - logger.info("Skipped Event due to SamplingPercentage " + this.samplingPercentage + " and randomPercentage " + randomPercentage); + logger.info("Skipped Event due to SamplingPercentage " + samplingPercentage + " and randomPercentage " + randomPercentage); } } @@ -543,6 +520,16 @@ && new Date().after(new Date(this.lastUpdatedTime.getTime() + 5 * 60 * 1000))) { } } + public int getSampleRateToUse(String userId, String companyId) { + int sampleRate = appConfigModel.getSampleRate(); + if (userId != null && appConfigModel.getUserSampleRate().containsKey(userId)) { + sampleRate = appConfigModel.getUserSampleRate().get(userId); + } else if (companyId != null && appConfigModel.getCompanySampleRate().containsKey(companyId)) { + sampleRate = appConfigModel.getCompanySampleRate().get(companyId); + } + return sampleRate; + } + static String getFullURL(HttpServletRequest request) { StringBuffer requestURL = request.getRequestURL(); String queryString = request.getQueryString(); diff --git a/moesif-servlet/src/test/java/com/moesif/servlet/MoesifServletTests.java b/moesif-servlet/src/test/java/com/moesif/servlet/MoesifServletTests.java index 0f0d6d4..b3c607c 100644 --- a/moesif-servlet/src/test/java/com/moesif/servlet/MoesifServletTests.java +++ b/moesif-servlet/src/test/java/com/moesif/servlet/MoesifServletTests.java @@ -13,8 +13,6 @@ import com.moesif.api.models.UserBuilder; import junit.framework.TestCase; import org.mockito.Mockito; -import com.moesif.servlet.MoesifFilter; -import com.moesif.servlet.MoesifConfiguration; import com.moesif.api.models.UserModel; import static org.mockito.Mockito.when; @@ -227,7 +225,7 @@ public void testUpdateCompaniesBatch() throws Throwable { public void testGetAppConfig() throws Throwable{ filter.init(filterConfig); - int sampleRate = filter.getAppConfig(null); + int sampleRate = filter.getSampleRateToUse(null, null); assertTrue("Sample Rate should be less than equal to 100", 100 >= sampleRate); } } diff --git a/moesif-springrequest/pom.xml b/moesif-springrequest/pom.xml index 3642da1..b1a213c 100644 --- a/moesif-springrequest/pom.xml +++ b/moesif-springrequest/pom.xml @@ -3,7 +3,7 @@ 4.0.0 com.moesif.springrequest moesif-springrequest - 1.0.4 + 1.0.5 jar moesif-springrequest Moesif SDK for Java to log and analyze outgoing API calls @@ -56,7 +56,7 @@ com.moesif.api moesifapi - 1.6.3 + 1.6.4 org.springframework diff --git a/moesif-springrequest/src/main/java/com/moesif/springrequest/MoesifSpringRequestInterceptor.java b/moesif-springrequest/src/main/java/com/moesif/springrequest/MoesifSpringRequestInterceptor.java index e15b300..4d00161 100644 --- a/moesif-springrequest/src/main/java/com/moesif/springrequest/MoesifSpringRequestInterceptor.java +++ b/moesif-springrequest/src/main/java/com/moesif/springrequest/MoesifSpringRequestInterceptor.java @@ -219,7 +219,7 @@ public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttp eventModel = config.maskContent(eventModel); - if (api.shouldSendSampledEvent()) { + if (api.shouldSendSampledEvent(eventModel)) { try { APICallBack callback = new APICallBack() { public void onSuccess(HttpContext context, HttpResponse response) {