diff --git a/google-cloud-clients/google-cloud-logging/src/main/java/com/google/cloud/logging/LogEntry.java b/google-cloud-clients/google-cloud-logging/src/main/java/com/google/cloud/logging/LogEntry.java index b2fbcb310ebd..7db2e9e7d555 100644 --- a/google-cloud-clients/google-cloud-logging/src/main/java/com/google/cloud/logging/LogEntry.java +++ b/google-cloud-clients/google-cloud-logging/src/main/java/com/google/cloud/logging/LogEntry.java @@ -65,8 +65,8 @@ public LogEntry apply(com.google.logging.v2.LogEntry pb) { private final HttpRequest httpRequest; private final Map labels; private final Operation operation; - private final String trace; - private final String spanId; + private final Object trace; + private final Object spanId; private final boolean traceSampled; private final SourceLocation sourceLocation; private final Payload payload; @@ -85,8 +85,8 @@ public static class Builder { private HttpRequest httpRequest; private Map labels = new HashMap<>(); private Operation operation; - private String trace; - private String spanId; + private Object trace; + private Object spanId; private boolean traceSampled; private SourceLocation sourceLocation; private Payload payload; @@ -223,7 +223,6 @@ public Builder setOperation(Operation operation) { return this; } - /** * Sets the resource name of the trace associated with the log entry, if any. If it contains a * relative resource name, the name is assumed to be relative to `//tracing.googleapis.com`. @@ -233,6 +232,14 @@ public Builder setTrace(String trace) { return this; } + /** + * Sets the resource name of the trace associated with the log entry, if any. If it contains a + * relative resource name, the name is assumed to be relative to `//tracing.googleapis.com`. + */ + public Builder setTrace(Object trace) { + this.trace = trace; + return this; + } /** * Sets the ID of the trace span associated with the log entry, if any. @@ -242,6 +249,14 @@ public Builder setSpanId(String spanId) { return this; } + /** + * Sets the ID of the trace span associated with the log entry, if any. + */ + public Builder setSpanId(Object spanId) { + this.spanId = spanId; + return this; + } + /** * Sets the sampling decision of the trace span associated with the log entry. @@ -385,7 +400,8 @@ public Operation getOperation() { * relative resource name, the name is assumed to be relative to `//tracing.googleapis.com`. */ public String getTrace() { - return trace; + // For backwards compatibility return null when trace not set instead of "null". + return trace == null ? null : String.valueOf(trace); } @@ -393,7 +409,8 @@ public String getTrace() { * Returns the ID of the trace span associated with the log entry, if any. */ public String getSpanId() { - return spanId; + // For backwards compatibility return null when spanId not set instead of "null". + return spanId == null ? null : String.valueOf(spanId); } @@ -429,7 +446,7 @@ public T getPayload() { @Override public int hashCode() { return Objects.hash(logName, resource, timestamp, receiveTimestamp, severity, insertId, - httpRequest, labels, operation, trace, spanId, traceSampled, sourceLocation, payload); + httpRequest, labels, operation, getTrace(), getSpanId(), traceSampled, sourceLocation, payload); } @Override @@ -450,8 +467,8 @@ public boolean equals(Object obj) { && Objects.equals(httpRequest, other.httpRequest) && Objects.equals(labels, other.labels) && Objects.equals(operation, other.operation) - && Objects.equals(trace, other.trace) - && Objects.equals(spanId, other.spanId) + && Objects.equals(getTrace(), other.getTrace()) + && Objects.equals(getSpanId(), other.getSpanId()) && Objects.equals(traceSampled, other.traceSampled) && Objects.equals(sourceLocation, other.sourceLocation) && Objects.equals(payload, other.payload); @@ -524,10 +541,10 @@ com.google.logging.v2.LogEntry toPb(String projectId) { builder.setOperation(operation.toPb()); } if (trace != null) { - builder.setTrace(trace); + builder.setTrace(getTrace()); } if (spanId != null) { - builder.setSpanId(spanId); + builder.setSpanId(getSpanId()); } builder.setTraceSampled(traceSampled); if (sourceLocation != null) { diff --git a/google-cloud-clients/google-cloud-logging/src/test/java/com/google/cloud/logging/LogEntryTest.java b/google-cloud-clients/google-cloud-logging/src/test/java/com/google/cloud/logging/LogEntryTest.java index 932333525eec..f8cb14440167 100644 --- a/google-cloud-clients/google-cloud-logging/src/test/java/com/google/cloud/logging/LogEntryTest.java +++ b/google-cloud-clients/google-cloud-logging/src/test/java/com/google/cloud/logging/LogEntryTest.java @@ -51,7 +51,19 @@ public class LogEntryTest { ImmutableMap.of("key1", "value1", "key2", "value2"); private static final Operation OPERATION = Operation.of("id", "producer"); private static final String TRACE = "trace"; + private static final Object TRACE_FORMATTER = new Object() { + @Override + public String toString() { + return TRACE; + } + }; private static final String SPAN_ID = "spanId"; + private static final Object SPAN_ID_FORMATTER = new Object() { + @Override + public String toString() { + return SPAN_ID; + } + }; private static final boolean TRACE_SAMPLED = true; private static final SourceLocation SOURCE_LOCATION = new SourceLocation.Builder() .setFile("file") @@ -73,8 +85,8 @@ public class LogEntryTest { .setHttpRequest(HTTP_REQUEST) .setLabels(LABELS) .setOperation(OPERATION) - .setTrace(TRACE) - .setSpanId(SPAN_ID) + .setTrace(TRACE_FORMATTER) + .setSpanId(SPAN_ID_FORMATTER) .setTraceSampled(TRACE_SAMPLED) .setSourceLocation(SOURCE_LOCATION) .build(); @@ -88,8 +100,8 @@ public class LogEntryTest { .setHttpRequest(HTTP_REQUEST) .setLabels(LABELS) .setOperation(OPERATION) - .setTrace(TRACE) - .setSpanId(SPAN_ID) + .setTrace(TRACE_FORMATTER) + .setSpanId(SPAN_ID_FORMATTER) .setTraceSampled(TRACE_SAMPLED) .setSourceLocation(SOURCE_LOCATION) .build(); @@ -103,8 +115,8 @@ public class LogEntryTest { .setHttpRequest(HTTP_REQUEST) .setLabels(LABELS) .setOperation(OPERATION) - .setTrace(TRACE) - .setSpanId(SPAN_ID) + .setTrace(TRACE_FORMATTER) + .setSpanId(SPAN_ID_FORMATTER) .setTraceSampled(TRACE_SAMPLED) .setSourceLocation(SOURCE_LOCATION) .build();