Fix khttp instrumentation in case of absent or read-only headers map
MODIFY
instrumentation/khttp-0.1/src/main/java/io/opentelemetry/auto/instrumentation/khttp/KHttpAdvice.java
instrumentation/khttp-0.1/src/main/java/io/opentelemetry/auto/instrumentation/khttp/KHttpAdvice.java
@@ -39,7 +39,7 @@ public class KHttpAdvice {
public static SpanWithScope methodEnter(
@Advice.Argument(value = 0) String method,
@Advice.Argument(value = 1) String uri,
- @Advice.Argument(value = 2) Map<String, String> headers) {
+ @Advice.Argument(value = 2, readOnly = false) Map<String, String> headers) {
final int callDepth = CallDepthThreadLocalMap.incrementCallDepth(KHttp.class);
if (callDepth > 0) {
@@ -53,7 +53,8 @@ public class KHttpAdvice {
final Context context = withSpan(span, Context.current());
- OpenTelemetry.getPropagators().getHttpTextFormat().inject(context, asWritable(headers), SETTER);
+ headers = asWritable(headers);
+ OpenTelemetry.getPropagators().getHttpTextFormat().inject(context, headers, SETTER);
return new SpanWithScope(span, withScopedContext(context));
}

MODIFY
instrumentation/khttp-0.1/src/main/java/io/opentelemetry/auto/instrumentation/khttp/KHttpHeadersInjectAdapter.java
instrumentation/khttp-0.1/src/main/java/io/opentelemetry/auto/instrumentation/khttp/KHttpHeadersInjectAdapter.java
@@ -21,23 +21,9 @@ import java.util.Map;
public class KHttpHeadersInjectAdapter implements HttpTextFormat.Setter<Map<String, String>> {
- private static Class emptyMap;
-
- static {
- try {
- emptyMap = Class.forName("kotlin.collections.EmptyMap");
- } catch (ClassNotFoundException e) {
- }
- }
-
public static Map<String, String> asWritable(Map<String, String> headers) {
- // EmptyMap is read-only so we have to substitute it with writable instance to be able to inject
- // headers
- if (emptyMap != null && emptyMap.isInstance(headers)) {
- return new HashMap<>();
- } else {
- return headers;
- }
+ // Kotlin likes to use read-only data structures, so wrap into new writable map
+ return new HashMap<>(headers);
}
public static final KHttpHeadersInjectAdapter SETTER = new KHttpHeadersInjectAdapter();

Fix khttp instrumentation in case of absent or read-only headers map (#416)