-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Calendar app: use instrumentation annotations (#127)
* Add a dependency on the `opentelemetry-instrumentation-annotations` library to use `@WithSpan` annotation. * Move `getDate` method to the `CalendarService` class. * Update OTel Java Agent to the latest `2.6.0` version * Fix helm instructions in the README
- Loading branch information
Showing
6 changed files
with
93 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
apps/rest-services/java/calendar/src/main/java/com/otel/service/CalendarService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
Unless explicitly stated otherwise all files in this repository are licensed | ||
under the Apache 2.0 License. | ||
This product includes software developed at Datadog (https://www.datadoghq.com/) | ||
Copyright 2024 Datadog, Inc. | ||
*/ | ||
package com.otel.service; | ||
|
||
import java.time.LocalDate; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.Random; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.stereotype.Component; | ||
|
||
import io.opentelemetry.api.trace.Span; | ||
import io.opentelemetry.api.trace.SpanKind; | ||
import io.opentelemetry.instrumentation.annotations.WithSpan; | ||
|
||
@Component | ||
public class CalendarService { | ||
private final Logger log = LoggerFactory.getLogger(CalendarService.class); | ||
private final Random random = new Random(); | ||
|
||
@WithSpan(kind = SpanKind.CLIENT) | ||
public String getDate() { | ||
Span span = Span.current(); | ||
span.setAttribute("peer.service", "random-date-service"); | ||
|
||
// generate a random day within current year | ||
int day = new Random().nextInt(365); | ||
LocalDate date = LocalDate.now().withDayOfYear(1 + day); | ||
String output = date.format(DateTimeFormatter.ISO_LOCAL_DATE); | ||
|
||
span.setAttribute("date", output); | ||
|
||
try { | ||
// add random sleep | ||
Thread.sleep(random.nextLong(1, 950)); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
log.info("generated date: {}", output); | ||
return output; | ||
} | ||
} |