Skip to content

Missing examples with reactive programming (reactor) #591

Open
@ygiros

Description

@ygiros

Hi,

Examples provided in Manual span creation and baggage propagation are only available with imperative programming. Is it possible to extend those examples to reactive programming ?

Zero-code instrumentation with opentelemetry agent specifies compatibility with reactor library https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/main/docs/supported-libraries.md#libraries--frameworks, however I didn't find any examples here or in https://opentelemetry.io/docs/ on how to do manual instrumentation on reactive code with reactor.

I would like to execute following actions in reactor operators

  • Create span and make it current
  • Inject attribute to current span
  • Inject baggage and make it current

As for now, I can only "guess" how I should do in reactive programming, by "translating" imperative examples I have found with reactor equivalents.

For example I translate following imperative code

public String imperativeFoo() {
    try (Scope scope1 = tracer.spanBuilder("foo").startSpan().makeCurrent()) {
        Span.current().setAttribute("fooAttribute", "fooAttributeValue");

        Baggage baggage = Baggage.current().toBuilder()
                .put("fooBaggage", "fooBaggageValue")
                .build();

        try (Scope scope2 = baggage.makeCurrent()) {
            return bar();
        }
    }
}

private String bar() {
    // returns "bar:fooBaggageValue"
    return "bar:" + Baggage.current().getEntryValue("fooBaggage");
}

into

public Mono<String> reactiveFoo() {
    return Mono.using(
        () -> tracer.spanBuilder("foo").startSpan().makeCurrent(),
            scope1 ->
                Mono.fromSupplier(() -> Baggage.current().toBuilder().put("fooBaggage", "fooBaggageValue").build())
                    .doOnNext(baggage -> Span.current().setAttribute("fooAttribute", "fooAttributeValue"))
                    .flatMap(baggage ->
                        Mono.using(
                            baggage::makeCurrent,
                            scope2 -> Mono.fromSupplier(this::bar))));
}

private String bar() {
    // returns "bar:fooBaggageValue"
    return "bar:" + Baggage.current().getEntryValue("fooBaggage");
}

But is it the right way to do it ? If yes, could you please add this example to the documentation ?

Did I miss some other documentation on it somewhere ?

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions