|
| 1 | +/* |
| 2 | + * Copyright The Hypertrace Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.opentelemetry.javaagent.instrumentation.hypertrace.grpc.v1_6; |
| 18 | + |
| 19 | +import com.google.protobuf.Descriptors; |
| 20 | +import com.google.protobuf.Message; |
| 21 | +import io.opentelemetry.api.common.AttributeKey; |
| 22 | +import io.opentelemetry.api.trace.Span; |
| 23 | +import io.opentelemetry.javaagent.instrumentation.hypertrace.com.google.protobuf.DescriptorProtos.FileDescriptorProto; |
| 24 | +import io.opentelemetry.javaagent.instrumentation.hypertrace.com.google.protobuf.Descriptors.Descriptor; |
| 25 | +import io.opentelemetry.javaagent.instrumentation.hypertrace.com.google.protobuf.Descriptors.FileDescriptor; |
| 26 | +import io.opentelemetry.javaagent.instrumentation.hypertrace.com.google.protobuf.DynamicMessage; |
| 27 | +import io.opentelemetry.javaagent.instrumentation.hypertrace.com.google.protobuf.util.JsonFormat; |
| 28 | + |
| 29 | +public class ProtobufRoundTripConverter { |
| 30 | + |
| 31 | + /** |
| 32 | + * Converts an unrelocated protobuf message into a relocated DynamicMessage via a byte-array |
| 33 | + * round-trip. |
| 34 | + * |
| 35 | + * @param message The original protobuf message (an instance of com.google.protobuf.Message). |
| 36 | + * @return A relocated DynamicMessage built from your relocated protobuf classes. |
| 37 | + * @throws Exception if conversion fails. |
| 38 | + */ |
| 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 | + } |
| 43 | + // 1. Serialize the original message to bytes. |
| 44 | + Message originalMessage = (Message) message; |
| 45 | + byte[] messageBytes = originalMessage.toByteArray(); |
| 46 | + |
| 47 | + // 2. Obtain the original (unrelocated) message descriptor. |
| 48 | + Descriptors.Descriptor originalDescriptor = originalMessage.getDescriptorForType(); |
| 49 | + |
| 50 | + // 3. Get the unrelocated file descriptor and its proto representation. |
| 51 | + Descriptors.FileDescriptor unrelocatedFileDescriptor = originalDescriptor.getFile(); |
| 52 | + com.google.protobuf.DescriptorProtos.FileDescriptorProto unrelocatedFileProto = |
| 53 | + unrelocatedFileDescriptor.toProto(); |
| 54 | + byte[] fileProtoBytes = unrelocatedFileProto.toByteArray(); |
| 55 | + |
| 56 | + // 4. Parse the file descriptor proto using relocated classes. |
| 57 | + // This converts the unrelocated FileDescriptorProto into your relocated FileDescriptorProto. |
| 58 | + FileDescriptorProto relocatedFileProto = FileDescriptorProto.parseFrom(fileProtoBytes); |
| 59 | + |
| 60 | + // 5. Build the relocated FileDescriptor. |
| 61 | + // Note: This example assumes there are no dependencies. |
| 62 | + FileDescriptor relocatedFileDescriptor = |
| 63 | + FileDescriptor.buildFrom(relocatedFileProto, new FileDescriptor[] {}); |
| 64 | + |
| 65 | + // 6. Find the relocated message descriptor by name. |
| 66 | + // We assume the message name is the same in both relocated and unrelocated files. |
| 67 | + Descriptor relocatedDescriptor = |
| 68 | + relocatedFileDescriptor.findMessageTypeByName(originalDescriptor.getName()); |
| 69 | + if (relocatedDescriptor == null) { |
| 70 | + throw new IllegalStateException( |
| 71 | + "Could not find relocated descriptor for message type: " + originalDescriptor.getName()); |
| 72 | + } |
| 73 | + |
| 74 | + // 7. Parse the original message bytes using the relocated descriptor. |
| 75 | + DynamicMessage relocatedMessage = DynamicMessage.parseFrom(relocatedDescriptor, messageBytes); |
| 76 | + return relocatedMessage; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 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. |
| 82 | + * |
| 83 | + * @param message The incoming (unrelocated) protobuf message. |
| 84 | + * @param span The span to which to attach the attribute. |
| 85 | + * @param key The attribute key. |
| 86 | + */ |
| 87 | + public static void addConvertedMessageAttribute( |
| 88 | + Object message, Span span, AttributeKey<String> key) { |
| 89 | + try { |
| 90 | + // Convert the unrelocated message into a relocated DynamicMessage. |
| 91 | + DynamicMessage relocatedMessage = convertToRelocatedDynamicMessage(message); |
| 92 | + |
| 93 | + // Use the relocated JsonFormat to print the message as JSON. |
| 94 | + JsonFormat.Printer relocatedPrinter = JsonFormat.printer(); |
| 95 | + String jsonOutput = relocatedPrinter.print(relocatedMessage); |
| 96 | + |
| 97 | + // Set the JSON output as a span attribute. |
| 98 | + span.setAttribute(key, jsonOutput); |
| 99 | + } catch (Exception e) { |
| 100 | + System.err.println("Failed to convert message via byte-array round-trip: " + e.getMessage()); |
| 101 | + e.printStackTrace(); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments