Skip to content

Commit db2da59

Browse files
sebrightyihanzhen
authored andcommitted
Add a spanId field to the google-cloud-logging LogEntry class. (#3332)
The new field is consistent with the span_id field in the Stackdriver LogEntry protocol buffer message. Fixes #3325.
1 parent ce5c6d8 commit db2da59

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

google-cloud-clients/google-cloud-logging/src/main/java/com/google/cloud/logging/LogEntry.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public LogEntry apply(com.google.logging.v2.LogEntry pb) {
6666
private final Map<String, String> labels;
6767
private final Operation operation;
6868
private final String trace;
69+
private final String spanId;
6970
private final SourceLocation sourceLocation;
7071
private final Payload<?> payload;
7172

@@ -84,6 +85,7 @@ public static class Builder {
8485
private Map<String, String> labels = new HashMap<>();
8586
private Operation operation;
8687
private String trace;
88+
private String spanId;
8789
private SourceLocation sourceLocation;
8890
private Payload<?> payload;
8991

@@ -102,6 +104,7 @@ public static class Builder {
102104
this.labels = new HashMap<>(entry.labels);
103105
this.operation = entry.operation;
104106
this.trace = entry.trace;
107+
this.spanId = entry.spanId;
105108
this.sourceLocation = entry.sourceLocation;
106109
this.payload = entry.payload;
107110
}
@@ -228,6 +231,15 @@ public Builder setTrace(String trace) {
228231
}
229232

230233

234+
/**
235+
* Sets the ID of the trace span associated with the log entry, if any.
236+
*/
237+
public Builder setSpanId(String spanId) {
238+
this.spanId = spanId;
239+
return this;
240+
}
241+
242+
231243
/**
232244
* Sets the source code location information associated with the log entry if any.
233245
*/
@@ -268,6 +280,7 @@ public LogEntry build() {
268280
this.labels = ImmutableMap.copyOf(builder.labels);
269281
this.operation = builder.operation;
270282
this.trace = builder.trace;
283+
this.spanId = builder.spanId;
271284
this.sourceLocation = builder.sourceLocation;
272285
this.payload = builder.payload;
273286
}
@@ -363,6 +376,14 @@ public String getTrace() {
363376
}
364377

365378

379+
/**
380+
* Returns the ID of the trace span associated with the log entry, if any.
381+
*/
382+
public String getSpanId() {
383+
return spanId;
384+
}
385+
386+
366387
/**
367388
* Returns the source code location information associated with the log entry, if any.
368389
*/
@@ -386,7 +407,7 @@ public <T extends Payload> T getPayload() {
386407
@Override
387408
public int hashCode() {
388409
return Objects.hash(logName, resource, timestamp, receiveTimestamp, severity, insertId,
389-
httpRequest, labels, operation, trace, sourceLocation, payload);
410+
httpRequest, labels, operation, trace, spanId, sourceLocation, payload);
390411
}
391412

392413
@Override
@@ -408,6 +429,7 @@ public boolean equals(Object obj) {
408429
&& Objects.equals(labels, other.labels)
409430
&& Objects.equals(operation, other.operation)
410431
&& Objects.equals(trace, other.trace)
432+
&& Objects.equals(spanId, other.spanId)
411433
&& Objects.equals(sourceLocation, other.sourceLocation)
412434
&& Objects.equals(payload, other.payload);
413435
}
@@ -425,6 +447,7 @@ public String toString() {
425447
.add("labels", labels)
426448
.add("operation", operation)
427449
.add("trace", trace)
450+
.add("spanId", spanId)
428451
.add("sourceLocation", sourceLocation)
429452
.add("payload", payload)
430453
.toString();
@@ -479,6 +502,9 @@ com.google.logging.v2.LogEntry toPb(String projectId) {
479502
if (trace != null) {
480503
builder.setTrace(trace);
481504
}
505+
if (spanId != null) {
506+
builder.setSpanId(spanId);
507+
}
482508
if (sourceLocation != null) {
483509
builder.setSourceLocation(sourceLocation.toPb());
484510
}
@@ -543,6 +569,9 @@ static LogEntry fromPb(com.google.logging.v2.LogEntry entryPb) {
543569
if (!entryPb.getTrace().equals("")) {
544570
builder.setTrace(entryPb.getTrace());
545571
}
572+
if (!entryPb.getSpanId().equals("")) {
573+
builder.setSpanId(entryPb.getSpanId());
574+
}
546575
if (!entryPb.getSourceLocation().equals(LogEntrySourceLocation.getDefaultInstance())) {
547576
builder.setSourceLocation(SourceLocation.fromPb(entryPb.getSourceLocation()));
548577
}

google-cloud-clients/google-cloud-logging/src/test/java/com/google/cloud/logging/LogEntryTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public class LogEntryTest {
5050
ImmutableMap.of("key1", "value1", "key2", "value2");
5151
private static final Operation OPERATION = Operation.of("id", "producer");
5252
private static final String TRACE = "trace";
53+
private static final String SPAN_ID = "spanId";
5354
private static final SourceLocation SOURCE_LOCATION = new SourceLocation.Builder()
5455
.setFile("file")
5556
.setLine(42L)
@@ -71,6 +72,7 @@ public class LogEntryTest {
7172
.setLabels(LABELS)
7273
.setOperation(OPERATION)
7374
.setTrace(TRACE)
75+
.setSpanId(SPAN_ID)
7476
.setSourceLocation(SOURCE_LOCATION)
7577
.build();
7678
private static final LogEntry JSON_ENTRY = LogEntry.newBuilder(JSON_PAYLOAD)
@@ -84,6 +86,7 @@ public class LogEntryTest {
8486
.setLabels(LABELS)
8587
.setOperation(OPERATION)
8688
.setTrace(TRACE)
89+
.setSpanId(SPAN_ID)
8790
.setSourceLocation(SOURCE_LOCATION)
8891
.build();
8992
private static final LogEntry PROTO_ENTRY = LogEntry.newBuilder(PROTO_PAYLOAD)
@@ -97,6 +100,7 @@ public class LogEntryTest {
97100
.setLabels(LABELS)
98101
.setOperation(OPERATION)
99102
.setTrace(TRACE)
103+
.setSpanId(SPAN_ID)
100104
.setSourceLocation(SOURCE_LOCATION)
101105
.build();
102106

@@ -114,6 +118,7 @@ public void testOf() {
114118
assertNull(logEntry.getHttpRequest());
115119
assertNull(logEntry.getOperation());
116120
assertNull(logEntry.getTrace());
121+
assertNull(logEntry.getSpanId());
117122
assertNull(logEntry.getSourceLocation());
118123
logEntry = LogEntry.of(LOG_NAME, RESOURCE, STRING_PAYLOAD);
119124
assertEquals(STRING_PAYLOAD, logEntry.getPayload());
@@ -128,6 +133,7 @@ public void testOf() {
128133
assertNull(logEntry.getHttpRequest());
129134
assertNull(logEntry.getOperation());
130135
assertNull(logEntry.getTrace());
136+
assertNull(logEntry.getSpanId());
131137
assertNull(logEntry.getSourceLocation());
132138
}
133139

@@ -143,6 +149,7 @@ public void testBuilder() {
143149
assertEquals(LABELS, STRING_ENTRY.getLabels());
144150
assertEquals(OPERATION, STRING_ENTRY.getOperation());
145151
assertEquals(TRACE, STRING_ENTRY.getTrace());
152+
assertEquals(SPAN_ID, STRING_ENTRY.getSpanId());
146153
assertEquals(SOURCE_LOCATION, STRING_ENTRY.getSourceLocation());
147154
assertEquals(STRING_PAYLOAD, STRING_ENTRY.getPayload());
148155
assertEquals(LOG_NAME, JSON_ENTRY.getLogName());
@@ -155,6 +162,7 @@ public void testBuilder() {
155162
assertEquals(LABELS, JSON_ENTRY.getLabels());
156163
assertEquals(OPERATION, JSON_ENTRY.getOperation());
157164
assertEquals(TRACE, JSON_ENTRY.getTrace());
165+
assertEquals(SPAN_ID, JSON_ENTRY.getSpanId());
158166
assertEquals(SOURCE_LOCATION, JSON_ENTRY.getSourceLocation());
159167
assertEquals(JSON_PAYLOAD, JSON_ENTRY.getPayload());
160168
assertEquals(LOG_NAME, PROTO_ENTRY.getLogName());
@@ -167,6 +175,7 @@ public void testBuilder() {
167175
assertEquals(LABELS, PROTO_ENTRY.getLabels());
168176
assertEquals(OPERATION, PROTO_ENTRY.getOperation());
169177
assertEquals(TRACE, PROTO_ENTRY.getTrace());
178+
assertEquals(SPAN_ID, PROTO_ENTRY.getSpanId());
170179
assertEquals(SOURCE_LOCATION, PROTO_ENTRY.getSourceLocation());
171180
assertEquals(PROTO_PAYLOAD, PROTO_ENTRY.getPayload());
172181
LogEntry logEntry = LogEntry.newBuilder(STRING_PAYLOAD)
@@ -182,6 +191,7 @@ public void testBuilder() {
182191
.addLabel("key2", "value2")
183192
.setOperation(OPERATION)
184193
.setTrace(TRACE)
194+
.setSpanId(SPAN_ID)
185195
.setSourceLocation(SOURCE_LOCATION)
186196
.build();
187197
assertEquals(LOG_NAME, logEntry.getLogName());
@@ -194,6 +204,7 @@ public void testBuilder() {
194204
assertEquals(LABELS, logEntry.getLabels());
195205
assertEquals(OPERATION, logEntry.getOperation());
196206
assertEquals(TRACE, logEntry.getTrace());
207+
assertEquals(SPAN_ID, logEntry.getSpanId());
197208
assertEquals(SOURCE_LOCATION, logEntry.getSourceLocation());
198209
assertEquals(StringPayload.of("otherPayload"), logEntry.getPayload());
199210
}
@@ -219,6 +230,7 @@ public void testToBuilder() {
219230
.addLabel("key", "value")
220231
.setOperation(Operation.of("otherId", "otherProducer"))
221232
.setTrace("otherTrace")
233+
.setSpanId("otherSpanId")
222234
.setSourceLocation(new SourceLocation.Builder().setFile("hey.java").build())
223235
.build();
224236
assertEquals("otherLogName", logEntry.getLogName());
@@ -231,6 +243,7 @@ public void testToBuilder() {
231243
assertEquals(ImmutableMap.of("key", "value"), logEntry.getLabels());
232244
assertEquals(Operation.of("otherId", "otherProducer"), logEntry.getOperation());
233245
assertEquals("otherTrace", logEntry.getTrace());
246+
assertEquals("otherSpanId", logEntry.getSpanId());
234247
assertEquals(new SourceLocation.Builder().setFile("hey.java").build(),
235248
logEntry.getSourceLocation());
236249
assertEquals(StringPayload.of("otherPayload"), logEntry.getPayload());
@@ -246,6 +259,7 @@ public void testToBuilder() {
246259
.setLabels(LABELS)
247260
.setOperation(OPERATION)
248261
.setTrace(TRACE)
262+
.setSpanId(SPAN_ID)
249263
.setSourceLocation(SOURCE_LOCATION)
250264
.build();
251265
compareLogEntry(STRING_ENTRY, logEntry);
@@ -274,6 +288,7 @@ private void compareLogEntry(LogEntry expected, LogEntry value) {
274288
assertEquals(expected.getLabels(), value.getLabels());
275289
assertEquals(expected.getOperation(), value.getOperation());
276290
assertEquals(expected.getTrace(), value.getTrace());
291+
assertEquals(expected.getSpanId(), value.getSpanId());
277292
assertEquals(expected.getSourceLocation(), value.getSourceLocation());
278293
assertEquals(expected.getPayload(), value.getPayload());
279294
assertEquals(expected.hashCode(), value.hashCode());

0 commit comments

Comments
 (0)