From 22f375e7fd121a229b8ff89870192ad530309647 Mon Sep 17 00:00:00 2001 From: Kristen Kozak Date: Thu, 25 Oct 2018 18:04:17 -0700 Subject: [PATCH] Add a traceSampled field to the google-cloud-logging LogEntry class. The new field was added to the Stackdriver LogEntry protocol buffer message in https://github.com/googleapis/googleapis/commit/7b34e5a3ed10775fe14d15ffa842870dc3b5400e#diff-a51916a680df6453b6d86586cd7d63f9R149. --- .../com/google/cloud/logging/LogEntry.java | 28 ++++++++++++++++++- .../google/cloud/logging/LogEntryTest.java | 16 +++++++++++ 2 files changed, 43 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 72be3bc22fd5..b2fbcb310ebd 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 @@ -67,6 +67,7 @@ public LogEntry apply(com.google.logging.v2.LogEntry pb) { private final Operation operation; private final String trace; private final String spanId; + private final boolean traceSampled; private final SourceLocation sourceLocation; private final Payload payload; @@ -86,6 +87,7 @@ public static class Builder { private Operation operation; private String trace; private String spanId; + private boolean traceSampled; private SourceLocation sourceLocation; private Payload payload; @@ -105,6 +107,7 @@ public static class Builder { this.operation = entry.operation; this.trace = entry.trace; this.spanId = entry.spanId; + this.traceSampled = entry.traceSampled; this.sourceLocation = entry.sourceLocation; this.payload = entry.payload; } @@ -240,6 +243,15 @@ public Builder setSpanId(String spanId) { } + /** + * Sets the sampling decision of the trace span associated with the log entry. + */ + public Builder setTraceSampled(boolean traceSampled) { + this.traceSampled = traceSampled; + return this; + } + + /** * Sets the source code location information associated with the log entry if any. */ @@ -281,6 +293,7 @@ public LogEntry build() { this.operation = builder.operation; this.trace = builder.trace; this.spanId = builder.spanId; + this.traceSampled = builder.traceSampled; this.sourceLocation = builder.sourceLocation; this.payload = builder.payload; } @@ -384,6 +397,15 @@ public String getSpanId() { } + /** + * Returns the sampling decision of the trace span associated with the log entry, or + * {@code false} if there is no trace span. + */ + public boolean getTraceSampled() { + return traceSampled; + } + + /** * Returns the source code location information associated with the log entry, if any. */ @@ -407,7 +429,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, trace, spanId, traceSampled, sourceLocation, payload); } @Override @@ -430,6 +452,7 @@ public boolean equals(Object obj) { && Objects.equals(operation, other.operation) && Objects.equals(trace, other.trace) && Objects.equals(spanId, other.spanId) + && Objects.equals(traceSampled, other.traceSampled) && Objects.equals(sourceLocation, other.sourceLocation) && Objects.equals(payload, other.payload); } @@ -448,6 +471,7 @@ public String toString() { .add("operation", operation) .add("trace", trace) .add("spanId", spanId) + .add("traceSampled", traceSampled) .add("sourceLocation", sourceLocation) .add("payload", payload) .toString(); @@ -505,6 +529,7 @@ com.google.logging.v2.LogEntry toPb(String projectId) { if (spanId != null) { builder.setSpanId(spanId); } + builder.setTraceSampled(traceSampled); if (sourceLocation != null) { builder.setSourceLocation(sourceLocation.toPb()); } @@ -572,6 +597,7 @@ static LogEntry fromPb(com.google.logging.v2.LogEntry entryPb) { if (!entryPb.getSpanId().equals("")) { builder.setSpanId(entryPb.getSpanId()); } + builder.setTraceSampled(entryPb.getTraceSampled()); 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 9de4e89a2a28..932333525eec 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 @@ -17,6 +17,7 @@ package com.google.cloud.logging; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; import com.google.cloud.MonitoredResource; @@ -51,6 +52,7 @@ public class LogEntryTest { 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 boolean TRACE_SAMPLED = true; private static final SourceLocation SOURCE_LOCATION = new SourceLocation.Builder() .setFile("file") .setLine(42L) @@ -73,6 +75,7 @@ public class LogEntryTest { .setOperation(OPERATION) .setTrace(TRACE) .setSpanId(SPAN_ID) + .setTraceSampled(TRACE_SAMPLED) .setSourceLocation(SOURCE_LOCATION) .build(); private static final LogEntry JSON_ENTRY = LogEntry.newBuilder(JSON_PAYLOAD) @@ -87,6 +90,7 @@ public class LogEntryTest { .setOperation(OPERATION) .setTrace(TRACE) .setSpanId(SPAN_ID) + .setTraceSampled(TRACE_SAMPLED) .setSourceLocation(SOURCE_LOCATION) .build(); private static final LogEntry PROTO_ENTRY = LogEntry.newBuilder(PROTO_PAYLOAD) @@ -101,6 +105,7 @@ public class LogEntryTest { .setOperation(OPERATION) .setTrace(TRACE) .setSpanId(SPAN_ID) + .setTraceSampled(TRACE_SAMPLED) .setSourceLocation(SOURCE_LOCATION) .build(); @@ -119,6 +124,7 @@ public void testOf() { assertNull(logEntry.getOperation()); assertNull(logEntry.getTrace()); assertNull(logEntry.getSpanId()); + assertFalse(logEntry.getTraceSampled()); assertNull(logEntry.getSourceLocation()); logEntry = LogEntry.of(LOG_NAME, RESOURCE, STRING_PAYLOAD); assertEquals(STRING_PAYLOAD, logEntry.getPayload()); @@ -134,6 +140,7 @@ public void testOf() { assertNull(logEntry.getOperation()); assertNull(logEntry.getTrace()); assertNull(logEntry.getSpanId()); + assertFalse(logEntry.getTraceSampled()); assertNull(logEntry.getSourceLocation()); } @@ -150,6 +157,7 @@ public void testBuilder() { assertEquals(OPERATION, STRING_ENTRY.getOperation()); assertEquals(TRACE, STRING_ENTRY.getTrace()); assertEquals(SPAN_ID, STRING_ENTRY.getSpanId()); + assertEquals(TRACE_SAMPLED, STRING_ENTRY.getTraceSampled()); assertEquals(SOURCE_LOCATION, STRING_ENTRY.getSourceLocation()); assertEquals(STRING_PAYLOAD, STRING_ENTRY.getPayload()); assertEquals(LOG_NAME, JSON_ENTRY.getLogName()); @@ -163,6 +171,7 @@ public void testBuilder() { assertEquals(OPERATION, JSON_ENTRY.getOperation()); assertEquals(TRACE, JSON_ENTRY.getTrace()); assertEquals(SPAN_ID, JSON_ENTRY.getSpanId()); + assertEquals(TRACE_SAMPLED, JSON_ENTRY.getTraceSampled()); assertEquals(SOURCE_LOCATION, JSON_ENTRY.getSourceLocation()); assertEquals(JSON_PAYLOAD, JSON_ENTRY.getPayload()); assertEquals(LOG_NAME, PROTO_ENTRY.getLogName()); @@ -176,6 +185,7 @@ public void testBuilder() { assertEquals(OPERATION, PROTO_ENTRY.getOperation()); assertEquals(TRACE, PROTO_ENTRY.getTrace()); assertEquals(SPAN_ID, PROTO_ENTRY.getSpanId()); + assertEquals(TRACE_SAMPLED, PROTO_ENTRY.getTraceSampled()); assertEquals(SOURCE_LOCATION, PROTO_ENTRY.getSourceLocation()); assertEquals(PROTO_PAYLOAD, PROTO_ENTRY.getPayload()); LogEntry logEntry = LogEntry.newBuilder(STRING_PAYLOAD) @@ -192,6 +202,7 @@ public void testBuilder() { .setOperation(OPERATION) .setTrace(TRACE) .setSpanId(SPAN_ID) + .setTraceSampled(TRACE_SAMPLED) .setSourceLocation(SOURCE_LOCATION) .build(); assertEquals(LOG_NAME, logEntry.getLogName()); @@ -205,6 +216,7 @@ public void testBuilder() { assertEquals(OPERATION, logEntry.getOperation()); assertEquals(TRACE, logEntry.getTrace()); assertEquals(SPAN_ID, logEntry.getSpanId()); + assertEquals(TRACE_SAMPLED, logEntry.getTraceSampled()); assertEquals(SOURCE_LOCATION, logEntry.getSourceLocation()); assertEquals(StringPayload.of("otherPayload"), logEntry.getPayload()); } @@ -231,6 +243,7 @@ public void testToBuilder() { .setOperation(Operation.of("otherId", "otherProducer")) .setTrace("otherTrace") .setSpanId("otherSpanId") + .setTraceSampled(false) .setSourceLocation(new SourceLocation.Builder().setFile("hey.java").build()) .build(); assertEquals("otherLogName", logEntry.getLogName()); @@ -244,6 +257,7 @@ public void testToBuilder() { assertEquals(Operation.of("otherId", "otherProducer"), logEntry.getOperation()); assertEquals("otherTrace", logEntry.getTrace()); assertEquals("otherSpanId", logEntry.getSpanId()); + assertFalse(logEntry.getTraceSampled()); assertEquals(new SourceLocation.Builder().setFile("hey.java").build(), logEntry.getSourceLocation()); assertEquals(StringPayload.of("otherPayload"), logEntry.getPayload()); @@ -260,6 +274,7 @@ public void testToBuilder() { .setOperation(OPERATION) .setTrace(TRACE) .setSpanId(SPAN_ID) + .setTraceSampled(TRACE_SAMPLED) .setSourceLocation(SOURCE_LOCATION) .build(); compareLogEntry(STRING_ENTRY, logEntry); @@ -289,6 +304,7 @@ private void compareLogEntry(LogEntry expected, LogEntry value) { assertEquals(expected.getOperation(), value.getOperation()); assertEquals(expected.getTrace(), value.getTrace()); assertEquals(expected.getSpanId(), value.getSpanId()); + assertEquals(expected.getTraceSampled(), value.getTraceSampled()); assertEquals(expected.getSourceLocation(), value.getSourceLocation()); assertEquals(expected.getPayload(), value.getPayload()); assertEquals(expected.hashCode(), value.hashCode());