18
18
19
19
import com .moesif .api .models .*;
20
20
21
- import com .moesif .api .APIHelper ;
22
21
import com .moesif .api .MoesifAPIClient ;
23
22
import com .moesif .api .http .client .APICallBack ;
24
23
import com .moesif .api .http .client .HttpContext ;
25
24
import com .moesif .api .http .response .HttpResponse ;
26
25
import com .moesif .api .controllers .APIController ;
27
- import com .moesif .api .Configuration ;
28
26
import com .moesif .api .IpAddress ;
29
27
import com .moesif .api .BodyParser ;
30
28
@@ -41,8 +39,8 @@ public class MoesifFilter implements Filter {
41
39
private MoesifAPIClient moesifApi ;
42
40
private boolean debug ;
43
41
private boolean logBody ;
44
- private int samplingPercentage ;
45
- private Map < String , Map < String , Object >> configDict ;
42
+ private AppConfigModel appConfigModel ;
43
+ private String cachedConfigEtag ;
46
44
private Date lastUpdatedTime ;
47
45
48
46
/**
@@ -172,14 +170,8 @@ public void init(FilterConfig filterConfig) throws ServletException {
172
170
this .logBody = false ;
173
171
}
174
172
}
175
-
176
- // Global dict
177
- this .configDict = new HashMap <String , Map <String , Object >>();
178
- try {
179
- this .samplingPercentage = getAppConfig (null );
180
- } catch (Throwable t ) {
181
- this .samplingPercentage = 100 ;
182
- }
173
+
174
+ getAndUpdateAppConfig ();
183
175
}
184
176
185
177
@ Override
@@ -189,41 +181,26 @@ public void destroy() {
189
181
}
190
182
}
191
183
192
- // Get Config
193
- public int getAppConfig (String cachedConfigEtag ) throws Throwable {
194
- int sampleRate = 100 ;
184
+ // Get Config. called only when configEtagChange is detected
185
+ public void getAndUpdateAppConfig () {
195
186
try {
196
- // Calling the api
197
- HttpResponse configApiResponse = moesifApi .getAPI ().getAppConfig ();
198
- // Fetch the response ETag
199
- String responseConfigEtag = configApiResponse .getHeaders ().get ("x-moesif-config-etag" );
200
-
201
- if (cachedConfigEtag != null && !cachedConfigEtag .isEmpty () && this .configDict .containsKey (cachedConfigEtag )) {
202
- // Remove from the cache
203
- this .configDict .remove (cachedConfigEtag );
204
- }
205
-
206
- // Read the response body
207
- ObjectMapper mapper = new ObjectMapper ();
208
- Map <String , Object > jsonMap = mapper .readValue (configApiResponse .getRawBody (), Map .class );
209
-
210
- // Add to the global dict
211
- this .configDict .put (responseConfigEtag , jsonMap );
212
-
213
- try {
214
- Map <String , Object > appConfig = this .configDict .get (responseConfigEtag );
215
- // Get the sample rate and update last updated time
216
- if (!appConfig .isEmpty () && appConfig .containsKey ("sample_rate" )) {
217
- sampleRate = (Integer ) appConfig .get ("sample_rate" );
218
- }
219
- } catch (Exception e ) {
220
- logger .warning ("getConfig() call failed " + e .toString ());
221
- }
222
- } catch (Exception e ) {
187
+ // Calling the api
188
+ HttpResponse configApiResponse = moesifApi .getAPI ().getAppConfig ();
189
+ // Fetch the response ETag
190
+ String responseConfigEtag = configApiResponse .getHeaders ().get ("x-moesif-config-etag" );
191
+
192
+ // Read the response body
193
+ ObjectMapper mapper = new ObjectMapper ();
194
+ AppConfigModel newConfig = mapper .readValue (configApiResponse .getRawBody (), AppConfigModel .class );
195
+
196
+ this .appConfigModel = newConfig ;
197
+ this .cachedConfigEtag = responseConfigEtag ;
198
+ } catch (Throwable e ) {
223
199
logger .warning ("getConfig() call failed " + e .toString ());
200
+ this .appConfigModel = new AppConfigModel ();
201
+ this .appConfigModel .setSampleRate (100 );
224
202
}
225
203
this .lastUpdatedTime = new Date ();
226
- return sampleRate ;
227
204
}
228
205
229
206
public void updateUser (UserModel userModel ) throws Throwable {
@@ -504,13 +481,13 @@ public void onFailure(HttpContext context, Throwable error) {
504
481
505
482
// Generate random number
506
483
double randomPercentage = Math .random () * 100 ;
507
-
484
+
485
+ int samplingPercentage = getSampleRateToUse (userId , companyId );
486
+
508
487
// Compare percentage to send event
509
- if (this . samplingPercentage >= randomPercentage ) {
488
+ if (samplingPercentage >= randomPercentage ) {
510
489
// Send Event
511
490
Map <String , String > eventApiResponse = moesifApi .getAPI ().createEvent (maskedEvent );
512
- // Get the key from the global dict
513
- String cachedConfigEtag = this .configDict .keySet ().iterator ().next ();
514
491
// Get the etag from event api response
515
492
String eventResponseConfigEtag = eventApiResponse .get ("x-moesif-config-etag" );
516
493
@@ -519,7 +496,7 @@ public void onFailure(HttpContext context, Throwable error) {
519
496
&& !(eventResponseConfigEtag .equals (cachedConfigEtag ))
520
497
&& new Date ().after (new Date (this .lastUpdatedTime .getTime () + 5 * 60 * 1000 ))) {
521
498
// Call api to update samplingPercentage
522
- this . samplingPercentage = getAppConfig ( cachedConfigEtag );
499
+ getAndUpdateAppConfig ( );
523
500
}
524
501
525
502
if (debug ) {
@@ -528,7 +505,7 @@ && new Date().after(new Date(this.lastUpdatedTime.getTime() + 5 * 60 * 1000))) {
528
505
}
529
506
else {
530
507
if (debug ) {
531
- logger .info ("Skipped Event due to SamplingPercentage " + this . samplingPercentage + " and randomPercentage " + randomPercentage );
508
+ logger .info ("Skipped Event due to SamplingPercentage " + samplingPercentage + " and randomPercentage " + randomPercentage );
532
509
}
533
510
}
534
511
@@ -543,6 +520,16 @@ && new Date().after(new Date(this.lastUpdatedTime.getTime() + 5 * 60 * 1000))) {
543
520
}
544
521
}
545
522
523
+ public int getSampleRateToUse (String userId , String companyId ) {
524
+ int sampleRate = appConfigModel .getSampleRate ();
525
+ if (userId != null && appConfigModel .getUserSampleRate ().containsKey (userId )) {
526
+ sampleRate = appConfigModel .getUserSampleRate ().get (userId );
527
+ } else if (companyId != null && appConfigModel .getCompanySampleRate ().containsKey (companyId )) {
528
+ sampleRate = appConfigModel .getCompanySampleRate ().get (companyId );
529
+ }
530
+ return sampleRate ;
531
+ }
532
+
546
533
static String getFullURL (HttpServletRequest request ) {
547
534
StringBuffer requestURL = request .getRequestURL ();
548
535
String queryString = request .getQueryString ();
0 commit comments