12
12
import io .grpc .protobuf .services .*;
13
13
import io .grpc .stub .StreamObserver ;
14
14
import io .opentelemetry .api .GlobalOpenTelemetry ;
15
+ import io .opentelemetry .api .OpenTelemetry ;
16
+ import io .opentelemetry .api .baggage .Baggage ;
15
17
import io .opentelemetry .api .common .AttributeKey ;
16
18
import io .opentelemetry .api .common .Attributes ;
17
19
import io .opentelemetry .api .metrics .LongCounter ;
18
20
import io .opentelemetry .api .metrics .Meter ;
19
21
import io .opentelemetry .api .trace .Span ;
20
22
import io .opentelemetry .api .trace .StatusCode ;
21
23
import io .opentelemetry .api .trace .Tracer ;
24
+ import io .opentelemetry .context .Context ;
22
25
import io .opentelemetry .context .Scope ;
23
26
import io .opentelemetry .instrumentation .annotations .SpanAttribute ;
24
27
import io .opentelemetry .instrumentation .annotations .WithSpan ;
@@ -132,7 +135,8 @@ private static class AdServiceImpl extends oteldemo.AdServiceGrpc.AdServiceImplB
132
135
private static final String ADSERVICE_FAILURE = "adServiceFailure" ;
133
136
private static final String ADSERVICE_MANUAL_GC_FEATURE_FLAG = "adServiceManualGc" ;
134
137
private static final String ADSERVICE_HIGH_CPU_FEATURE_FLAG = "adServiceHighCpu" ;
135
-
138
+ Client ffClient = OpenFeatureAPI .getInstance ().getClient ();
139
+
136
140
private AdServiceImpl () {}
137
141
138
142
/**
@@ -155,6 +159,15 @@ public void getAds(AdRequest req, StreamObserver<AdResponse> responseObserver) {
155
159
AdRequestType adRequestType ;
156
160
AdResponseType adResponseType ;
157
161
162
+ Baggage baggage = Baggage .fromContextOrNull (Context .current ());
163
+ if (baggage != null ) {
164
+ final String sessionId = baggage .getEntryValue ("session.id" );
165
+ span .setAttribute ("session.id" , sessionId );
166
+ ffClient .setEvaluationContext (new MutableContext ().add ("session" , sessionId ));
167
+ } else {
168
+ logger .info ("no baggage found in context" );
169
+ }
170
+
158
171
span .setAttribute ("app.ads.contextKeys" , req .getContextKeysList ().toString ());
159
172
span .setAttribute ("app.ads.contextKeys.count" , req .getContextKeysCount ());
160
173
if (req .getContextKeysCount () > 0 ) {
@@ -214,11 +227,7 @@ public void getAds(AdRequest req, StreamObserver<AdResponse> responseObserver) {
214
227
* @return {@code true} if the feature flag is enabled, {@code false} otherwise or in case of errors.
215
228
*/
216
229
boolean getFeatureFlagEnabled (String ff ) {
217
- Client client = OpenFeatureAPI .getInstance ().getClient ();
218
- // TODO: Plumb the actual session ID from the frontend via baggage?
219
- UUID uuid = UUID .randomUUID ();
220
- client .setEvaluationContext (new MutableContext ().add ("session" , uuid .toString ()));
221
- Boolean boolValue = client .getBooleanValue (ff , false );
230
+ Boolean boolValue = ffClient .getBooleanValue (ff , false );
222
231
return boolValue ;
223
232
}
224
233
}
0 commit comments