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

Feature Request: Better support for injecting into immutable carriers

Open youngm opened this issue 2 years ago • 6 comments

Is your feature request related to a problem? Please describe. TextMapSetter API doesn't work well with immutable Carriers. For example, SQS SendMessageRequest in the AWS Java SDK v2.

Describe the solution you'd like Perhaps the inject() API and TextMapSetter could support returning the newly injected carrier?

Describe alternatives you've considered Currently, my TextMapSetter for Immutable carriers put the new carrier in some kind of variable that I then aquire and actually send. This is sub-optimal because it makes my TextMapSetter, not thread-safe requiring a new instance for each injection. This isn't too bad but seems to go against the idea of the injection api.

youngm avatar Feb 22 '22 17:02 youngm

I think this is a good idea..now we just have to figure out how to make it work while maintaining binary backward compatibility.

jkwatson avatar Feb 23 '22 02:02 jkwatson

@youngm SendMessageRequest has a Builder class can you use it to inject?

Returning the type probably would be good but indeed will be somewhat tricky to update instrumentation for. In practice I've seen most immutable objects have a builder type so wondering if it's something we can live with still.

anuraaga avatar Feb 23 '22 02:02 anuraaga

@anuraaga SendMessageRequest does have a builder but it builds a new immutable instance, it doesn't modify the original instance. Creating the new Carrier isn't the issue here. I think the main issue here is the API to get that new carrier to the code sending the carrier.

youngm avatar Feb 23 '22 17:02 youngm

just as a reference, here are two places in the java-instrumentation repo where we hack around immutable carriers:

https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/main/instrumentation/akka/akka-http-10.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/akkahttp/client/HttpHeaderSetter.java

https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/main/instrumentation/java-http-client/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/httpclient/HttpHeadersSetter.java

trask avatar Feb 23 '22 18:02 trask

@anuraaga SendMessageRequest does have a builder but it builds a new immutable instance, it doesn't modify the original instance. Creating the new Carrier isn't the issue here. I think the main issue here is the API to get that new carrier to the code sending the carrier.

I think @anuraaga 's idea was to use the builder as the carrier, rather than the request itself.

jkwatson avatar Feb 23 '22 19:02 jkwatson

@anuraaga yeah, that's a good idea and better than the workaround I've been using in many cases. Thanks.

youngm avatar Feb 23 '22 22:02 youngm