Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

W3CBaggagePropagator wipe baggage from other propagators #5883

Closed
ddark008 opened this issue Oct 5, 2023 · 7 comments
Closed

W3CBaggagePropagator wipe baggage from other propagators #5883

ddark008 opened this issue Oct 5, 2023 · 7 comments
Labels
needs author feedback Waiting for additional feedback from the author

Comments

@ddark008
Copy link

ddark008 commented Oct 5, 2023

Describe the bug

I am currently working on developing custom propagators that parse baggage from specific headers.
However, when trying to use these custom propagators alongside the W3CBaggagePropagator, I encounter an unexpected behavior.
It seems that the W3CBaggagePropagator creates a new BaggageBuilder and discards the current context Baggage, preventing my custom propagators from functioning properly.

Best solution use exist baggage:

Baggage baggage = Baggage.fromContext(context);

Steps to reproduce

import java.util.List;

import javax.annotation.Nullable;

import io.opentelemetry.api.baggage.Baggage;
import io.opentelemetry.api.baggage.BaggageBuilder;
import io.opentelemetry.api.internal.StringUtils;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.propagation.TextMapGetter;
import io.opentelemetry.context.propagation.TextMapPropagator;
import io.opentelemetry.context.propagation.TextMapSetter;

public class XReqIdPropagator implements TextMapPropagator {
    public static final String X_REQUEST_ID_HEADER = "X-Request-Id";

    @Override
    public List<String> fields() {
        return List.of(X_REQUEST_ID_HEADER);
    }

    @Override
    public <C> void inject(Context context, @Nullable C carrier, TextMapSetter<C> setter) {
        Baggage baggage = Baggage.fromContext(context);
        String outgoingReqId = baggage.getEntryValue("req-id-key");
        if (!StringUtils.isNullOrEmpty(outgoingReqId)) {
            setter.set(carrier, X_REQUEST_ID_HEADER, outgoingReqId);
        }
    }

    @Override
    public <C> Context extract(Context context, @Nullable C carrier, TextMapGetter<C> getter) {
        String incomingReqId = getter.get(carrier, X_REQUEST_ID_HEADER);
        if (!StringUtils.isNullOrEmpty(incomingReqId)) {
           // Use exist Baggage
            Baggage baggage = Baggage.fromContext(context);
            BaggageBuilder builder = baggage.toBuilder();
            builder.put("req-id-key", incomingReqId);
            return context.with(builder.build());
        }
        return context;
    }
}
import com.google.auto.service.AutoService;
import io.opentelemetry.context.propagation.TextMapPropagator;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurablePropagatorProvider;

@AutoService(ConfigurablePropagatorProvider.class)
public class XReqIdPropagatorProvider implements ConfigurablePropagatorProvider {
    @Override
    public TextMapPropagator getPropagator(ConfigProperties config) {
        return new XReqIdPropagator();
    }

    @Override
    public String getName() {
        return "xreqid";
    }
}

Add to config otel.propagators = tracecontext,baggage,xreqid

Expected behavior

Context contain baggage from all propagators

Actual behavior

Context contain baggage only from W3CBaggagePropagator

Javaagent or library instrumentation version

1.29.0

Environment

JDK: 17
OS: Ubuntu

Additional context

No response

@ddark008 ddark008 added the Bug Something isn't working label Oct 5, 2023
@laurit
Copy link
Contributor

laurit commented Oct 5, 2023

@jkwatson @jack-berg could you verify that current behavior is intended or transfer this to opentelemetry-java if this is a valid concern

@jkwatson
Copy link
Contributor

jkwatson commented Oct 5, 2023

This is intended behavior as far as I know. The W3C Baggage Propagator is not intended to combine baggage with any other baggage propagator (much as the W3C Trace Context propagator doesn't somehow combine trace contexts from other propagators). I'll transfer the issue, but I'm pretty sure there would need to be a spec change to support this.

@jkwatson jkwatson transferred this issue from open-telemetry/opentelemetry-java-instrumentation Oct 5, 2023
@jack-berg
Copy link
Member

@ddark008 can't you get the behavior you intend by ensuring that your custom xreqid propagator runs after W3CBaggagePropagator?

@ddark008
Copy link
Author

@jack-berg Thank you for your answer!
That will be sufficient. The propagators are runining according to the in the property otel.propogators ?

@jack-berg
Copy link
Member

@ddark008 yup you can see the source code that interprets otel.propagators here. We read in the property, translate each to the corresponding TextMapPropagator, and call TextMapPropagator.composite(..) with the resulting collection, which ultimately returns [MultiTextMapPropagator](https://github.com/open-telemetry/opentelemetry-java/blob/main/context/src/main/java/io/opentelemetry/context/propagation/MultiTextMapPropagator.java. MultiTextMapPropagator will iterate through the propagators in order and apply each to the resulting context.

So setting otel.propagators=baggage,xreqid should result in xreqid running after W3CBaggagePropagator, and achieve the behavior you want.

@breedx-splk breedx-splk removed the Bug Something isn't working label Oct 18, 2023
@breedx-splk
Copy link
Contributor

@ddark008 please close if this has been satisfied. Thanks! 🙏🏻

@breedx-splk breedx-splk added the needs author feedback Waiting for additional feedback from the author label Oct 18, 2023
@ddark008
Copy link
Author

@jack-berg thank you for the detailed response. It would be great if this behavior is documented to ensure it doesn't change during future refactoring.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs author feedback Waiting for additional feedback from the author
Projects
None yet
Development

No branches or pull requests

5 participants