From 2177c0cab348f3515c1f95d82e52cac3b31954db Mon Sep 17 00:00:00 2001 From: David Pequegnot Date: Sun, 7 Jul 2024 21:29:31 +0200 Subject: [PATCH 1/2] fix(tracing): remove duplicate and fix span prefix name --- docs/workshop.md | 109 +---------------------------------------------- 1 file changed, 1 insertion(+), 108 deletions(-) diff --git a/docs/workshop.md b/docs/workshop.md index 59864ab..1540cfc 100644 --- a/docs/workshop.md +++ b/docs/workshop.md @@ -1631,119 +1631,12 @@ import io.opentelemetry.instrumentation.annotations.WithSpan; public class PaymentService { // ... - @WithSpan("Payment processing method") - private void process(PaymentProcessingContext context) { - //... - } - - @WithSpan("Payment store method") - private void store(PaymentProcessingContext context) { - //... - } -``` - -πŸ“ We can also provide additional information to the span, such as method parameters using the ``@SpanAttribute`` annotation: - -```java -// ... -import io.opentelemetry.instrumentation.annotations.SpanAttribute; - -@Service -public class PaymentService { - // ... - @WithSpan("RivieraDev: Payment processing method") - private void process(@SpanAttribute("context") PaymentProcessingContext context) { // <-- HERE - // ... - } - - @WithSpan("RivieraDev: Payment store method") - private void store(@SpanAttribute("context") PaymentProcessingContext context) { // <-- HERE - // ... - } -``` - -This will provide the whole PaymentProcessingContext into the trace. - -#### 3. Build and redeploy - -πŸ› οΈ As we did before: - -```bash -$ docker compose build easypay-service -$ docker compose up -d easypay-service -``` - -#### 4. Test it! - -πŸ› οΈ Generate some payments: - -```bash -$ http POST :8080/api/easypay/payments posId=POS-01 cardNumber=5555567898780008 expiryDate=789456123 amount:=40000 -``` - -πŸ‘€ Go back to Grafana and try to find your new traces using what you've learned previously. Observe the spans you added. - -> aside negative -> -> It may take some time for `easypay-service` to be registered in the service discovery and be available from the API gateway. -> Similarly, your traces being ingested by Tempo might also take some time. Patience is key πŸ˜… - -Starting from this moment, you should no longer see traces related to `actuator/health` or `actuator/prometheus` endpoints. - -### Custom Traces - -Just like metrics, it is also possible to add your own spans on arbitrary methods to provide more business value to the observability of your application. - -Let’s return to our code! - -#### Objectives - -We want to add new spans to the traces generated in the `easypay-service` application to track payment processing and store events. - -To achieve this goal, we will create new spans when the `process` and `store` methods of the `com.worldline.easypay.payment.control.PaymentService` class in the `easypay-service` module are invoked. - -As a reminder, this class is the central component responsible for processing payments. It provides the public method `accept`, which delegates its responsibilities to two private methods: -* `process`: which handles all the processing of the payment, including validation and calling third parties. -* `store`: which saves the processing result in the database. - -#### 1. Add Required Dependencies - -We need to add the `io.opentelemetry.instrumentation:opentelemetry-instrumentation-annotations` dependency to our module to access some useful annotations. - -πŸ‘€ This has already been done in advance for this workshop. The following dependencies were added to the Gradle build file (`build.gradle.kts`) of the `easypay-service` module: - -```kotlin -dependencies { - //... - - // Add opentelemetry support - implementation(platform("io.opentelemetry:opentelemetry-bom:1.38.0")) - implementation("io.opentelemetry:opentelemetry-api") - implementation("io.opentelemetry.instrumentation:opentelemetry-instrumentation-annotations:2.5.0") - - // ... -} -``` - -#### 2. Add Custom Spans - -πŸ“ To add new spans based on methods, we can simply use the `@WithSpan` Java annotation. When a traced transaction invokes the annotated method, a new span will be created. Here’s how to do it: - -```java -// ... -import io.opentelemetry.instrumentation.annotations.WithSpan; - -@Service -public class PaymentService { - // ... - - @WithSpan("Payment processing method") private void process(PaymentProcessingContext context) { //... } - @WithSpan("Payment store method") + @WithSpan("RivieraDev: Payment store method") private void store(PaymentProcessingContext context) { //... } From ed3ccbbf5b6729fff86f8f5cac284e1cad70547e Mon Sep 17 00:00:00 2001 From: David Pequegnot Date: Sun, 7 Jul 2024 21:32:24 +0200 Subject: [PATCH 2/2] chore: Add toString to context --- .../easypay/payment/control/PaymentProcessingContext.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/easypay-service/src/main/java/com/worldline/easypay/payment/control/PaymentProcessingContext.java b/easypay-service/src/main/java/com/worldline/easypay/payment/control/PaymentProcessingContext.java index ebe29d4..4da0268 100644 --- a/easypay-service/src/main/java/com/worldline/easypay/payment/control/PaymentProcessingContext.java +++ b/easypay-service/src/main/java/com/worldline/easypay/payment/control/PaymentProcessingContext.java @@ -60,4 +60,11 @@ public PaymentResponse generateResponse() { processingMode); } + @Override + public String toString() { + return "PaymentProcessingContext [posId=" + posId + ", cardNumber=" + "**MASKED IN CODE**" + ", expiryDate=" + expiryDate + + ", amount=" + amount + ", id=" + id + ", responseCode=" + responseCode + ", cardType=" + cardType + + ", processingMode=" + processingMode + ", responseTime=" + responseTime + ", dateTime=" + dateTime + + ", bankCalled=" + bankCalled + ", authorized=" + authorized + ", authorId=" + authorId + "]"; + } }