Skip to content

Commit

Permalink
some minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Øyvind Randa committed Sep 2, 2024
1 parent cb4d2b8 commit bc2d1e3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 67 deletions.
58 changes: 0 additions & 58 deletions src/adservice/src/main/java/oteldemo/AdService.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,6 @@ public final class AdService {
private HealthStatusManager healthMgr;

private static final AdService service = new AdService();
// assignment.3
private static final Tracer tracer = GlobalOpenTelemetry.getTracer("adservice");
// Assignment.4
private static final Meter meter = GlobalOpenTelemetry.getMeter("adservice");
//Assignment.4
private static final LongCounter adRequestsCounter =
meter
.counterBuilder("app.ads.ad_requests")
.setDescription("Counts ad requests by request and response type")
.build();

private static final AttributeKey<String> adRequestTypeKey =
AttributeKey.stringKey("app.ads.ad_request_type");
private static final AttributeKey<String> adResponseTypeKey =
AttributeKey.stringKey("app.ads.ad_response_type");

private void start() throws IOException {
int port =
Expand Down Expand Up @@ -151,33 +136,16 @@ private AdServiceImpl() {}
@Override
public void getAds(AdRequest req, StreamObserver<AdResponse> responseObserver) {
AdService service = AdService.getInstance();
// assignment.3 code
// get the current span in context
Span span = Span.current();

try {
List<Ad> allAds = new ArrayList<>();
AdRequestType adRequestType;
AdResponseType adResponseType;
// Assignment 5
Baggage baggage = Baggage.fromContextOrNull(Context.current());
MutableContext evaluationContext = new MutableContext();
if (baggage != null) {
final String sessionId = baggage.getEntryValue("session.id");
span.setAttribute("session.id", sessionId);
evaluationContext.setTargetingKey(sessionId);
evaluationContext.add("session", sessionId);
} else {
logger.info("no baggage found in context");
}

CPULoad cpuload = CPULoad.getInstance();
cpuload.execute(ffClient.getBooleanValue(ADSERVICE_HIGH_CPU_FEATURE_FLAG, false, evaluationContext));

// Assignment 3 kode
span.setAttribute("app.ads.contextKeys", req.getContextKeysList().toString());
span.setAttribute("app.ads.contextKeys.count", req.getContextKeysCount());

if (req.getContextKeysCount() > 0) {
logger.info("Targeted ad request received for " + req.getContextKeysList());
for (int i = 0; i < req.getContextKeysCount(); i++) {
Expand All @@ -197,15 +165,7 @@ public void getAds(AdRequest req, StreamObserver<AdResponse> responseObserver) {
allAds = service.getRandomAds();
adResponseType = AdResponseType.RANDOM;
}
// Assignment.2
span.setAttribute("app.ads.count", allAds.size());
span.setAttribute("app.ads.ad_request_type", adRequestType.name());
span.setAttribute("app.ads.ad_response_type", adResponseType.name());

adRequestsCounter.add(
1,
Attributes.of(
adRequestTypeKey, adRequestType.name(), adResponseTypeKey, adResponseType.name()));

// Throw 1/10 of the time to simulate a failure when the feature flag is enabled
if (ffClient.getBooleanValue(ADSERVICE_FAILURE, false, evaluationContext) && random.nextInt(10) == 0) {
Expand All @@ -222,23 +182,15 @@ public void getAds(AdRequest req, StreamObserver<AdResponse> responseObserver) {
responseObserver.onNext(reply);
responseObserver.onCompleted();
} catch (StatusRuntimeException e) {
// Assignment 3
span.addEvent(
"Error", Attributes.of(AttributeKey.stringKey("exception.message"), e.getMessage()));
span.setStatus(StatusCode.ERROR);
logger.log(Level.WARN, "GetAds Failed with status {}", e.getStatus());
responseObserver.onError(e);
}
}
}

private static final ImmutableListMultimap<String, Ad> adsMap = createAdsMap();
// assignmentt.3
@WithSpan("getAdsByCategory")
private Collection<Ad> getAdsByCategory(@SpanAttribute("app.ads.category") String category) {
Collection<Ad> ads = adsMap.get(category);
// Assignment.3
Span.current().setAttribute("app.ads.count", ads.size());
return ads;
}

Expand All @@ -248,23 +200,13 @@ private List<Ad> getRandomAds() {

List<Ad> ads = new ArrayList<>(MAX_ADS_TO_SERVE);

// create and start a new span manually
// Assignment 4
Span span = tracer.spanBuilder("getRandomAds").startSpan();

// put the span into context, so if any child span is started the parent will be set properly
// Assignment.3
try (Scope ignored = span.makeCurrent()) {

Collection<Ad> allAds = adsMap.values();
for (int i = 0; i < MAX_ADS_TO_SERVE; i++) {
ads.add(Iterables.get(allAds, random.nextInt(allAds.size())));
}
span.setAttribute("app.ads.count", ads.size());

} finally {
span.end();
}

return ads;
}
Expand Down
16 changes: 7 additions & 9 deletions workshop/03-instrumentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ Assignment :

In get getAds method hook into the span context and add the following attributes to the span "app.ads.contextKeys", "app.ads.contextKeys.count", "app.ads.count", "app.ads.ad_request_type", "app.ads.ad_response_type" . This will give us insight into what advertisement that has been shown.

You can rebuild the adservice with the following command :

```bash
docker-compose down adservice
docker-compose up adservice --build -d
```

You can verify that the attributes are set by looking at the traces in Grafana with Tempo as the datasource.

Next we want to refine the traces from the span with events and status codes if getAds method fails. So inside the catch block add and error event with attributeKey thats should be called : "exception.message" that adds the exception to the span. We should also as part of this mark the span as failed.
Expand Down Expand Up @@ -60,15 +67,6 @@ it should be used to enrich the current span and to update a custom context obje
If no baggage is found, you will handle this case by logging an appropriate message.


## OpenTelemetry with Spring boot and micrometer

Spring boot 3 has excellent support for OpenTelemetry via the micrometer. Spring Boot 3 comes with full support for OpenTelemetry.

## Instrument Currency-service

Currency-service has been reimplemented in Spring Boot 3 from C++. To enable that we have to comment out the image from currency-service in the docker-compose file.

´´´



Expand Down

0 comments on commit bc2d1e3

Please sign in to comment.