Skip to content

Commit

Permalink
Resolve the issue of failing to retrieve attachments in dubbo 2.7.6 a…
Browse files Browse the repository at this point in the history
…nd 2.7.7 (#12982)

Co-authored-by: Lauri Tulmin <[email protected]>
Co-authored-by: Lauri Tulmin <[email protected]>
  • Loading branch information
3 people authored Jan 10, 2025
1 parent 260f237 commit 3c94040
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ dependencies {
}

tasks.withType<Test>().configureEach {
systemProperty("testLatestDeps", findProperty("testLatestDeps") as Boolean)
jvmArgs("-XX:+IgnoreUnrecognizedVMOptions")
// to suppress non-fatal errors on jdk17
jvmArgs("--add-opens=java.base/java.math=ALL-UNNAMED")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,44 @@
package io.opentelemetry.instrumentation.apachedubbo.v2_7;

import io.opentelemetry.context.propagation.TextMapGetter;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.util.Map;
import org.apache.dubbo.rpc.RpcInvocation;

enum DubboHeadersGetter implements TextMapGetter<DubboRequest> {
INSTANCE;

private static final MethodHandle GET_OBJECT_ATTACHMENTS;

static {
MethodHandles.Lookup lookup = MethodHandles.lookup();
MethodHandle getObjectAttachments = null;
try {
getObjectAttachments =
lookup.findVirtual(
RpcInvocation.class, "getObjectAttachments", MethodType.methodType(Map.class));
} catch (Throwable t) {
// ignore
}
GET_OBJECT_ATTACHMENTS = getObjectAttachments;
}

@Override
@SuppressWarnings("deprecation") // deprecation for dubbo 3.2.15
@SuppressWarnings("unchecked")
public Iterable<String> keys(DubboRequest request) {
return request.invocation().getAttachments().keySet();
RpcInvocation invocation = request.invocation();
try {
// In 2.7.6, 2.7.7, the StringToObjectMap implementation does not correctly retrieve the
// keySet. Therefore, it's advisable to always call getObjectAttachments when it is available.
if (GET_OBJECT_ATTACHMENTS != null) {
return ((Map<String, Object>) GET_OBJECT_ATTACHMENTS.invoke(invocation)).keySet();
}
} catch (Throwable t) {
// ignore
}
return invocation.getAttachments().keySet();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.apachedubbo.v2_7;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;

import java.net.InetSocketAddress;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.rpc.RpcContext;
import org.apache.dubbo.rpc.RpcInvocation;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
class DubboHeadersGetterTest {

@Mock RpcContext context;
@Mock RpcInvocation rpcInvocation;

@Test
void testKeys() throws Exception {
when(context.getUrl()).thenReturn(new URL("http", "localhost", 1));
when(context.getRemoteAddress()).thenReturn(new InetSocketAddress(1));
when(context.getLocalAddress()).thenReturn(new InetSocketAddress(1));

// for latest dep tests call getObjectAttachments, otherwise call getAttachments
if (Boolean.getBoolean("testLatestDeps")) {
when(getObjectAttachments()).thenReturn(Collections.singletonMap("key", "value"));
} else {
when(rpcInvocation.getAttachments()).thenReturn(Collections.singletonMap("key", "value"));
}
DubboRequest request = DubboRequest.create(rpcInvocation, context);

Iterator<String> iterator = DubboHeadersGetter.INSTANCE.keys(request).iterator();
assertThat(iterator.hasNext()).isTrue();
assertThat(iterator.next()).isEqualTo("key");
assertThat(iterator.hasNext()).isFalse();
}

@SuppressWarnings("unchecked")
private Map<Object, Object> getObjectAttachments() throws Exception {
return (Map<Object, Object>)
RpcInvocation.class.getMethod("getObjectAttachments").invoke(rpcInvocation);
}
}

0 comments on commit 3c94040

Please sign in to comment.