1818
1919import com .google .protobuf .Descriptors ;
2020import com .google .protobuf .Message ;
21- import io .opentelemetry .api .common .AttributeKey ;
22- import io .opentelemetry .api .trace .Span ;
2321import io .opentelemetry .javaagent .instrumentation .hypertrace .com .google .protobuf .DescriptorProtos .FileDescriptorProto ;
2422import io .opentelemetry .javaagent .instrumentation .hypertrace .com .google .protobuf .Descriptors .Descriptor ;
2523import io .opentelemetry .javaagent .instrumentation .hypertrace .com .google .protobuf .Descriptors .FileDescriptor ;
2624import io .opentelemetry .javaagent .instrumentation .hypertrace .com .google .protobuf .DynamicMessage ;
2725import 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