Skip to content

Commit 3c94040

Browse files
steveraolaurit
andauthored
Resolve the issue of failing to retrieve attachments in dubbo 2.7.6 and 2.7.7 (#12982)
Co-authored-by: Lauri Tulmin <[email protected]> Co-authored-by: Lauri Tulmin <[email protected]>
1 parent 260f237 commit 3c94040

File tree

3 files changed

+87
-2
lines changed

3 files changed

+87
-2
lines changed

instrumentation/apache-dubbo-2.7/library-autoconfigure/build.gradle.kts

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ dependencies {
1414
}
1515

1616
tasks.withType<Test>().configureEach {
17+
systemProperty("testLatestDeps", findProperty("testLatestDeps") as Boolean)
1718
jvmArgs("-XX:+IgnoreUnrecognizedVMOptions")
1819
// to suppress non-fatal errors on jdk17
1920
jvmArgs("--add-opens=java.base/java.math=ALL-UNNAMED")

instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetter.java

+32-2
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,44 @@
66
package io.opentelemetry.instrumentation.apachedubbo.v2_7;
77

88
import io.opentelemetry.context.propagation.TextMapGetter;
9+
import java.lang.invoke.MethodHandle;
10+
import java.lang.invoke.MethodHandles;
11+
import java.lang.invoke.MethodType;
12+
import java.util.Map;
13+
import org.apache.dubbo.rpc.RpcInvocation;
914

1015
enum DubboHeadersGetter implements TextMapGetter<DubboRequest> {
1116
INSTANCE;
1217

18+
private static final MethodHandle GET_OBJECT_ATTACHMENTS;
19+
20+
static {
21+
MethodHandles.Lookup lookup = MethodHandles.lookup();
22+
MethodHandle getObjectAttachments = null;
23+
try {
24+
getObjectAttachments =
25+
lookup.findVirtual(
26+
RpcInvocation.class, "getObjectAttachments", MethodType.methodType(Map.class));
27+
} catch (Throwable t) {
28+
// ignore
29+
}
30+
GET_OBJECT_ATTACHMENTS = getObjectAttachments;
31+
}
32+
1333
@Override
14-
@SuppressWarnings("deprecation") // deprecation for dubbo 3.2.15
34+
@SuppressWarnings("unchecked")
1535
public Iterable<String> keys(DubboRequest request) {
16-
return request.invocation().getAttachments().keySet();
36+
RpcInvocation invocation = request.invocation();
37+
try {
38+
// In 2.7.6, 2.7.7, the StringToObjectMap implementation does not correctly retrieve the
39+
// keySet. Therefore, it's advisable to always call getObjectAttachments when it is available.
40+
if (GET_OBJECT_ATTACHMENTS != null) {
41+
return ((Map<String, Object>) GET_OBJECT_ATTACHMENTS.invoke(invocation)).keySet();
42+
}
43+
} catch (Throwable t) {
44+
// ignore
45+
}
46+
return invocation.getAttachments().keySet();
1747
}
1848

1949
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.instrumentation.apachedubbo.v2_7;
7+
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
import static org.mockito.Mockito.when;
10+
11+
import java.net.InetSocketAddress;
12+
import java.util.Collections;
13+
import java.util.Iterator;
14+
import java.util.Map;
15+
import org.apache.dubbo.common.URL;
16+
import org.apache.dubbo.rpc.RpcContext;
17+
import org.apache.dubbo.rpc.RpcInvocation;
18+
import org.junit.jupiter.api.Test;
19+
import org.junit.jupiter.api.extension.ExtendWith;
20+
import org.mockito.Mock;
21+
import org.mockito.junit.jupiter.MockitoExtension;
22+
23+
@ExtendWith(MockitoExtension.class)
24+
class DubboHeadersGetterTest {
25+
26+
@Mock RpcContext context;
27+
@Mock RpcInvocation rpcInvocation;
28+
29+
@Test
30+
void testKeys() throws Exception {
31+
when(context.getUrl()).thenReturn(new URL("http", "localhost", 1));
32+
when(context.getRemoteAddress()).thenReturn(new InetSocketAddress(1));
33+
when(context.getLocalAddress()).thenReturn(new InetSocketAddress(1));
34+
35+
// for latest dep tests call getObjectAttachments, otherwise call getAttachments
36+
if (Boolean.getBoolean("testLatestDeps")) {
37+
when(getObjectAttachments()).thenReturn(Collections.singletonMap("key", "value"));
38+
} else {
39+
when(rpcInvocation.getAttachments()).thenReturn(Collections.singletonMap("key", "value"));
40+
}
41+
DubboRequest request = DubboRequest.create(rpcInvocation, context);
42+
43+
Iterator<String> iterator = DubboHeadersGetter.INSTANCE.keys(request).iterator();
44+
assertThat(iterator.hasNext()).isTrue();
45+
assertThat(iterator.next()).isEqualTo("key");
46+
assertThat(iterator.hasNext()).isFalse();
47+
}
48+
49+
@SuppressWarnings("unchecked")
50+
private Map<Object, Object> getObjectAttachments() throws Exception {
51+
return (Map<Object, Object>)
52+
RpcInvocation.class.getMethod("getObjectAttachments").invoke(rpcInvocation);
53+
}
54+
}

0 commit comments

Comments
 (0)