Fix khttp instrumentation in case of absent or read-only headers map (#416)
This commit is contained in:
parent
4acfeb9ddc
commit
a258f1424a
|
|
@ -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));
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
|
|||
Loading…
Reference in New Issue