Skip to content

I want to propagate TraceId to all places where parallelStream() and CustomForkJoinPool is used. #138

@petercheon

Description

@petercheon

I would like to know how to propagate the TraceId within the Java Stream API in Spring Cloud Sleuth.

In the code below, the trace_id is not propagated, and each client ends up having an individual TraceId.

TestForkJoinPool testForkJoinPool = new TestForkJoinPool(10);
Span span = tracer.nextSpan(tracer.currentSpan());

testForkJoinPool.submit(() -> {
    try (SpanInScope ignored = tracer.withSpan(span)) {
        IntStream.range(1, 20).parallel().forEach(idx -> {
            httpClient.get(1000L);
        });
    } finally {
        span.end();
    }
}).get();

However, if we modify the above code as shown below, the TraceId is propagated.

TestForkJoinPool testForkJoinPool = new TestForkJoinPool(10);
Span span = tracer.nextSpan(tracer.currentSpan());

testForkJoinPool.submit(() -> {
    IntStream.range(1, 20).parallel().forEach(idx -> {
        try (SpanInScope ignored = tracer.withSpan(span)) {
            httpClient.get(1000L);
        } finally {
            span.end();
        }
    });
}).get();

Unfortunately, applying SpanInScope to all existing Stream API code as mentioned above has its limitations. Is there a way to inject SpanInScope with minimal modifications to the existing Stream API code?

Thank you.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions