Skip to content

Commit fb75872

Browse files
[frontend + adservice] Pass Session ID through baggage (#1502)
* pass session ID as baggage to adservice * address review comments
1 parent 17f1bd4 commit fb75872

File tree

4 files changed

+32
-14
lines changed

4 files changed

+32
-14
lines changed

src/adservice/src/main/java/oteldemo/AdService.java

+15-6
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@
1212
import io.grpc.protobuf.services.*;
1313
import io.grpc.stub.StreamObserver;
1414
import io.opentelemetry.api.GlobalOpenTelemetry;
15+
import io.opentelemetry.api.OpenTelemetry;
16+
import io.opentelemetry.api.baggage.Baggage;
1517
import io.opentelemetry.api.common.AttributeKey;
1618
import io.opentelemetry.api.common.Attributes;
1719
import io.opentelemetry.api.metrics.LongCounter;
1820
import io.opentelemetry.api.metrics.Meter;
1921
import io.opentelemetry.api.trace.Span;
2022
import io.opentelemetry.api.trace.StatusCode;
2123
import io.opentelemetry.api.trace.Tracer;
24+
import io.opentelemetry.context.Context;
2225
import io.opentelemetry.context.Scope;
2326
import io.opentelemetry.instrumentation.annotations.SpanAttribute;
2427
import io.opentelemetry.instrumentation.annotations.WithSpan;
@@ -132,7 +135,8 @@ private static class AdServiceImpl extends oteldemo.AdServiceGrpc.AdServiceImplB
132135
private static final String ADSERVICE_FAILURE = "adServiceFailure";
133136
private static final String ADSERVICE_MANUAL_GC_FEATURE_FLAG = "adServiceManualGc";
134137
private static final String ADSERVICE_HIGH_CPU_FEATURE_FLAG = "adServiceHighCpu";
135-
138+
Client ffClient = OpenFeatureAPI.getInstance().getClient();
139+
136140
private AdServiceImpl() {}
137141

138142
/**
@@ -155,6 +159,15 @@ public void getAds(AdRequest req, StreamObserver<AdResponse> responseObserver) {
155159
AdRequestType adRequestType;
156160
AdResponseType adResponseType;
157161

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+
158171
span.setAttribute("app.ads.contextKeys", req.getContextKeysList().toString());
159172
span.setAttribute("app.ads.contextKeys.count", req.getContextKeysCount());
160173
if (req.getContextKeysCount() > 0) {
@@ -214,11 +227,7 @@ public void getAds(AdRequest req, StreamObserver<AdResponse> responseObserver) {
214227
* @return {@code true} if the feature flag is enabled, {@code false} otherwise or in case of errors.
215228
*/
216229
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);
222231
return boolValue;
223232
}
224233
}

src/frontend/gateways/Api.gateway.ts

+14-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import { Ad, Address, Cart, CartItem, Money, PlaceOrderRequest, Product } from '../protos/demo';
55
import { IProductCart, IProductCartItem, IProductCheckout } from '../types/Cart';
66
import request from '../utils/Request';
7+
import { AttributeNames } from '../utils/enums/AttributeNames';
78
import SessionGateway from './Session.gateway';
9+
import { context, propagation } from "@opentelemetry/api";
810

911
const { userId } = SessionGateway.getSession();
1012

@@ -82,11 +84,18 @@ const ApiGateway = () => ({
8284
});
8385
},
8486
listAds(contextKeys: string[]) {
85-
return request<Ad[]>({
86-
url: `${basePath}/data`,
87-
queryParams: {
88-
contextKeys,
89-
},
87+
// TODO: Figure out a better way to do this so session ID gets propagated to
88+
// all endpoints
89+
const baggage = propagation.getActiveBaggage() || propagation.createBaggage();
90+
const newBaggage = baggage.setEntry(AttributeNames.SESSION_ID, { value: userId });
91+
const newContext = propagation.setBaggage(context.active(), newBaggage);
92+
context.with(newContext, () => {
93+
return request<Ad[]>({
94+
url: `${basePath}/data`,
95+
queryParams: {
96+
contextKeys,
97+
},
98+
});
9099
});
91100
},
92101
});

src/frontend/providers/Ad.provider.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ const AdProvider = ({ children, productIds, contextKeys }: IProps) => {
2929
const { selectedCurrency } = useCurrency();
3030
const { data: adList = [] } = useQuery(
3131
['ads', contextKeys],
32-
() => {
32+
async () => {
3333
if (contextKeys.length === 0) {
34-
return Promise.resolve([]);
34+
return [];
3535
} else {
3636
return ApiGateway.listAds(contextKeys);
3737
}

src/frontend/utils/enums/AttributeNames.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
export enum AttributeNames {
5-
SESSION_ID = 'app.session.id'
5+
SESSION_ID = 'session.id'
66
}

0 commit comments

Comments
 (0)