Skip to content

Commit

Permalink
[frontend + adservice] Pass Session ID through baggage (open-telemetr…
Browse files Browse the repository at this point in the history
…y#1502)

* pass session ID as baggage to adservice

* address review comments
  • Loading branch information
austinlparker authored and AlexPSplunk committed Jul 10, 2024
1 parent d2f09b0 commit 160da76
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
21 changes: 15 additions & 6 deletions src/adservice/src/main/java/oteldemo/AdService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@
import io.grpc.protobuf.services.*;
import io.grpc.stub.StreamObserver;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.baggage.Baggage;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.metrics.LongCounter;
import io.opentelemetry.api.metrics.Meter;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.StatusCode;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.instrumentation.annotations.SpanAttribute;
import io.opentelemetry.instrumentation.annotations.WithSpan;
Expand Down Expand Up @@ -132,7 +135,8 @@ private static class AdServiceImpl extends oteldemo.AdServiceGrpc.AdServiceImplB
private static final String ADSERVICE_FAILURE = "adServiceFailure";
private static final String ADSERVICE_MANUAL_GC_FEATURE_FLAG = "adServiceManualGc";
private static final String ADSERVICE_HIGH_CPU_FEATURE_FLAG = "adServiceHighCpu";

Client ffClient = OpenFeatureAPI.getInstance().getClient();

private AdServiceImpl() {}

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

Baggage baggage = Baggage.fromContextOrNull(Context.current());
if (baggage != null) {
final String sessionId = baggage.getEntryValue("session.id");
span.setAttribute("session.id", sessionId);
ffClient.setEvaluationContext(new MutableContext().add("session", sessionId));
} else {
logger.info("no baggage found in context");
}

span.setAttribute("app.ads.contextKeys", req.getContextKeysList().toString());
span.setAttribute("app.ads.contextKeys.count", req.getContextKeysCount());
if (req.getContextKeysCount() > 0) {
Expand Down Expand Up @@ -214,11 +227,7 @@ public void getAds(AdRequest req, StreamObserver<AdResponse> responseObserver) {
* @return {@code true} if the feature flag is enabled, {@code false} otherwise or in case of errors.
*/
boolean getFeatureFlagEnabled(String ff) {
Client client = OpenFeatureAPI.getInstance().getClient();
// TODO: Plumb the actual session ID from the frontend via baggage?
UUID uuid = UUID.randomUUID();
client.setEvaluationContext(new MutableContext().add("session", uuid.toString()));
Boolean boolValue = client.getBooleanValue(ff, false);
Boolean boolValue = ffClient.getBooleanValue(ff, false);
return boolValue;
}
}
Expand Down
19 changes: 14 additions & 5 deletions src/frontend/gateways/Api.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import { Ad, Address, Cart, CartItem, Money, PlaceOrderRequest, Product } from '../protos/demo';
import { IProductCart, IProductCartItem, IProductCheckout } from '../types/Cart';
import request from '../utils/Request';
import { AttributeNames } from '../utils/enums/AttributeNames';
import SessionGateway from './Session.gateway';
import { context, propagation } from "@opentelemetry/api";

const { userId } = SessionGateway.getSession();

Expand Down Expand Up @@ -82,11 +84,18 @@ const ApiGateway = () => ({
});
},
listAds(contextKeys: string[]) {
return request<Ad[]>({
url: `${basePath}/data`,
queryParams: {
contextKeys,
},
// TODO: Figure out a better way to do this so session ID gets propagated to
// all endpoints
const baggage = propagation.getActiveBaggage() || propagation.createBaggage();
const newBaggage = baggage.setEntry(AttributeNames.SESSION_ID, { value: userId });
const newContext = propagation.setBaggage(context.active(), newBaggage);
context.with(newContext, () => {
return request<Ad[]>({
url: `${basePath}/data`,
queryParams: {
contextKeys,
},
});
});
},
});
Expand Down
4 changes: 2 additions & 2 deletions src/frontend/providers/Ad.provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ const AdProvider = ({ children, productIds, contextKeys }: IProps) => {
const { selectedCurrency } = useCurrency();
const { data: adList = [] } = useQuery(
['ads', contextKeys],
() => {
async () => {
if (contextKeys.length === 0) {
return Promise.resolve([]);
return [];
} else {
return ApiGateway.listAds(contextKeys);
}
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/utils/enums/AttributeNames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
// SPDX-License-Identifier: Apache-2.0

export enum AttributeNames {
SESSION_ID = 'app.session.id'
SESSION_ID = 'session.id'
}

0 comments on commit 160da76

Please sign in to comment.