-
Notifications
You must be signed in to change notification settings - Fork 14
feat: use shaded protobuf-java , instead of applications #414
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
Merged
shashank11p
merged 5 commits into
hypertrace:main
from
thugrock7:protobuf-java-dependency-exclusion
Feb 14, 2025
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
...pentelemetry/javaagent/instrumentation/hypertrace/grpc/v1_6/ProtobufMessageConverter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* Copyright The Hypertrace Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.hypertrace.grpc.v1_6; | ||
|
||
import com.google.protobuf.Descriptors; | ||
import com.google.protobuf.Message; | ||
import io.opentelemetry.javaagent.instrumentation.hypertrace.com.google.protobuf.DescriptorProtos.FileDescriptorProto; | ||
import io.opentelemetry.javaagent.instrumentation.hypertrace.com.google.protobuf.Descriptors.Descriptor; | ||
import io.opentelemetry.javaagent.instrumentation.hypertrace.com.google.protobuf.Descriptors.FileDescriptor; | ||
import io.opentelemetry.javaagent.instrumentation.hypertrace.com.google.protobuf.DynamicMessage; | ||
import io.opentelemetry.javaagent.instrumentation.hypertrace.com.google.protobuf.util.JsonFormat; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class ProtobufMessageConverter { | ||
private static final Logger log = LoggerFactory.getLogger(ProtobufMessageConverter.class); | ||
/** | ||
* Converts an unrelocated protobuf message into a relocated DynamicMessage via a byte-array | ||
* round-trip. | ||
* | ||
* @param message The original protobuf message (an instance of com.google.protobuf.Message). | ||
* @return A relocated DynamicMessage built from your relocated protobuf classes. | ||
* @throws Exception if conversion fails. | ||
*/ | ||
public static DynamicMessage convertToRelocatedDynamicMessage(Message message) throws Exception { | ||
// 1. Serialize the original message to bytes. | ||
byte[] messageBytes = message.toByteArray(); | ||
|
||
// 2. Obtain the original (unrelocated) message descriptor. | ||
Descriptors.Descriptor originalDescriptor = message.getDescriptorForType(); | ||
|
||
// 3. Get the unrelocated file descriptor and its proto representation. | ||
Descriptors.FileDescriptor unrelocatedFileDescriptor = originalDescriptor.getFile(); | ||
com.google.protobuf.DescriptorProtos.FileDescriptorProto unrelocatedFileProto = | ||
unrelocatedFileDescriptor.toProto(); | ||
byte[] fileProtoBytes = unrelocatedFileProto.toByteArray(); | ||
|
||
// 4. Parse the file descriptor proto using relocated classes. | ||
// This converts the unrelocated FileDescriptorProto into your relocated FileDescriptorProto. | ||
FileDescriptorProto relocatedFileProto = FileDescriptorProto.parseFrom(fileProtoBytes); | ||
|
||
// 5. Build the relocated FileDescriptor. | ||
FileDescriptor relocatedFileDescriptor = | ||
FileDescriptor.buildFrom(relocatedFileProto, new FileDescriptor[] {}); | ||
|
||
// 6. Find the relocated message descriptor by name. | ||
Descriptor relocatedDescriptor = | ||
relocatedFileDescriptor.findMessageTypeByName(originalDescriptor.getName()); | ||
if (relocatedDescriptor == null) { | ||
throw new IllegalStateException( | ||
"Could not find relocated descriptor for message type: " + originalDescriptor.getName()); | ||
} | ||
|
||
// 7. Parse the original message bytes using the relocated descriptor. | ||
DynamicMessage relocatedMessage = DynamicMessage.parseFrom(relocatedDescriptor, messageBytes); | ||
return relocatedMessage; | ||
} | ||
|
||
/** | ||
* Method that takes an incoming message, converts it to a relocated one, prints it as JSON using | ||
* the relocated JsonFormat | ||
* | ||
* @param message The incoming (unrelocated) protobuf message. | ||
*/ | ||
public static String getMessage(Message message) { | ||
try { | ||
// Convert the unrelocated message into a relocated DynamicMessage. | ||
DynamicMessage relocatedMessage = convertToRelocatedDynamicMessage(message); | ||
|
||
// Use the relocated JsonFormat to print the message as JSON. | ||
JsonFormat.Printer relocatedPrinter = JsonFormat.printer(); | ||
String jsonOutput = relocatedPrinter.print(relocatedMessage); | ||
|
||
return jsonOutput; | ||
} catch (Exception e) { | ||
log.error("Failed to convert message with relocated protobuf message: {}", e.getMessage()); | ||
} | ||
return ""; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need to do this? What was happening while we were directly trying to get it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the object we are getting is of
com.google.protobuf.Message
, this cannot directly be taken as instance of our relocated Message class.Type Incompatibility: Relocated classes are essentially different types, so couldn’t simply cast one into the other without conversion.
Version Mismatch: two versions (e.g., 3.2.0 vs. 3.25.5) have incompatible internal representations. Like application is using protobuf version 3.2.0, but our bundled dependency of protobuf is 3.25.5.
Different Metadata: The unrelocated message carries descriptors/metadata) that the relocated version doesn't recognize directly.