Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve the issue of failing to retrieve attachments in dubbo 2.7.6 and 2.7.7 #12982

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
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.findStatic(
RpcInvocation.class, "getObjectAttachments", MethodType.methodType(void.class));
} catch (Throwable t) {
// ignore
}
GET_OBJECT_ATTACHMENTS = getObjectAttachments;
}

@Override
@SuppressWarnings("deprecation") // deprecation for dubbo 3.2.15
@SuppressWarnings("unchecked") // unchecked for 2.7.6, 2.7.7
public Iterable<String> keys(DubboRequest request) {
return request.invocation().getAttachments().keySet();
RpcInvocation invocation = request.invocation();
steverao marked this conversation as resolved.
Show resolved Hide resolved
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,42 @@
/*
* 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.Iterator;
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;

@Test
@SuppressWarnings("deprecation") // deprecation for RpcInvocation()
void testKeys() {
when(context.getUrl()).thenReturn(new URL("http", "localhost", 1));
when(context.getRemoteAddress()).thenReturn(new InetSocketAddress(1));
when(context.getLocalAddress()).thenReturn(new InetSocketAddress(1));

RpcInvocation invocation = new RpcInvocation();
invocation.setAttachment("key", "value");
DubboRequest request = DubboRequest.create(invocation, context);

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