Skip to content

Commit

Permalink
Optimize codes
Browse files Browse the repository at this point in the history
  • Loading branch information
steverao committed Jan 2, 2025
1 parent 3cd545e commit a9e1180
Showing 1 changed file with 6 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@
import io.opentelemetry.context.propagation.TextMapGetter;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Set;
import org.apache.dubbo.rpc.RpcInvocation;

enum DubboHeadersGetter implements TextMapGetter<DubboRequest> {
INSTANCE;

@Override
@SuppressWarnings("unchecked") // unchecked for dubbo 2.7.6
@SuppressWarnings("unchecked") // unchecked for 2.7.6, 2.7.7
public Iterable<String> keys(DubboRequest request) {
RpcInvocation invocation = request.invocation();
Map<String, String> attachments = invocation.getAttachments();
// in 2.7.6+, type of attachments is StringToObjectMap, it doesn't contain keySet method.
if ("ObjectToStringMap".equals(attachments.getClass().getSimpleName())) {
Set<String> keys = invocation.getAttachments().keySet();
// In 2.7.6, 2.7.7, the StringToObjectMap implementation does not correctly retrieve the keySet.
if (keys.size() == 0 && "ObjectToStringMap".equals(attachments.getClass().getSimpleName())) {
Method getObjectAttachmentsMethod = null;
try {
getObjectAttachmentsMethod = invocation.getClass().getMethod("getObjectAttachments");
Expand All @@ -28,7 +30,7 @@ public Iterable<String> keys(DubboRequest request) {
// ignore
}
}
return invocation.getAttachments().keySet();
return keys;
}

@Override
Expand Down

0 comments on commit a9e1180

Please sign in to comment.