Skip to content

Commit 6f79c2d

Browse files
authored
feat(bigguery): add url.domain to span tracing (#12208)
This adds the url.domain attribute to http span tracing. This field should matching the proto domain if not overridden by user, otherwise it should match the user set value. [default domain](https://screenshot.googleplex.com/6k7r32jK4fu9x4E.png) [user set example](https://screenshot.googleplex.com/BLgGfV7hLTgaDME.png)
1 parent 92bcdf4 commit 6f79c2d

File tree

4 files changed

+59
-3
lines changed

4 files changed

+59
-3
lines changed

java-bigquery/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/HttpBigQueryRpc.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ public class HttpBigQueryRpc implements BigQueryRpc {
9696
private static final int HTTP_RESUME_INCOMPLETE = 308;
9797
private final BigQueryOptions options;
9898
private final Bigquery bigquery;
99+
private final String urlDomain;
99100

100101
@InternalApi("Visible for testing")
101102
static final Function<DatasetList.Datasets, Dataset> LIST_TO_DATASET =
@@ -117,6 +118,7 @@ public HttpBigQueryRpc(BigQueryOptions options) {
117118
HttpTransport transport = transportOptions.getHttpTransportFactory().create();
118119
HttpRequestInitializer initializer = transportOptions.getHttpRequestInitializer(options);
119120
this.options = options;
121+
this.urlDomain = new GenericUrl(options.getResolvedApiaryHost("bigquery")).getHost();
120122

121123
if (options.isOpenTelemetryTracingEnabled()
122124
&& options.getOpenTelemetryTracer() != null
@@ -2149,7 +2151,8 @@ private Span createRpcTracingSpan(
21492151
builder
21502152
.setAttribute(
21512153
BigQueryTelemetryTracer.GCP_RESOURCE_DESTINATION_ID, gcpResourceDestinationId)
2152-
.setAttribute(BigQueryTelemetryTracer.URL_TEMPLATE, urlTemplate);
2154+
.setAttribute(BigQueryTelemetryTracer.URL_TEMPLATE, urlTemplate)
2155+
.setAttribute(BigQueryTelemetryTracer.URL_DOMAIN, this.urlDomain);
21532156
}
21542157

21552158
if (options != null) {

java-bigquery/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/telemetry/BigQueryTelemetryTracer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ private BigQueryTelemetryTracer() {}
6666
AttributeKey.stringKey("server.address");
6767
public static final AttributeKey<Long> SERVER_PORT = AttributeKey.longKey("server.port");
6868
public static final AttributeKey<String> URL_TEMPLATE = AttributeKey.stringKey("url.template");
69+
public static final AttributeKey<String> URL_DOMAIN = AttributeKey.stringKey("url.domain");
6970

7071
public static void addCommonAttributeToSpan(Span span) {
7172
span.setAttribute(GCP_CLIENT_SERVICE, BQ_GCP_CLIENT_SERVICE)

java-bigquery/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/telemetry/HttpTracingRequestInitializer.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ public class HttpTracingRequestInitializer implements HttpRequestInitializer {
3939
public static final AttributeKey<String> HTTP_REQUEST_METHOD =
4040
AttributeKey.stringKey("http.request.method");
4141
public static final AttributeKey<String> URL_FULL = AttributeKey.stringKey("url.full");
42-
public static final AttributeKey<String> URL_DOMAIN = AttributeKey.stringKey("url.domain");
4342
public static final AttributeKey<Long> HTTP_RESPONSE_STATUS_CODE =
4443
AttributeKey.longKey("http.response.status_code");
4544
public static final AttributeKey<Long> HTTP_REQUEST_RESEND_COUNT =

java-bigquery/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/spi/v2/HttpBigQueryRpcTest.java

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1140,6 +1140,59 @@ public void testGetUriTemplateValueTelemetry() throws Exception {
11401140
"projects/{+projectId}/datasets/{+datasetId}",
11411141
rpcSpan.getAttributes().get(BigQueryTelemetryTracer.URL_TEMPLATE));
11421142
}
1143+
1144+
@Test
1145+
public void testUrlDomain_DefaultValue() throws Exception {
1146+
setMockResponse(
1147+
"{\"kind\":\"bigquery#dataset\",\"id\":\"" + PROJECT_ID + ":" + DATASET_ID + "\"}");
1148+
1149+
rpc.getDatasetSkipExceptionTranslation(PROJECT_ID, DATASET_ID, new HashMap<>());
1150+
1151+
List<io.opentelemetry.sdk.trace.data.SpanData> spans = spanExporter.getFinishedSpanItems();
1152+
io.opentelemetry.sdk.trace.data.SpanData rpcSpan =
1153+
spans.stream()
1154+
.filter(
1155+
span -> span.getName().equals("com.google.cloud.bigquery.BigQueryRpc.getDataset"))
1156+
.findFirst()
1157+
.orElse(null);
1158+
assertNotNull(rpcSpan);
1159+
assertEquals(
1160+
"bigquery.googleapis.com",
1161+
rpcSpan.getAttributes().get(BigQueryTelemetryTracer.URL_DOMAIN));
1162+
}
1163+
1164+
@Test
1165+
public void testUrlDomain_OverriddenValue() throws Exception {
1166+
setMockResponse(
1167+
"{\"kind\":\"bigquery#dataset\",\"id\":\"" + PROJECT_ID + ":" + DATASET_ID + "\"}");
1168+
1169+
BigQueryOptions customOptions =
1170+
BigQueryOptions.newBuilder()
1171+
.setProjectId(PROJECT_ID)
1172+
.setCredentials(NoCredentials.getInstance())
1173+
.setEnableOpenTelemetryTracing(true)
1174+
.setOpenTelemetryTracer(tracer)
1175+
.setTransportOptions(
1176+
BigQueryOptions.getDefaultHttpTransportOptions().toBuilder()
1177+
.setHttpTransportFactory(() -> mockTransport)
1178+
.build())
1179+
.setHost("https://custom.googleapis.com")
1180+
.build();
1181+
HttpBigQueryRpc customRpc = new HttpBigQueryRpc(customOptions);
1182+
1183+
customRpc.getDatasetSkipExceptionTranslation(PROJECT_ID, DATASET_ID, new HashMap<>());
1184+
1185+
List<io.opentelemetry.sdk.trace.data.SpanData> spans = spanExporter.getFinishedSpanItems();
1186+
io.opentelemetry.sdk.trace.data.SpanData rpcSpan =
1187+
spans.stream()
1188+
.filter(
1189+
span -> span.getName().equals("com.google.cloud.bigquery.BigQueryRpc.getDataset"))
1190+
.findFirst()
1191+
.orElse(null);
1192+
assertNotNull(rpcSpan);
1193+
assertEquals(
1194+
"custom.googleapis.com", rpcSpan.getAttributes().get(BigQueryTelemetryTracer.URL_DOMAIN));
1195+
}
11431196
}
11441197

11451198
@Nested
@@ -1190,8 +1243,8 @@ public void testHttpTracingDisabledDoesNotAddAdditionalAttributes() throws Excep
11901243
assertNull(
11911244
rpcSpan.getAttributes().get(AttributeKey.stringKey("url.template")),
11921245
"url.template attribute should not be set");
1193-
11941246
assertNull(rpcSpan.getAttributes().get(BigQueryTelemetryTracer.GCP_RESOURCE_DESTINATION_ID));
1247+
assertNull(rpcSpan.getAttributes().get(BigQueryTelemetryTracer.URL_DOMAIN));
11951248
}
11961249

11971250
@Test

0 commit comments

Comments
 (0)