Skip to content

Commit

Permalink
Add a spanId field to the google-cloud-logging LogEntry class. (#3332)
Browse files Browse the repository at this point in the history
The new field is consistent with the span_id field in the Stackdriver LogEntry
protocol buffer message.  Fixes #3325.
  • Loading branch information
sebright authored and yihanzhen committed Jun 4, 2018
1 parent ce5c6d8 commit db2da59
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public LogEntry apply(com.google.logging.v2.LogEntry pb) {
private final Map<String, String> labels;
private final Operation operation;
private final String trace;
private final String spanId;
private final SourceLocation sourceLocation;
private final Payload<?> payload;

Expand All @@ -84,6 +85,7 @@ public static class Builder {
private Map<String, String> labels = new HashMap<>();
private Operation operation;
private String trace;
private String spanId;
private SourceLocation sourceLocation;
private Payload<?> payload;

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

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

Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -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)
Expand All @@ -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());
Expand All @@ -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());
}
Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -246,6 +259,7 @@ public void testToBuilder() {
.setLabels(LABELS)
.setOperation(OPERATION)
.setTrace(TRACE)
.setSpanId(SPAN_ID)
.setSourceLocation(SOURCE_LOCATION)
.build();
compareLogEntry(STRING_ENTRY, logEntry);
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit db2da59

Please sign in to comment.