Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logging: Allow deferred formatting for trace and spanId #3661

Merged
merged 5 commits into from
Nov 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ public LogEntry apply(com.google.logging.v2.LogEntry pb) {
private final HttpRequest httpRequest;
private final Map<String, String> labels;
private final Operation operation;
private final String trace;
private final String spanId;
private final Object trace;
private final Object spanId;
private final boolean traceSampled;
private final SourceLocation sourceLocation;
private final Payload<?> payload;
Expand All @@ -85,8 +85,8 @@ public static class Builder {
private HttpRequest httpRequest;
private Map<String, String> labels = new HashMap<>();
private Operation operation;
private String trace;
private String spanId;
private Object trace;
private Object spanId;
private boolean traceSampled;
private SourceLocation sourceLocation;
private Payload<?> payload;
Expand Down Expand Up @@ -223,7 +223,6 @@ public Builder setOperation(Operation operation) {
return this;
}


/**
* Sets the resource name of the trace associated with the log entry, if any. If it contains a
* relative resource name, the name is assumed to be relative to `//tracing.googleapis.com`.
Expand All @@ -233,6 +232,14 @@ public Builder setTrace(String trace) {
return this;
}

/**
* Sets the resource name of the trace associated with the log entry, if any. If it contains a
* relative resource name, the name is assumed to be relative to `//tracing.googleapis.com`.
*/
public Builder setTrace(Object trace) {
this.trace = trace;
return this;
}

/**
* Sets the ID of the trace span associated with the log entry, if any.
Expand All @@ -242,6 +249,14 @@ public Builder setSpanId(String spanId) {
return this;
}

/**
* Sets the ID of the trace span associated with the log entry, if any.
*/
public Builder setSpanId(Object spanId) {
chingor13 marked this conversation as resolved.
Show resolved Hide resolved
this.spanId = spanId;
return this;
}


/**
* Sets the sampling decision of the trace span associated with the log entry.
Expand Down Expand Up @@ -385,15 +400,17 @@ public Operation getOperation() {
* relative resource name, the name is assumed to be relative to `//tracing.googleapis.com`.
*/
public String getTrace() {
return trace;
// For backwards compatibility return null when trace not set instead of "null".
return trace == null ? null : String.valueOf(trace);
}


/**
* Returns the ID of the trace span associated with the log entry, if any.
*/
public String getSpanId() {
return spanId;
// For backwards compatibility return null when spanId not set instead of "null".
return spanId == null ? null : String.valueOf(spanId);
}


Expand Down Expand Up @@ -429,7 +446,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, traceSampled, sourceLocation, payload);
httpRequest, labels, operation, getTrace(), getSpanId(), traceSampled, sourceLocation, payload);
}

@Override
Expand All @@ -450,8 +467,8 @@ public boolean equals(Object obj) {
&& Objects.equals(httpRequest, other.httpRequest)
&& Objects.equals(labels, other.labels)
&& Objects.equals(operation, other.operation)
&& Objects.equals(trace, other.trace)
&& Objects.equals(spanId, other.spanId)
&& Objects.equals(getTrace(), other.getTrace())
&& Objects.equals(getSpanId(), other.getSpanId())
&& Objects.equals(traceSampled, other.traceSampled)
&& Objects.equals(sourceLocation, other.sourceLocation)
&& Objects.equals(payload, other.payload);
Expand Down Expand Up @@ -524,10 +541,10 @@ com.google.logging.v2.LogEntry toPb(String projectId) {
builder.setOperation(operation.toPb());
}
if (trace != null) {
builder.setTrace(trace);
builder.setTrace(getTrace());
}
if (spanId != null) {
builder.setSpanId(spanId);
builder.setSpanId(getSpanId());
}
builder.setTraceSampled(traceSampled);
if (sourceLocation != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,19 @@ 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 Object TRACE_FORMATTER = new Object() {
@Override
public String toString() {
return TRACE;
}
};
private static final String SPAN_ID = "spanId";
private static final Object SPAN_ID_FORMATTER = new Object() {
@Override
public String toString() {
return SPAN_ID;
}
};
private static final boolean TRACE_SAMPLED = true;
private static final SourceLocation SOURCE_LOCATION = new SourceLocation.Builder()
.setFile("file")
Expand All @@ -73,8 +85,8 @@ public class LogEntryTest {
.setHttpRequest(HTTP_REQUEST)
.setLabels(LABELS)
.setOperation(OPERATION)
.setTrace(TRACE)
.setSpanId(SPAN_ID)
.setTrace(TRACE_FORMATTER)
.setSpanId(SPAN_ID_FORMATTER)
.setTraceSampled(TRACE_SAMPLED)
.setSourceLocation(SOURCE_LOCATION)
.build();
Expand All @@ -88,8 +100,8 @@ public class LogEntryTest {
.setHttpRequest(HTTP_REQUEST)
.setLabels(LABELS)
.setOperation(OPERATION)
.setTrace(TRACE)
.setSpanId(SPAN_ID)
.setTrace(TRACE_FORMATTER)
.setSpanId(SPAN_ID_FORMATTER)
.setTraceSampled(TRACE_SAMPLED)
.setSourceLocation(SOURCE_LOCATION)
.build();
Expand All @@ -103,8 +115,8 @@ public class LogEntryTest {
.setHttpRequest(HTTP_REQUEST)
.setLabels(LABELS)
.setOperation(OPERATION)
.setTrace(TRACE)
.setSpanId(SPAN_ID)
.setTrace(TRACE_FORMATTER)
.setSpanId(SPAN_ID_FORMATTER)
.setTraceSampled(TRACE_SAMPLED)
.setSourceLocation(SOURCE_LOCATION)
.build();
Expand Down