Skip to content

Commit

Permalink
Add a traceSampled field to the google-cloud-logging LogEntry class.
Browse files Browse the repository at this point in the history
The new field was added to the Stackdriver LogEntry protocol buffer message in
googleapis/googleapis@7b34e5a#diff-a51916a680df6453b6d86586cd7d63f9R149.
  • Loading branch information
sebright committed Oct 30, 2018
1 parent f69e7fa commit 6ad70dc
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand All @@ -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;
}
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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.
*/
Expand All @@ -407,7 +429,7 @@ public <T extends Payload> 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
Expand All @@ -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);
}
Expand All @@ -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();
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -101,6 +105,7 @@ public class LogEntryTest {
.setOperation(OPERATION)
.setTrace(TRACE)
.setSpanId(SPAN_ID)
.setTraceSampled(TRACE_SAMPLED)
.setSourceLocation(SOURCE_LOCATION)
.build();

Expand All @@ -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());
Expand All @@ -134,6 +140,7 @@ public void testOf() {
assertNull(logEntry.getOperation());
assertNull(logEntry.getTrace());
assertNull(logEntry.getSpanId());
assertFalse(logEntry.getTraceSampled());
assertNull(logEntry.getSourceLocation());
}

Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -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)
Expand All @@ -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());
Expand All @@ -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());
}
Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -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);
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit 6ad70dc

Please sign in to comment.