Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
removing unnecessary lines
  • Loading branch information
thugrock7 committed Apr 22, 2025
commit bcf0b52fe7e48d0e63041f10a7a0a148d81b406a
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import io.grpc.Metadata.Key;
import io.opentelemetry.api.common.AttributeKey;
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.util.LinkedHashMap;
import java.util.Map;
Expand All @@ -35,16 +34,17 @@ public class GrpcSpanDecorator {
private GrpcSpanDecorator() {}

private static final Logger log = LoggerFactory.getLogger(GrpcSpanDecorator.class);
private static final JsonFormat.Printer PRINTER = JsonFormat.printer();

public static void addMessageAttribute(Object message, Span span, AttributeKey<String> key) {
if (message instanceof Message) {
Message mb = (Message) message;
try {
String jsonOutput = ProtobufMessageConverter.getMessage(mb);
span.setAttribute(key, jsonOutput);
if (jsonOutput != null && !jsonOutput.isEmpty()) {
span.setAttribute(key, jsonOutput);
}
} catch (Exception e) {
log.debug("Failed to decode message as JSON", e);
log.debug("Failed to decode message as JSON: {}", e.getMessage(), 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
Expand Up @@ -165,28 +165,21 @@ private static FileDescriptor processFileDescriptorWithDependencies(
* the relocated JsonFormat
*
* @param message The incoming (unrelocated) protobuf message.
* @return JSON string representation of the message
* @throws Exception if conversion fails
Comment thread
thugrock7 marked this conversation as resolved.
*/
public static String getMessage(Message message) {
public static String getMessage(Message message) throws Exception {
if (message == null) {
log.debug("Cannot convert null message to JSON");
return "";
}

try {
// Convert the unrelocated message into a relocated DynamicMessage.
DynamicMessage relocatedMessage = convertToRelocatedDynamicMessage(message);
// 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().includingDefaultValueFields().preservingProtoFieldNames();
return relocatedPrinter.print(relocatedMessage);
} catch (Exception e) {
log.error("Failed to convert message to JSON: {}", e.getMessage(), e);
if (log.isDebugEnabled()) {
log.debug("Message type: {}", message.getClass().getName());
log.debug("Message descriptor: {}", message.getDescriptorForType().getFullName());
}
}
return "";
// Use the relocated JsonFormat to print the message as JSON.
JsonFormat.Printer relocatedPrinter =
JsonFormat.printer().includingDefaultValueFields().preservingProtoFieldNames();
return relocatedPrinter.print(relocatedMessage);
}
}
Loading