@@ -135,7 +135,7 @@ private static class AdServiceImpl extends oteldemo.AdServiceGrpc.AdServiceImplB
135
135
private static final String ADSERVICE_FAILURE = "adServiceFailure" ;
136
136
private static final String ADSERVICE_MANUAL_GC_FEATURE_FLAG = "adServiceManualGc" ;
137
137
private static final String ADSERVICE_HIGH_CPU_FEATURE_FLAG = "adServiceHighCpu" ;
138
- Client ffClient = OpenFeatureAPI .getInstance ().getClient ();
138
+ private static final Client ffClient = OpenFeatureAPI .getInstance ().getClient ();
139
139
140
140
private AdServiceImpl () {}
141
141
@@ -149,8 +149,6 @@ private AdServiceImpl() {}
149
149
@ Override
150
150
public void getAds (AdRequest req , StreamObserver <AdResponse > responseObserver ) {
151
151
AdService service = AdService .getInstance ();
152
- CPULoad cpuload = CPULoad .getInstance ();
153
- cpuload .execute (getFeatureFlagEnabled (ADSERVICE_HIGH_CPU_FEATURE_FLAG ));
154
152
155
153
// get the current span in context
156
154
Span span = Span .current ();
@@ -160,14 +158,19 @@ public void getAds(AdRequest req, StreamObserver<AdResponse> responseObserver) {
160
158
AdResponseType adResponseType ;
161
159
162
160
Baggage baggage = Baggage .fromContextOrNull (Context .current ());
161
+ MutableContext evaluationContext = new MutableContext ();
163
162
if (baggage != null ) {
164
163
final String sessionId = baggage .getEntryValue ("session.id" );
165
164
span .setAttribute ("session.id" , sessionId );
166
- ffClient .setEvaluationContext (new MutableContext ().add ("session" , sessionId ));
165
+ evaluationContext .setTargetingKey (sessionId );
166
+ evaluationContext .add ("session" , sessionId );
167
167
} else {
168
168
logger .info ("no baggage found in context" );
169
169
}
170
170
171
+ CPULoad cpuload = CPULoad .getInstance ();
172
+ cpuload .execute (ffClient .getBooleanValue (ADSERVICE_HIGH_CPU_FEATURE_FLAG , false , evaluationContext ));
173
+
171
174
span .setAttribute ("app.ads.contextKeys" , req .getContextKeysList ().toString ());
172
175
span .setAttribute ("app.ads.contextKeys.count" , req .getContextKeysCount ());
173
176
if (req .getContextKeysCount () > 0 ) {
@@ -198,11 +201,11 @@ public void getAds(AdRequest req, StreamObserver<AdResponse> responseObserver) {
198
201
Attributes .of (
199
202
adRequestTypeKey , adRequestType .name (), adResponseTypeKey , adResponseType .name ()));
200
203
201
- if (getFeatureFlagEnabled (ADSERVICE_FAILURE )) {
204
+ if (ffClient . getBooleanValue (ADSERVICE_FAILURE , false , evaluationContext )) {
202
205
throw new StatusRuntimeException (Status .UNAVAILABLE );
203
206
}
204
207
205
- if (getFeatureFlagEnabled (ADSERVICE_MANUAL_GC_FEATURE_FLAG )) {
208
+ if (ffClient . getBooleanValue (ADSERVICE_MANUAL_GC_FEATURE_FLAG , false , evaluationContext )) {
206
209
logger .warn ("Feature Flag " + ADSERVICE_MANUAL_GC_FEATURE_FLAG + " enabled, performing a manual gc now" );
207
210
GarbageCollectionTrigger gct = new GarbageCollectionTrigger ();
208
211
gct .doExecute ();
@@ -219,17 +222,6 @@ public void getAds(AdRequest req, StreamObserver<AdResponse> responseObserver) {
219
222
responseObserver .onError (e );
220
223
}
221
224
}
222
-
223
- /**
224
- * Retrieves the status of a feature flag from the Feature Flag service.
225
- *
226
- * @param ff The name of the feature flag to retrieve.
227
- * @return {@code true} if the feature flag is enabled, {@code false} otherwise or in case of errors.
228
- */
229
- boolean getFeatureFlagEnabled (String ff ) {
230
- Boolean boolValue = ffClient .getBooleanValue (ff , false );
231
- return boolValue ;
232
- }
233
225
}
234
226
235
227
private static final ImmutableListMultimap <String , Ad > adsMap = createAdsMap ();
0 commit comments