From bf63680e3cc95863dd75f477d7c887f68a185c3f Mon Sep 17 00:00:00 2001 From: Bogdan Drutu Date: Mon, 10 Sep 2018 13:07:04 -0700 Subject: [PATCH 1/2] Allow deferred formating for trace and spanId. --- .../com/google/cloud/logging/LogEntry.java | 28 ++++++++++--------- .../google/cloud/logging/LogEntryTest.java | 24 ++++++++++++---- 2 files changed, 33 insertions(+), 19 deletions(-) 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 72be3bc22fd5..9ce2701602a5 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 SourceLocation sourceLocation; private final Payload payload; @@ -84,8 +84,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 SourceLocation sourceLocation; private Payload payload; @@ -225,7 +225,7 @@ public Builder setOperation(Operation operation) { * 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(String trace) { + public Builder setTrace(Object trace) { this.trace = trace; return this; } @@ -234,7 +234,7 @@ public Builder setTrace(String trace) { /** * Sets the ID of the trace span associated with the log entry, if any. */ - public Builder setSpanId(String spanId) { + public Builder setSpanId(Object spanId) { this.spanId = spanId; return this; } @@ -372,7 +372,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); } @@ -380,7 +381,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); } @@ -407,7 +409,7 @@ public T getPayload() { @Override public int hashCode() { return Objects.hash(logName, resource, timestamp, receiveTimestamp, severity, insertId, - httpRequest, labels, operation, trace, spanId, sourceLocation, payload); + httpRequest, labels, operation, getTrace(), getSpanId(), sourceLocation, payload); } @Override @@ -428,8 +430,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(sourceLocation, other.sourceLocation) && Objects.equals(payload, other.payload); } @@ -500,10 +502,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()); } if (sourceLocation != null) { builder.setSourceLocation(sourceLocation.toPb()); 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 9de4e89a2a28..2022456e480f 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 @@ -50,7 +50,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 SourceLocation SOURCE_LOCATION = new SourceLocation.Builder() .setFile("file") .setLine(42L) @@ -71,8 +83,8 @@ public class LogEntryTest { .setHttpRequest(HTTP_REQUEST) .setLabels(LABELS) .setOperation(OPERATION) - .setTrace(TRACE) - .setSpanId(SPAN_ID) + .setTrace(TRACE_FORMATTER) + .setSpanId(SPAN_ID_FORMATTER) .setSourceLocation(SOURCE_LOCATION) .build(); private static final LogEntry JSON_ENTRY = LogEntry.newBuilder(JSON_PAYLOAD) @@ -85,8 +97,8 @@ public class LogEntryTest { .setHttpRequest(HTTP_REQUEST) .setLabels(LABELS) .setOperation(OPERATION) - .setTrace(TRACE) - .setSpanId(SPAN_ID) + .setTrace(TRACE_FORMATTER) + .setSpanId(SPAN_ID_FORMATTER) .setSourceLocation(SOURCE_LOCATION) .build(); private static final LogEntry PROTO_ENTRY = LogEntry.newBuilder(PROTO_PAYLOAD) @@ -99,8 +111,8 @@ public class LogEntryTest { .setHttpRequest(HTTP_REQUEST) .setLabels(LABELS) .setOperation(OPERATION) - .setTrace(TRACE) - .setSpanId(SPAN_ID) + .setTrace(TRACE_FORMATTER) + .setSpanId(SPAN_ID_FORMATTER) .setSourceLocation(SOURCE_LOCATION) .build(); From a2c45fdc1959e356d0f1f9f2e359d64e9aa8c004 Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Wed, 28 Nov 2018 13:13:23 -0800 Subject: [PATCH 2/2] Add back setTrace(String) and setSpanId(String) for binary compatibility --- .../java/com/google/cloud/logging/LogEntry.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) 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 9a36967d8b4c..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 @@ -223,6 +223,14 @@ 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`. + */ + public Builder setTrace(String trace) { + this.trace = trace; + return this; + } /** * Sets the resource name of the trace associated with the log entry, if any. If it contains a @@ -233,6 +241,13 @@ public Builder setTrace(Object trace) { return this; } + /** + * Sets the ID of the trace span associated with the log entry, if any. + */ + public Builder setSpanId(String spanId) { + this.spanId = spanId; + return this; + } /** * Sets the ID of the trace span associated with the log entry, if any. @@ -431,7 +446,7 @@ public T getPayload() { @Override public int hashCode() { return Objects.hash(logName, resource, timestamp, receiveTimestamp, severity, insertId, - httpRequest, labels, operation, getTrace(), getSpanId, traceSampled, sourceLocation, payload); + httpRequest, labels, operation, getTrace(), getSpanId(), traceSampled, sourceLocation, payload); } @Override