Skip to content

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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion instrumentation/grpc-1.6/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ muzzle {
group = "io.grpc"
module = "grpc-core"
versions = "[1.6.0,)"
// for body capture via com.google.protobuf.Message to exist always

extraDependency("com.google.protobuf:protobuf-java:3.3.1")
extraDependency("io.grpc:grpc-netty:1.6.0")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,10 @@
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.javaagent.instrumentation.hypertrace.com.google.protobuf.util.JsonFormat;
import io.opentelemetry.javaagent.instrumentation.hypertrace.grpc.GrpcSemanticAttributes;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Function;
import org.hypertrace.agent.core.instrumentation.HypertraceSemanticAttributes;
import org.hypertrace.agent.core.instrumentation.buffer.BoundedBuffersFactory;
import org.hypertrace.agent.core.instrumentation.buffer.BoundedCharArrayWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -44,12 +41,13 @@ public static void addMessageAttribute(Object message, Span span, AttributeKey<S
if (message instanceof Message) {
Message mb = (Message) message;
try {
BoundedCharArrayWriter writer = BoundedBuffersFactory.createWriter();
PRINTER.appendTo(mb, writer);
span.setAttribute(key, writer.toString());
} catch (IOException e) {
log.error("Failed to decode message to JSON", e);
String jsonOutput = ProtobufMessageConverter.getMessage(mb);
Copy link
Contributor

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?

Copy link
Contributor Author

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.

span.setAttribute(key, jsonOutput);
} catch (Exception e) {
log.error("Failed to decode message as JSON", e);
}
} else {
log.debug("message is not an instance of com.google.protobuf.Message");
}
}

Expand Down
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 "";
}
}
9 changes: 5 additions & 4 deletions shaded-protobuf-java-util/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ plugins {
}

dependencies {
implementation("com.google.protobuf:protobuf-java-util:3.25.5") {
exclude("com.google.protobuf", "protobuf-java")
exclude("com.google.guava", "guava")
}
implementation("com.google.protobuf:protobuf-java-util:3.25.5")

// fix vulnerability
constraints {
implementation("com.google.code.gson:gson:2.8.9")
}
}

tasks.shadowJar {
relocate("com.google.protobuf", "io.opentelemetry.javaagent.instrumentation.hypertrace.com.google.protobuf")
relocate("com.google.protobuf.util", "io.opentelemetry.javaagent.instrumentation.hypertrace.com.google.protobuf.util")
relocate("com.google.gson", "io.opentelemetry.javaagent.instrumentation.hypertrace.com.google.gson")
relocate("com.google.common", "io.opentelemetry.javaagent.instrumentation.hypertrace.com.google.common") // Add this
relocate("com.google.guava", "io.opentelemetry.javaagent.instrumentation.hypertrace.com.google.guava")
}
Loading