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

feat: add trace id support to JsonWriter and add default trace id to help identify json writer users. #1302

Merged
merged 3 commits into from
Sep 8, 2021
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 @@ -72,7 +72,8 @@ private JsonStreamWriter(Builder builder)
builder.channelProvider,
builder.credentialsProvider,
builder.endpoint,
builder.flowControlSettings);
builder.flowControlSettings,
builder.traceId);
this.streamWriter = streamWriterBuilder.build();
this.streamName = builder.streamName;
}
Expand Down Expand Up @@ -156,7 +157,8 @@ private void setStreamWriterSettings(
@Nullable TransportChannelProvider channelProvider,
@Nullable CredentialsProvider credentialsProvider,
@Nullable String endpoint,
@Nullable FlowControlSettings flowControlSettings) {
@Nullable FlowControlSettings flowControlSettings,
@Nullable String traceId) {
if (channelProvider != null) {
streamWriterBuilder.setChannelProvider(channelProvider);
}
Expand All @@ -166,6 +168,11 @@ private void setStreamWriterSettings(
if (endpoint != null) {
streamWriterBuilder.setEndpoint(endpoint);
}
if (traceId != null) {
streamWriterBuilder.setTraceId("JsonWriterBeta_" + traceId);
} else {
streamWriterBuilder.setTraceId("JsonWriterBeta:null");
}
if (flowControlSettings != null) {
if (flowControlSettings.getMaxOutstandingRequestBytes() != null) {
streamWriterBuilder.setMaxInflightBytes(
Expand Down Expand Up @@ -246,6 +253,7 @@ public static final class Builder {
private FlowControlSettings flowControlSettings;
private String endpoint;
private boolean createDefaultStream = false;
private String traceId;

private static String streamPatternString =
"(projects/[^/]+/datasets/[^/]+/tables/[^/]+)/streams/[^/]+";
Expand Down Expand Up @@ -336,6 +344,17 @@ public Builder setEndpoint(String endpoint) {
return this;
}

/**
* Setter for a traceId to help identify traffic origin.
*
* @param traceId
* @return Builder
*/
public Builder setTraceId(String traceId) {
this.traceId = Preconditions.checkNotNull(traceId, "TraceId is null.");
return this;
}

/**
* Builds JsonStreamWriter
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,9 @@ public void testSingleAppendSimpleJson() throws Exception {
jsonArr.put(foo);

try (JsonStreamWriter writer =
getTestJsonStreamWriterBuilder(TEST_STREAM, TABLE_SCHEMA).build()) {
getTestJsonStreamWriterBuilder(TEST_STREAM, TABLE_SCHEMA)
.setTraceId("test:empty")
.build()) {

testBigQueryWrite.addResponse(
AppendRowsResponse.newBuilder()
Expand All @@ -280,6 +282,8 @@ public void testSingleAppendSimpleJson() throws Exception {
.getRows()
.getSerializedRows(0),
expectedProto.toByteString());
assertEquals(
testBigQueryWrite.getAppendRequests().get(0).getTraceId(), "JsonWriterBeta_test:empty");
}
}

Expand Down Expand Up @@ -320,6 +324,8 @@ public void testSingleAppendMultipleSimpleJson() throws Exception {
.getProtoRows()
.getRows()
.getSerializedRowsCount());
assertEquals(
testBigQueryWrite.getAppendRequests().get(0).getTraceId(), "JsonWriterBeta:null");
for (int i = 0; i < 4; i++) {
assertEquals(
testBigQueryWrite
Expand Down