java-sdk icon indicating copy to clipboard operation
java-sdk copied to clipboard

Error propagating cutom trace-id on pub/sub

Open rochabr opened this issue 9 months ago • 4 comments

Expected Behavior

Dapr runtime: 1.15.5 Dapr Cli: 1.15.0 Java SDK: 1.13.3

I'm expecting to see the same trace-id for my publisher and subscriber on zipkin when I set a custom trace-id as a metadata when I call PublishEvent. Ie:

Image

Sample code:

// Create order data
Map<String, Object> order = new HashMap<>();
String orderId = UUID.randomUUID().toString();
order.put("orderId", orderId);
order.put("customer", "test-customer");
order.put("amount", 100.00);
order.put("timestamp", System.currentTimeMillis());

// Create metadata to override CloudEvent properties
Map<String, String> metadata = new HashMap<>();
metadata.put("cloudevent.traceid", "your-custom-trace-id");
metadata.put("cloudevent.source", "order-service");
metadata.put("cloudevent.type", "com.example.order");
metadata.put("cloudevent.id", UUID.randomUUID().toString());

// Publish with metadata
return daprClient.publishEvent(
        PUBSUB_NAME,
        TOPIC_NAME,
        order,
        metadata
).then(Mono.fromCallable(() -> {
    String response = "Published order with ID: " + orderId;
    logger.info(response);
    return response;
}));

Actual Behavior

Following the example above, Zipkin shows 2 different entries for the publisher and subscriber.each with its own trace-id:

Image

Steps to Reproduce the Problem

Run the sample here: https://github.com/rochabr/dapr-trace-example

rochabr avatar Mar 04 '25 05:03 rochabr

@rochabr I am not sure if this is a bug.. I think adding a cloudevent trace.id doesn't automatically propagate the trace to the right place.

Look at this example on how the context is propagated: https://github.com/salaboy/pizza/blob/observability/pizza-store/src/main/java/io/diagrid/dapr/PizzaStore.java#L220

The trick is in:

.contextWrite(getReactorContext(context))

By adding this code, we are grabbing the context from the Reactor framework in Spring and propagating the incoming trace to Dapr.

salaboy avatar Mar 04 '25 08:03 salaboy

@salaboy I think I'm missing a few steps here. to pass this context from the publisher to the subscriber it needs to be fetched somehow. I couldn't find in your project how to do it.

I see an import statement to import io.diagrid.dapr.otel.OpenTelemetryConfig.getReactorContext; which cannot be resolved with the dapr-spring-boot-starter dependency. Can you help me understand or point me to the docs that explain how to fetch context, modify the trace and propagate to the subscriber?

rochabr avatar Mar 05 '25 01:03 rochabr

Updated my example with the otel sdk. The only issue I have now is the publisher trace which is duplicated but one of them (the one with the custom id) propagates properly to the subscriber.

rochabr avatar Mar 07 '25 01:03 rochabr

Can you share more details? like the zipkin graph showing the duplicated IDs? can you share the trace graph too?

salaboy avatar Mar 07 '25 08:03 salaboy