2323import com .google .common .base .MoreObjects ;
2424import com .google .common .collect .ImmutableMap ;
2525import com .google .logging .v2 .LogEntryOperation ;
26+ import com .google .logging .v2 .LogEntrySourceLocation ;
2627import com .google .logging .v2 .LogName ;
2728import com .google .protobuf .Timestamp ;
2829
@@ -57,11 +58,14 @@ public LogEntry apply(com.google.logging.v2.LogEntry pb) {
5758 private final String logName ;
5859 private final MonitoredResource resource ;
5960 private final Long timestamp ;
61+ private final Long receiveTimestamp ;
6062 private final Severity severity ;
6163 private final String insertId ;
6264 private final HttpRequest httpRequest ;
6365 private final Map <String , String > labels ;
6466 private final Operation operation ;
67+ private final String trace ;
68+ private final SourceLocation sourceLocation ;
6569 private final Payload <?> payload ;
6670
6771 /**
@@ -72,11 +76,14 @@ public static class Builder {
7276 private String logName ;
7377 private MonitoredResource resource ;
7478 private Long timestamp ;
79+ private Long receiveTimestamp ;
7580 private Severity severity = Severity .DEFAULT ;
7681 private String insertId ;
7782 private HttpRequest httpRequest ;
7883 private Map <String , String > labels = new HashMap <>();
7984 private Operation operation ;
85+ private String trace ;
86+ private SourceLocation sourceLocation ;
8087 private Payload <?> payload ;
8188
8289 Builder (Payload <?> payload ) {
@@ -87,11 +94,14 @@ public static class Builder {
8794 this .logName = entry .logName ;
8895 this .resource = entry .resource ;
8996 this .timestamp = entry .timestamp ;
97+ this .receiveTimestamp = entry .receiveTimestamp ;
9098 this .severity = entry .severity ;
9199 this .insertId = entry .insertId ;
92100 this .httpRequest = entry .httpRequest ;
93101 this .labels = new HashMap <>(entry .labels );
94102 this .operation = entry .operation ;
103+ this .trace = entry .trace ;
104+ this .sourceLocation = entry .sourceLocation ;
95105 this .payload = entry .payload ;
96106 }
97107
@@ -130,6 +140,15 @@ public Builder setTimestamp(long timestamp) {
130140 }
131141
132142
143+ /**
144+ * Sets the time the log entry was received by Stackdriver Logging.
145+ */
146+ public Builder setReceiveTimestamp (long receiveTimestamp ) {
147+ this .receiveTimestamp = receiveTimestamp ;
148+ return this ;
149+ }
150+
151+
133152 /**
134153 * Sets the severity of the log entry. If not set, {@link Severity#DEFAULT} is used.
135154 */
@@ -168,6 +187,7 @@ public Builder setLabels(Map<String, String> labels) {
168187 return this ;
169188 }
170189
190+
171191 /**
172192 * Adds a label to the log entry's labels. Labels are user-defined (key, value) data that
173193 * provides additional information about the log entry.
@@ -177,6 +197,7 @@ public Builder addLabel(String key, String value) {
177197 return this ;
178198 }
179199
200+
180201 /**
181202 * Clears all the labels of the log entry. Labels are user-defined (key, value) data that
182203 * provides additional information about the log entry.
@@ -196,6 +217,25 @@ public Builder setOperation(Operation operation) {
196217 }
197218
198219
220+ /**
221+ * Sets the resource name of the trace associated with the log entry, if any. If it contains a
222+ * relative resource name, the name is assumed to be relative to `//tracing.googleapis.com`.
223+ */
224+ public Builder setTrace (String trace ) {
225+ this .trace = trace ;
226+ return this ;
227+ }
228+
229+
230+ /**
231+ * Sets the source code location information associated with the log entry if any.
232+ */
233+ public Builder setSourceLocation (SourceLocation sourceLocation ) {
234+ this .sourceLocation = sourceLocation ;
235+ return this ;
236+ }
237+
238+
199239 /**
200240 * Sets the payload for this log entry. The log entry payload can be provided as an UTF-8 string
201241 * (see {@link Payload.StringPayload}), a JSON object (see {@link Payload.JsonPayload}, or
@@ -220,11 +260,14 @@ public LogEntry build() {
220260 this .logName = builder .logName ;
221261 this .resource = builder .resource ;
222262 this .timestamp = builder .timestamp ;
263+ this .receiveTimestamp = builder .receiveTimestamp ;
223264 this .severity = builder .severity ;
224265 this .insertId = builder .insertId ;
225266 this .httpRequest = builder .httpRequest ;
226267 this .labels = ImmutableMap .copyOf (builder .labels );
227268 this .operation = builder .operation ;
269+ this .trace = builder .trace ;
270+ this .sourceLocation = builder .sourceLocation ;
228271 this .payload = builder .payload ;
229272 }
230273
@@ -260,6 +303,14 @@ public Long getTimestamp() {
260303 }
261304
262305
306+ /**
307+ * Returns the time the log entry was received by Stackdriver Logging.
308+ */
309+ public Long getReceiveTimestamp () {
310+ return receiveTimestamp ;
311+ }
312+
313+
263314 /**
264315 * Returns the severity of the log entry. If not set, {@link Severity#DEFAULT} is used.
265316 */
@@ -302,6 +353,23 @@ public Operation getOperation() {
302353 }
303354
304355
356+ /**
357+ * Returns the resource name of the trace associated with the log entry, if any. If it contains a
358+ * relative resource name, the name is assumed to be relative to `//tracing.googleapis.com`.
359+ */
360+ public String getTrace () {
361+ return trace ;
362+ }
363+
364+
365+ /**
366+ * Returns the source code location information associated with the log entry, if any.
367+ */
368+ public SourceLocation getSourceLocation () {
369+ return sourceLocation ;
370+ }
371+
372+
305373 /**
306374 * Returns the payload for this log entry. The log entry payload can be an UTF-8 string (see
307375 * {@link Payload.StringPayload}), a JSON object (see {@link Payload.JsonPayload}, or a protobuf
@@ -316,8 +384,8 @@ public <T extends Payload> T getPayload() {
316384
317385 @ Override
318386 public int hashCode () {
319- return Objects .hash (logName , resource , timestamp , severity , insertId , httpRequest , labels ,
320- operation , payload );
387+ return Objects .hash (logName , resource , timestamp , receiveTimestamp , severity , insertId ,
388+ httpRequest , labels , operation , trace , sourceLocation , payload );
321389 }
322390
323391 @ Override
@@ -332,11 +400,14 @@ public boolean equals(Object obj) {
332400 return Objects .equals (logName , other .logName )
333401 && Objects .equals (resource , other .resource )
334402 && Objects .equals (timestamp , other .timestamp )
403+ && Objects .equals (receiveTimestamp , other .receiveTimestamp )
335404 && Objects .equals (severity , other .severity )
336405 && Objects .equals (insertId , other .insertId )
337406 && Objects .equals (httpRequest , other .httpRequest )
338407 && Objects .equals (labels , other .labels )
339408 && Objects .equals (operation , other .operation )
409+ && Objects .equals (trace , other .trace )
410+ && Objects .equals (sourceLocation , other .sourceLocation )
340411 && Objects .equals (payload , other .payload );
341412 }
342413
@@ -346,11 +417,14 @@ public String toString() {
346417 .add ("logName" , logName )
347418 .add ("resource" , resource )
348419 .add ("timestamp" , timestamp )
420+ .add ("receiveTimestamp" , receiveTimestamp )
349421 .add ("severity" , severity )
350422 .add ("insertId" , insertId )
351423 .add ("httpRequest" , httpRequest )
352424 .add ("labels" , labels )
353425 .add ("operation" , operation )
426+ .add ("trace" , trace )
427+ .add ("sourceLocation" , sourceLocation )
354428 .add ("payload" , payload )
355429 .toString ();
356430 }
@@ -362,6 +436,18 @@ public Builder toBuilder() {
362436 return new Builder (this );
363437 }
364438
439+ private static Timestamp timestampFromMillis (Long millis ) {
440+ Timestamp .Builder tsBuilder = Timestamp .newBuilder ();
441+ tsBuilder .setSeconds (millis / MILLIS_PER_SECOND );
442+ tsBuilder .setNanos ((int ) (millis % MILLIS_PER_SECOND * NANOS_PER_MILLISECOND ));
443+ return tsBuilder .build ();
444+ }
445+
446+ private static Long millisFromTimestamp (Timestamp timestamp ) {
447+ return timestamp .getSeconds () * MILLIS_PER_SECOND
448+ + timestamp .getNanos () / NANOS_PER_MILLISECOND ;
449+ }
450+
365451 com .google .logging .v2 .LogEntry toPb (String projectId ) {
366452 com .google .logging .v2 .LogEntry .Builder builder = payload .toPb ();
367453 builder .putAllLabels (labels );
@@ -372,10 +458,10 @@ com.google.logging.v2.LogEntry toPb(String projectId) {
372458 builder .setResource (resource .toPb ());
373459 }
374460 if (timestamp != null ) {
375- Timestamp . Builder tsBuilder = Timestamp . newBuilder ( );
376- tsBuilder . setSeconds ( timestamp / MILLIS_PER_SECOND );
377- tsBuilder . setNanos (( int ) ( timestamp % MILLIS_PER_SECOND * NANOS_PER_MILLISECOND ));
378- builder .setTimestamp ( tsBuilder . build ( ));
461+ builder . setTimestamp ( timestampFromMillis ( timestamp ) );
462+ }
463+ if ( receiveTimestamp != null ) {
464+ builder .setReceiveTimestamp ( timestampFromMillis ( receiveTimestamp ));
379465 }
380466 if (severity != null ) {
381467 builder .setSeverity (severity .toPb ());
@@ -389,6 +475,12 @@ com.google.logging.v2.LogEntry toPb(String projectId) {
389475 if (operation != null ) {
390476 builder .setOperation (operation .toPb ());
391477 }
478+ if (trace != null ) {
479+ builder .setTrace (trace );
480+ }
481+ if (sourceLocation != null ) {
482+ builder .setSourceLocation (sourceLocation .toPb ());
483+ }
392484 return builder .build ();
393485 }
394486
@@ -426,12 +518,17 @@ static LogEntry fromPb(com.google.logging.v2.LogEntry entryPb) {
426518 builder .setResource (MonitoredResource .fromPb (entryPb .getResource ()));
427519 }
428520 if (entryPb .hasTimestamp ()) {
429- Timestamp ts = entryPb .getTimestamp ();
430- Long millis = ts .getSeconds () * MILLIS_PER_SECOND + ts .getNanos () / NANOS_PER_MILLISECOND ;
521+ Long millis = millisFromTimestamp (entryPb .getTimestamp ());
431522 if (millis != 0 ) {
432523 builder .setTimestamp (millis );
433524 }
434525 }
526+ if (entryPb .hasReceiveTimestamp ()) {
527+ Long millis = millisFromTimestamp (entryPb .getReceiveTimestamp ());
528+ if (millis != 0 ) {
529+ builder .setReceiveTimestamp (millis );
530+ }
531+ }
435532 if (!entryPb .getInsertId ().equals ("" )) {
436533 builder .setInsertId (entryPb .getInsertId ());
437534 }
@@ -442,6 +539,12 @@ static LogEntry fromPb(com.google.logging.v2.LogEntry entryPb) {
442539 if (!entryPb .getOperation ().equals (LogEntryOperation .getDefaultInstance ())) {
443540 builder .setOperation (Operation .fromPb (entryPb .getOperation ()));
444541 }
542+ if (!entryPb .getTrace ().equals ("" )) {
543+ builder .setTrace (entryPb .getTrace ());
544+ }
545+ if (!entryPb .getSourceLocation ().equals (LogEntrySourceLocation .getDefaultInstance ())) {
546+ builder .setSourceLocation (SourceLocation .fromPb (entryPb .getSourceLocation ()));
547+ }
445548 return builder .build ();
446549 }
447550
0 commit comments