Skip to content

Commit 6817c8b

Browse files
committed
refactor code
1 parent 5648f0e commit 6817c8b

File tree

2 files changed

+21
-43
lines changed

2 files changed

+21
-43
lines changed

instrumentation/grpc-1.6/src/main/java/io/opentelemetry/javaagent/instrumentation/hypertrace/grpc/v1_6/GrpcSpanDecorator.java

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package io.opentelemetry.javaagent.instrumentation.hypertrace.grpc.v1_6;
1818

19+
import com.google.protobuf.Message;
1920
import io.grpc.Metadata;
2021
import io.grpc.Metadata.Key;
2122
import io.opentelemetry.api.common.AttributeKey;
@@ -37,12 +38,16 @@ private GrpcSpanDecorator() {}
3738
private static final JsonFormat.Printer PRINTER = JsonFormat.printer();
3839

3940
public static void addMessageAttribute(Object message, Span span, AttributeKey<String> key) {
40-
if (isProtobufMessage(message)) {
41+
if (message instanceof Message) {
42+
Message mb = (Message) message;
4143
try {
42-
ProtobufRoundTripConverter.addConvertedMessageAttribute(message, span, key);
44+
String jsonOutput = ProtobufMessageConverter.getMessage(mb);
45+
span.setAttribute(key, jsonOutput);
4346
} catch (Exception e) {
44-
log.error("Failed to print message as JSON", e);
47+
log.error("Failed to decode message as JSON", e);
4548
}
49+
} else {
50+
log.debug("message is not an instance of com.google.protobuf.Message");
4651
}
4752
}
4853

@@ -83,21 +88,4 @@ public static Map<String, String> metadataToMap(Metadata metadata) {
8388
}
8489
return mapHeaders;
8590
}
86-
87-
private static boolean isProtobufMessage(Object message) {
88-
if (message == null) {
89-
return false;
90-
}
91-
// Check the interfaces on the class and its superclasses
92-
Class<?> cls = message.getClass();
93-
while (cls != null) {
94-
for (Class<?> iface : cls.getInterfaces()) {
95-
if ("com.google.protobuf.Message".equals(iface.getName())) {
96-
return true;
97-
}
98-
}
99-
cls = cls.getSuperclass();
100-
}
101-
return false;
102-
}
10391
}

instrumentation/grpc-1.6/src/main/java/io/opentelemetry/javaagent/instrumentation/hypertrace/grpc/v1_6/ProtobufRoundTripConverter.java renamed to instrumentation/grpc-1.6/src/main/java/io/opentelemetry/javaagent/instrumentation/hypertrace/grpc/v1_6/ProtobufMessageConverter.java

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@
1818

1919
import com.google.protobuf.Descriptors;
2020
import com.google.protobuf.Message;
21-
import io.opentelemetry.api.common.AttributeKey;
22-
import io.opentelemetry.api.trace.Span;
2321
import io.opentelemetry.javaagent.instrumentation.hypertrace.com.google.protobuf.DescriptorProtos.FileDescriptorProto;
2422
import io.opentelemetry.javaagent.instrumentation.hypertrace.com.google.protobuf.Descriptors.Descriptor;
2523
import io.opentelemetry.javaagent.instrumentation.hypertrace.com.google.protobuf.Descriptors.FileDescriptor;
2624
import io.opentelemetry.javaagent.instrumentation.hypertrace.com.google.protobuf.DynamicMessage;
2725
import io.opentelemetry.javaagent.instrumentation.hypertrace.com.google.protobuf.util.JsonFormat;
26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
2828

29-
public class ProtobufRoundTripConverter {
30-
29+
public class ProtobufMessageConverter {
30+
private static final Logger log = LoggerFactory.getLogger(ProtobufMessageConverter.class);
3131
/**
3232
* Converts an unrelocated protobuf message into a relocated DynamicMessage via a byte-array
3333
* round-trip.
@@ -36,16 +36,12 @@ public class ProtobufRoundTripConverter {
3636
* @return A relocated DynamicMessage built from your relocated protobuf classes.
3737
* @throws Exception if conversion fails.
3838
*/
39-
public static DynamicMessage convertToRelocatedDynamicMessage(Object message) throws Exception {
40-
if (!(message instanceof Message)) {
41-
throw new IllegalArgumentException("message is not a protobuf Message");
42-
}
39+
public static DynamicMessage convertToRelocatedDynamicMessage(Message message) throws Exception {
4340
// 1. Serialize the original message to bytes.
44-
Message originalMessage = (Message) message;
45-
byte[] messageBytes = originalMessage.toByteArray();
41+
byte[] messageBytes = message.toByteArray();
4642

4743
// 2. Obtain the original (unrelocated) message descriptor.
48-
Descriptors.Descriptor originalDescriptor = originalMessage.getDescriptorForType();
44+
Descriptors.Descriptor originalDescriptor = message.getDescriptorForType();
4945

5046
// 3. Get the unrelocated file descriptor and its proto representation.
5147
Descriptors.FileDescriptor unrelocatedFileDescriptor = originalDescriptor.getFile();
@@ -58,12 +54,10 @@ public static DynamicMessage convertToRelocatedDynamicMessage(Object message) th
5854
FileDescriptorProto relocatedFileProto = FileDescriptorProto.parseFrom(fileProtoBytes);
5955

6056
// 5. Build the relocated FileDescriptor.
61-
// Note: This example assumes there are no dependencies.
6257
FileDescriptor relocatedFileDescriptor =
6358
FileDescriptor.buildFrom(relocatedFileProto, new FileDescriptor[] {});
6459

6560
// 6. Find the relocated message descriptor by name.
66-
// We assume the message name is the same in both relocated and unrelocated files.
6761
Descriptor relocatedDescriptor =
6862
relocatedFileDescriptor.findMessageTypeByName(originalDescriptor.getName());
6963
if (relocatedDescriptor == null) {
@@ -77,15 +71,12 @@ public static DynamicMessage convertToRelocatedDynamicMessage(Object message) th
7771
}
7872

7973
/**
80-
* Example method that takes an incoming message, converts it to a relocated one, prints it as
81-
* JSON using the relocated JsonFormat, and attaches it as a span attribute.
74+
* Method that takes an incoming message, converts it to a relocated one, prints it as JSON using
75+
* the relocated JsonFormat
8276
*
8377
* @param message The incoming (unrelocated) protobuf message.
84-
* @param span The span to which to attach the attribute.
85-
* @param key The attribute key.
8678
*/
87-
public static void addConvertedMessageAttribute(
88-
Object message, Span span, AttributeKey<String> key) {
79+
public static String getMessage(Message message) {
8980
try {
9081
// Convert the unrelocated message into a relocated DynamicMessage.
9182
DynamicMessage relocatedMessage = convertToRelocatedDynamicMessage(message);
@@ -94,11 +85,10 @@ public static void addConvertedMessageAttribute(
9485
JsonFormat.Printer relocatedPrinter = JsonFormat.printer();
9586
String jsonOutput = relocatedPrinter.print(relocatedMessage);
9687

97-
// Set the JSON output as a span attribute.
98-
span.setAttribute(key, jsonOutput);
88+
return jsonOutput;
9989
} catch (Exception e) {
100-
System.err.println("Failed to convert message via byte-array round-trip: " + e.getMessage());
101-
e.printStackTrace();
90+
log.error("Failed to convert message with relocated protobuf message: {}", e.getMessage());
10291
}
92+
return "";
10393
}
10494
}

0 commit comments

Comments
 (0)