Skip to content
This repository was archived by the owner on Feb 24, 2026. It is now read-only.

Commit 0e749d9

Browse files
authored
feat: add trace id support to JsonWriter and add default trace id to help identify json writer users. (#1302)
* fix: update code comment to reflect max size change * feat: add setTraceId to JsonWriter to annotate writes from json writer
1 parent b3ef5ee commit 0e749d9

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1beta2/JsonStreamWriter.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ private JsonStreamWriter(Builder builder)
7272
builder.channelProvider,
7373
builder.credentialsProvider,
7474
builder.endpoint,
75-
builder.flowControlSettings);
75+
builder.flowControlSettings,
76+
builder.traceId);
7677
this.streamWriter = streamWriterBuilder.build();
7778
this.streamName = builder.streamName;
7879
}
@@ -156,7 +157,8 @@ private void setStreamWriterSettings(
156157
@Nullable TransportChannelProvider channelProvider,
157158
@Nullable CredentialsProvider credentialsProvider,
158159
@Nullable String endpoint,
159-
@Nullable FlowControlSettings flowControlSettings) {
160+
@Nullable FlowControlSettings flowControlSettings,
161+
@Nullable String traceId) {
160162
if (channelProvider != null) {
161163
streamWriterBuilder.setChannelProvider(channelProvider);
162164
}
@@ -166,6 +168,11 @@ private void setStreamWriterSettings(
166168
if (endpoint != null) {
167169
streamWriterBuilder.setEndpoint(endpoint);
168170
}
171+
if (traceId != null) {
172+
streamWriterBuilder.setTraceId("JsonWriterBeta_" + traceId);
173+
} else {
174+
streamWriterBuilder.setTraceId("JsonWriterBeta:null");
175+
}
169176
if (flowControlSettings != null) {
170177
if (flowControlSettings.getMaxOutstandingRequestBytes() != null) {
171178
streamWriterBuilder.setMaxInflightBytes(
@@ -246,6 +253,7 @@ public static final class Builder {
246253
private FlowControlSettings flowControlSettings;
247254
private String endpoint;
248255
private boolean createDefaultStream = false;
256+
private String traceId;
249257

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

347+
/**
348+
* Setter for a traceId to help identify traffic origin.
349+
*
350+
* @param traceId
351+
* @return Builder
352+
*/
353+
public Builder setTraceId(String traceId) {
354+
this.traceId = Preconditions.checkNotNull(traceId, "TraceId is null.");
355+
return this;
356+
}
357+
339358
/**
340359
* Builds JsonStreamWriter
341360
*

google-cloud-bigquerystorage/src/test/java/com/google/cloud/bigquery/storage/v1beta2/JsonStreamWriterTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,9 @@ public void testSingleAppendSimpleJson() throws Exception {
253253
jsonArr.put(foo);
254254

255255
try (JsonStreamWriter writer =
256-
getTestJsonStreamWriterBuilder(TEST_STREAM, TABLE_SCHEMA).build()) {
256+
getTestJsonStreamWriterBuilder(TEST_STREAM, TABLE_SCHEMA)
257+
.setTraceId("test:empty")
258+
.build()) {
257259

258260
testBigQueryWrite.addResponse(
259261
AppendRowsResponse.newBuilder()
@@ -280,6 +282,8 @@ public void testSingleAppendSimpleJson() throws Exception {
280282
.getRows()
281283
.getSerializedRows(0),
282284
expectedProto.toByteString());
285+
assertEquals(
286+
testBigQueryWrite.getAppendRequests().get(0).getTraceId(), "JsonWriterBeta_test:empty");
283287
}
284288
}
285289

@@ -320,6 +324,8 @@ public void testSingleAppendMultipleSimpleJson() throws Exception {
320324
.getProtoRows()
321325
.getRows()
322326
.getSerializedRowsCount());
327+
assertEquals(
328+
testBigQueryWrite.getAppendRequests().get(0).getTraceId(), "JsonWriterBeta:null");
323329
for (int i = 0; i < 4; i++) {
324330
assertEquals(
325331
testBigQueryWrite

0 commit comments

Comments
 (0)