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 c42125a0e60a..72be3bc22fd5 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 @@ -66,6 +66,7 @@ public LogEntry apply(com.google.logging.v2.LogEntry pb) { private final Map labels; private final Operation operation; private final String trace; + private final String spanId; private final SourceLocation sourceLocation; private final Payload payload; @@ -84,6 +85,7 @@ public static class Builder { private Map labels = new HashMap<>(); private Operation operation; private String trace; + private String spanId; private SourceLocation sourceLocation; private Payload payload; @@ -102,6 +104,7 @@ public static class Builder { this.labels = new HashMap<>(entry.labels); this.operation = entry.operation; this.trace = entry.trace; + this.spanId = entry.spanId; this.sourceLocation = entry.sourceLocation; this.payload = entry.payload; } @@ -228,6 +231,15 @@ public Builder setTrace(String trace) { } + /** + * 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 source code location information associated with the log entry if any. */ @@ -268,6 +280,7 @@ public LogEntry build() { this.labels = ImmutableMap.copyOf(builder.labels); this.operation = builder.operation; this.trace = builder.trace; + this.spanId = builder.spanId; this.sourceLocation = builder.sourceLocation; this.payload = builder.payload; } @@ -363,6 +376,14 @@ public String getTrace() { } + /** + * Returns the ID of the trace span associated with the log entry, if any. + */ + public String getSpanId() { + return spanId; + } + + /** * Returns the source code location information associated with the log entry, if any. */ @@ -386,7 +407,7 @@ public T getPayload() { @Override public int hashCode() { return Objects.hash(logName, resource, timestamp, receiveTimestamp, severity, insertId, - httpRequest, labels, operation, trace, sourceLocation, payload); + httpRequest, labels, operation, trace, spanId, sourceLocation, payload); } @Override @@ -408,6 +429,7 @@ public boolean equals(Object obj) { && Objects.equals(labels, other.labels) && Objects.equals(operation, other.operation) && Objects.equals(trace, other.trace) + && Objects.equals(spanId, other.spanId) && Objects.equals(sourceLocation, other.sourceLocation) && Objects.equals(payload, other.payload); } @@ -425,6 +447,7 @@ public String toString() { .add("labels", labels) .add("operation", operation) .add("trace", trace) + .add("spanId", spanId) .add("sourceLocation", sourceLocation) .add("payload", payload) .toString(); @@ -479,6 +502,9 @@ com.google.logging.v2.LogEntry toPb(String projectId) { if (trace != null) { builder.setTrace(trace); } + if (spanId != null) { + builder.setSpanId(spanId); + } if (sourceLocation != null) { builder.setSourceLocation(sourceLocation.toPb()); } @@ -543,6 +569,9 @@ static LogEntry fromPb(com.google.logging.v2.LogEntry entryPb) { if (!entryPb.getTrace().equals("")) { builder.setTrace(entryPb.getTrace()); } + if (!entryPb.getSpanId().equals("")) { + builder.setSpanId(entryPb.getSpanId()); + } if (!entryPb.getSourceLocation().equals(LogEntrySourceLocation.getDefaultInstance())) { builder.setSourceLocation(SourceLocation.fromPb(entryPb.getSourceLocation())); } 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 3a7953234233..9de4e89a2a28 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,6 +50,7 @@ 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 String SPAN_ID = "spanId"; private static final SourceLocation SOURCE_LOCATION = new SourceLocation.Builder() .setFile("file") .setLine(42L) @@ -71,6 +72,7 @@ public class LogEntryTest { .setLabels(LABELS) .setOperation(OPERATION) .setTrace(TRACE) + .setSpanId(SPAN_ID) .setSourceLocation(SOURCE_LOCATION) .build(); private static final LogEntry JSON_ENTRY = LogEntry.newBuilder(JSON_PAYLOAD) @@ -84,6 +86,7 @@ public class LogEntryTest { .setLabels(LABELS) .setOperation(OPERATION) .setTrace(TRACE) + .setSpanId(SPAN_ID) .setSourceLocation(SOURCE_LOCATION) .build(); private static final LogEntry PROTO_ENTRY = LogEntry.newBuilder(PROTO_PAYLOAD) @@ -97,6 +100,7 @@ public class LogEntryTest { .setLabels(LABELS) .setOperation(OPERATION) .setTrace(TRACE) + .setSpanId(SPAN_ID) .setSourceLocation(SOURCE_LOCATION) .build(); @@ -114,6 +118,7 @@ public void testOf() { assertNull(logEntry.getHttpRequest()); assertNull(logEntry.getOperation()); assertNull(logEntry.getTrace()); + assertNull(logEntry.getSpanId()); assertNull(logEntry.getSourceLocation()); logEntry = LogEntry.of(LOG_NAME, RESOURCE, STRING_PAYLOAD); assertEquals(STRING_PAYLOAD, logEntry.getPayload()); @@ -128,6 +133,7 @@ public void testOf() { assertNull(logEntry.getHttpRequest()); assertNull(logEntry.getOperation()); assertNull(logEntry.getTrace()); + assertNull(logEntry.getSpanId()); assertNull(logEntry.getSourceLocation()); } @@ -143,6 +149,7 @@ public void testBuilder() { assertEquals(LABELS, STRING_ENTRY.getLabels()); assertEquals(OPERATION, STRING_ENTRY.getOperation()); assertEquals(TRACE, STRING_ENTRY.getTrace()); + assertEquals(SPAN_ID, STRING_ENTRY.getSpanId()); assertEquals(SOURCE_LOCATION, STRING_ENTRY.getSourceLocation()); assertEquals(STRING_PAYLOAD, STRING_ENTRY.getPayload()); assertEquals(LOG_NAME, JSON_ENTRY.getLogName()); @@ -155,6 +162,7 @@ public void testBuilder() { assertEquals(LABELS, JSON_ENTRY.getLabels()); assertEquals(OPERATION, JSON_ENTRY.getOperation()); assertEquals(TRACE, JSON_ENTRY.getTrace()); + assertEquals(SPAN_ID, JSON_ENTRY.getSpanId()); assertEquals(SOURCE_LOCATION, JSON_ENTRY.getSourceLocation()); assertEquals(JSON_PAYLOAD, JSON_ENTRY.getPayload()); assertEquals(LOG_NAME, PROTO_ENTRY.getLogName()); @@ -167,6 +175,7 @@ public void testBuilder() { assertEquals(LABELS, PROTO_ENTRY.getLabels()); assertEquals(OPERATION, PROTO_ENTRY.getOperation()); assertEquals(TRACE, PROTO_ENTRY.getTrace()); + assertEquals(SPAN_ID, PROTO_ENTRY.getSpanId()); assertEquals(SOURCE_LOCATION, PROTO_ENTRY.getSourceLocation()); assertEquals(PROTO_PAYLOAD, PROTO_ENTRY.getPayload()); LogEntry logEntry = LogEntry.newBuilder(STRING_PAYLOAD) @@ -182,6 +191,7 @@ public void testBuilder() { .addLabel("key2", "value2") .setOperation(OPERATION) .setTrace(TRACE) + .setSpanId(SPAN_ID) .setSourceLocation(SOURCE_LOCATION) .build(); assertEquals(LOG_NAME, logEntry.getLogName()); @@ -194,6 +204,7 @@ public void testBuilder() { assertEquals(LABELS, logEntry.getLabels()); assertEquals(OPERATION, logEntry.getOperation()); assertEquals(TRACE, logEntry.getTrace()); + assertEquals(SPAN_ID, logEntry.getSpanId()); assertEquals(SOURCE_LOCATION, logEntry.getSourceLocation()); assertEquals(StringPayload.of("otherPayload"), logEntry.getPayload()); } @@ -219,6 +230,7 @@ public void testToBuilder() { .addLabel("key", "value") .setOperation(Operation.of("otherId", "otherProducer")) .setTrace("otherTrace") + .setSpanId("otherSpanId") .setSourceLocation(new SourceLocation.Builder().setFile("hey.java").build()) .build(); assertEquals("otherLogName", logEntry.getLogName()); @@ -231,6 +243,7 @@ public void testToBuilder() { assertEquals(ImmutableMap.of("key", "value"), logEntry.getLabels()); assertEquals(Operation.of("otherId", "otherProducer"), logEntry.getOperation()); assertEquals("otherTrace", logEntry.getTrace()); + assertEquals("otherSpanId", logEntry.getSpanId()); assertEquals(new SourceLocation.Builder().setFile("hey.java").build(), logEntry.getSourceLocation()); assertEquals(StringPayload.of("otherPayload"), logEntry.getPayload()); @@ -246,6 +259,7 @@ public void testToBuilder() { .setLabels(LABELS) .setOperation(OPERATION) .setTrace(TRACE) + .setSpanId(SPAN_ID) .setSourceLocation(SOURCE_LOCATION) .build(); compareLogEntry(STRING_ENTRY, logEntry); @@ -274,6 +288,7 @@ private void compareLogEntry(LogEntry expected, LogEntry value) { assertEquals(expected.getLabels(), value.getLabels()); assertEquals(expected.getOperation(), value.getOperation()); assertEquals(expected.getTrace(), value.getTrace()); + assertEquals(expected.getSpanId(), value.getSpanId()); assertEquals(expected.getSourceLocation(), value.getSourceLocation()); assertEquals(expected.getPayload(), value.getPayload()); assertEquals(expected.hashCode(), value.hashCode());