diff --git a/examples/src/main/java/io/dapr/examples/pubsub/http/PublisherWithTracing.java b/examples/src/main/java/io/dapr/examples/pubsub/http/PublisherWithTracing.java new file mode 100644 index 000000000..646d0f04f --- /dev/null +++ b/examples/src/main/java/io/dapr/examples/pubsub/http/PublisherWithTracing.java @@ -0,0 +1,82 @@ +/* + * Copyright (c) Microsoft Corporation and Dapr Contributors. + * Licensed under the MIT License. + */ + +package io.dapr.examples.pubsub.http; + +import io.dapr.client.DaprClient; +import io.dapr.client.DaprClientBuilder; +import io.dapr.examples.OpenTelemetryConfig; +import io.opentelemetry.api.OpenTelemetry; +import io.opentelemetry.api.trace.Span; +import io.opentelemetry.api.trace.Tracer; +import io.opentelemetry.context.Scope; +import io.opentelemetry.sdk.OpenTelemetrySdk; + +import static io.dapr.examples.OpenTelemetryConfig.getReactorContext; + +/** + * Message publisher. + * 1. Build and install jars: + * mvn clean install + * 2. cd [repo root]/examples + * 3. Run the program: + * dapr run --components-path ./components/pubsub --app-id publisher_tracing -- \ + * java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.pubsub.http.PublisherWithTracing + */ +public class PublisherWithTracing { + + //Number of messages to be sent. + private static final int NUM_MESSAGES = 10; + + //The title of the topic to be used for publishing + private static final String TOPIC_NAME = "testingtopic"; + + //The name of the pubsub + private static final String PUBSUB_NAME = "messagebus"; + + /** + * This is the entry point of the publisher app example. + * + * @param args Args, unused. + * @throws Exception A startup Exception. + */ + public static void main(String[] args) throws Exception { + OpenTelemetry openTelemetry = OpenTelemetryConfig.createOpenTelemetry(); + Tracer tracer = openTelemetry.getTracer(PublisherWithTracing.class.getCanonicalName()); + Span span = tracer.spanBuilder("Publisher's Main").setSpanKind(Span.Kind.CLIENT).startSpan(); + + try (DaprClient client = new DaprClientBuilder().build()) { + try (Scope scope = span.makeCurrent()) { + for (int i = 0; i < NUM_MESSAGES; i++) { + String message = String.format("This is message #%d", i); + // Publishing messages, notice the use of subscriberContext() for tracing. + client.publishEvent( + PUBSUB_NAME, + TOPIC_NAME, + message).subscriberContext(getReactorContext()).block(); + System.out.println("Published message: " + message); + + try { + Thread.sleep((long) (1000 * Math.random())); + } catch (InterruptedException e) { + e.printStackTrace(); + Thread.currentThread().interrupt(); + return; + } + } + } + + // Close the span. + span.end(); + + // Shutdown the OpenTelemetry tracer. + OpenTelemetrySdk.getGlobalTracerManagement().shutdown(); + + // This is an example, so for simplicity we are just exiting here. + // Normally a dapr app would be a web service and not exit main. + System.out.println("Done."); + } + } +} diff --git a/examples/src/main/java/io/dapr/examples/pubsub/http/README.md b/examples/src/main/java/io/dapr/examples/pubsub/http/README.md index ea7f3308d..1e7fe7062 100644 --- a/examples/src/main/java/io/dapr/examples/pubsub/http/README.md +++ b/examples/src/main/java/io/dapr/examples/pubsub/http/README.md @@ -238,6 +238,41 @@ Once running, the Subscriber should print the output as follows: Messages have been retrieved from the topic. +### Tracing + +Dapr handles tracing in PubSub automatically. Open Zipkin on [http://localhost:9411/zipkin](http://localhost:9411/zipkin). You should see a screen like the one below: + +![zipking-landing](https://raw.githubusercontent.com/dapr/java-sdk/master/examples/src/main/resources/img/zipkin-pubsub-landing.png) + +Click on the search icon to see the latest query results. You should see a tracing diagram similar to the one below: + +![zipking-landing](https://raw.githubusercontent.com/dapr/java-sdk/master/examples/src/main/resources/img/zipkin-pubsub-result.png) + +Once you click on the tracing event, you will see the details of the call stack starting in the client and then showing the service API calls right below. + +![zipking-details](https://raw.githubusercontent.com/dapr/java-sdk/master/examples/src/main/resources/img/zipkin-pubsub-details.png) + +If you would like to add a tracing span as a parent of the span created by Dapr, change the publisher to handle that. See `PublisherWithTracing.java` to see the difference and run it with: + + + +```bash +dapr run --components-path ./components/pubsub --app-id publisher_tracing -- java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.pubsub.http.PublisherWithTracing +``` + + + +Now, repeat the search on Zipkin website. All the publisher and subscriber spans are under the same parent span, like in the screen below: + +![zipking-details-custom-span](https://raw.githubusercontent.com/dapr/java-sdk/master/examples/src/main/resources/img/zipkin-pubsub-details-custom-span.png) + ### Message expiration (Optional) Optionally, you can see how Dapr can automatically drop expired messages on behalf of the subscriber. diff --git a/examples/src/main/resources/img/zipkin-pubsub-details-custom-span.png b/examples/src/main/resources/img/zipkin-pubsub-details-custom-span.png new file mode 100644 index 000000000..a60995d32 Binary files /dev/null and b/examples/src/main/resources/img/zipkin-pubsub-details-custom-span.png differ diff --git a/examples/src/main/resources/img/zipkin-pubsub-details.png b/examples/src/main/resources/img/zipkin-pubsub-details.png new file mode 100644 index 000000000..4a2e77ec8 Binary files /dev/null and b/examples/src/main/resources/img/zipkin-pubsub-details.png differ diff --git a/examples/src/main/resources/img/zipkin-pubsub-landing.png b/examples/src/main/resources/img/zipkin-pubsub-landing.png new file mode 100644 index 000000000..a6e0e449b Binary files /dev/null and b/examples/src/main/resources/img/zipkin-pubsub-landing.png differ diff --git a/examples/src/main/resources/img/zipkin-pubsub-result.png b/examples/src/main/resources/img/zipkin-pubsub-result.png new file mode 100644 index 000000000..1ecf680c6 Binary files /dev/null and b/examples/src/main/resources/img/zipkin-pubsub-result.png differ