opentelemetry-java-instrumentation
opentelemetry-java-instrumentation copied to clipboard
Every time call Conext.current() will return a different object
Describe the bug
Once we added dependency to this library: io.opentelemetry.instrumentation / opentelemetry-grpc-1.6
It will inject the ContextStorageBridge, with that, it changes the Context.current() behavior, it will return a new Object everytime call the method. That causes various issue such as it breaks the unit test that depends the equality of the Context.
Steps to reproduce
Add dependency to: io.opentelemetry.instrumentation / opentelemetry-grpc-1.6
Run following code:
package org.example;
import io.grpc.Context;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.context.Scope;
public class Main {
public static void main(String[] args) {
try (Scope scope = GlobalOpenTelemetry.get().getTracer("test").spanBuilder("test").startSpan().makeCurrent()) {
System.out.println(Context.current().equals(Context.current()));
}
}
}
Expected behavior
It prints "false"
Actual behavior
it should print "true"
Javaagent or library instrumentation version
1.64.0
Environment
JDK: Java 17 OS: Mac os
Additional context
No response
@jianwu is there anything in gRPC documentation that requires Context.current() to return the same instance. Or is this an undocumented behavior that your application relies on?
Not sure if this is specified. But this seems to be a common sense behavior. When you call a current() multiple times, if you return a different object every time, it will cause surprise and also performance issue. In our case, we have unit test that verify the GRpc context is not changed after calling some logic. And those unit test has nothing to do with OTel at all. Only because we added the dependency in a lower level common library. As the OTel version of ContextStorageOverride is dynamically discovered. It breaks those unit tests.
This breakage is hard to troubleshoot. Also I can't find a workaround for this issue and there's no meaningful equal() for GRpc context.