Skip to content
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 @@ -32,7 +32,7 @@ public final class BigQueryTelemetryTracer {
private BigQueryTelemetryTracer() {}

public static final String BQ_GCP_CLIENT_SERVICE = "bigquery";
public static final String BQ_GCP_CLIENT_REPO = "googleapis/java-bigquery";
public static final String BQ_GCP_CLIENT_REPO = "googleapis/google-cloud-java";
public static final String BQ_GCP_CLIENT_ARTIFACT = "google-cloud-bigquery";
public static final String BQ_GCP_CLIENT_LANGUAGE = "java";

Expand Down Expand Up @@ -61,10 +61,6 @@ private BigQueryTelemetryTracer() {}
public static final AttributeKey<String> STATUS_MESSAGE =
AttributeKey.stringKey("status.message");

// Common Server Attributes
public static final AttributeKey<String> SERVER_ADDRESS =
AttributeKey.stringKey("server.address");
public static final AttributeKey<Long> SERVER_PORT = AttributeKey.longKey("server.port");
public static final AttributeKey<String> URL_TEMPLATE = AttributeKey.stringKey("url.template");
public static final AttributeKey<String> URL_DOMAIN = AttributeKey.stringKey("url.domain");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public class HttpTracingRequestInitializer implements HttpRequestInitializer {
AttributeKey.longKey("http.request.body.size");
public static final AttributeKey<Long> HTTP_RESPONSE_BODY_SIZE =
AttributeKey.longKey("http.response.body.size");
public static final AttributeKey<String> SERVER_ADDRESS =
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Appropriate file

Why do we need these two attributes to be moved here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is where I'm setting them. I have attributes broken up by HTTP interceptors attributes set in this class and client side attributes set in HttpBigQueryRpc class. I think it makes it easier to distinguish between the 2.

AttributeKey.stringKey("server.address");
public static final AttributeKey<Long> SERVER_PORT = AttributeKey.longKey("server.port");

@VisibleForTesting public static final String HTTP_RPC_SYSTEM_NAME = "http";

Expand Down Expand Up @@ -119,10 +122,10 @@ private void addInitialHttpAttributesToSpan(Span span, HttpRequest request) {
BigQueryTelemetryTracer.addCommonAttributeToSpan(span);
span.setAttribute(BigQueryTelemetryTracer.RPC_SYSTEM_NAME, HTTP_RPC_SYSTEM_NAME);
String host = request.getUrl().getHost();
span.setAttribute(BigQueryTelemetryTracer.SERVER_ADDRESS, host);
span.setAttribute(SERVER_ADDRESS, host);
int port = request.getUrl().getPort();
if (port > 0) {
span.setAttribute(BigQueryTelemetryTracer.SERVER_PORT, (long) port);
span.setAttribute(SERVER_PORT, (long) port);
}
span.setAttribute(URL_FULL, getSanitizedUrl(request));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

Expand All @@ -30,11 +31,13 @@
import com.google.cloud.bigquery.testing.RemoteBigQueryHelper;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter;
import io.opentelemetry.sdk.trace.SdkTracerProvider;
import io.opentelemetry.sdk.trace.data.SpanData;
import io.opentelemetry.sdk.trace.data.StatusData;
import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor;
import java.io.IOException;
import java.util.List;
Expand Down Expand Up @@ -86,12 +89,15 @@ public void testListDatasetsTraced() {
for (SpanData span : spans) {
if (span.getName().equals("com.google.cloud.bigquery.BigQueryRpc.listDatasets")) {
foundRpcSpan = true;
assertEquals(SpanKind.CLIENT, span.getKind());
assertEquals(StatusData.unset(), span.getStatus());
Map<AttributeKey<?>, Object> attrs = span.getAttributes().asMap();
checkGeneralAttributes(attrs);
assertEquals("GET", attrs.get(HttpTracingRequestInitializer.HTTP_REQUEST_METHOD));
assertEquals("DatasetService", attrs.get(AttributeKey.stringKey("bq.rpc.service")));
assertEquals("ListDatasets", attrs.get(AttributeKey.stringKey("bq.rpc.method")));
assertEquals("bigquery.googleapis.com", attrs.get(BigQueryTelemetryTracer.SERVER_ADDRESS));
assertEquals(
"bigquery.googleapis.com", attrs.get(HttpTracingRequestInitializer.SERVER_ADDRESS));
assertEquals(200L, attrs.get(HttpTracingRequestInitializer.HTTP_RESPONSE_STATUS_CODE));
assertEquals("bigquery.googleapis.com", attrs.get(BigQueryTelemetryTracer.URL_DOMAIN));
assertEquals(
Expand All @@ -102,6 +108,10 @@ public void testListDatasetsTraced() {
attrs.get(BigQueryTelemetryTracer.GCP_RESOURCE_DESTINATION_ID));
assertEquals(
"projects/{+projectId}/datasets", attrs.get(BigQueryTelemetryTracer.URL_TEMPLATE));
assertNull(attrs.get(BigQueryTelemetryTracer.STATUS_MESSAGE));
assertNull(attrs.get(BigQueryTelemetryTracer.ERROR_TYPE));
assertNull(attrs.get(BigQueryTelemetryTracer.EXCEPTION_TYPE));
assertNull(attrs.get(HttpTracingRequestInitializer.HTTP_REQUEST_RESEND_COUNT));
}
}
assertTrue(foundRpcSpan, "Expected to find BigQueryRpc.listDatasets span");
Expand All @@ -126,6 +136,8 @@ public void testGetDatasetNotFoundTraced() {
for (SpanData span : spans) {
if (span.getName().equals("com.google.cloud.bigquery.BigQueryRpc.getDataset")) {
foundRpcSpan = true;
assertEquals(SpanKind.CLIENT, span.getKind());
assertEquals(StatusData.error(), span.getStatus());
Map<AttributeKey<?>, Object> attrs = span.getAttributes().asMap();
checkGeneralAttributes(attrs);
assertEquals("GET", attrs.get(HttpTracingRequestInitializer.HTTP_REQUEST_METHOD));
Expand All @@ -138,7 +150,8 @@ public void testGetDatasetNotFoundTraced() {
assertEquals(
"https://bigquery.googleapis.com/bigquery/v2/projects/gcloud-devel/datasets/non_existent_dataset?prettyPrint=false",
attrs.get(HttpTracingRequestInitializer.URL_FULL));
assertEquals("bigquery.googleapis.com", attrs.get(BigQueryTelemetryTracer.SERVER_ADDRESS));
assertEquals(
"bigquery.googleapis.com", attrs.get(HttpTracingRequestInitializer.SERVER_ADDRESS));
assertEquals("bigquery.googleapis.com", attrs.get(BigQueryTelemetryTracer.URL_DOMAIN));
assertEquals(
"//bigquery.googleapis.com/projects/gcloud-devel/datasets/non_existent_dataset",
Expand All @@ -155,7 +168,7 @@ public void testGetDatasetNotFoundTraced() {
}

@Test
public void testConnectionErrorRetriesTraced() {
public void testClientErrorAndRetriesTraced() {
// Pass invalid host to force connection error and retries
BigQuery bq =
bigqueryHelper.getOptions().toBuilder()
Expand All @@ -181,14 +194,16 @@ public void testConnectionErrorRetriesTraced() {
for (SpanData span : spans) {
if (span.getName().equals("com.google.cloud.bigquery.BigQueryRpc.listDatasets")) {
rpcSpanCount++;
assertEquals(SpanKind.CLIENT, span.getKind());
assertEquals(StatusData.error().getStatusCode(), span.getStatus().getStatusCode());
Map<AttributeKey<?>, Object> attrs = span.getAttributes().asMap();
checkGeneralAttributes(attrs);
assertEquals(
"https://invalid-host-name-12345.com:8080/bigquery/v2/projects/gcloud-devel/datasets?prettyPrint=false",
(String) attrs.get(HttpTracingRequestInitializer.URL_FULL));
assertEquals(
"invalid-host-name-12345.com", attrs.get(BigQueryTelemetryTracer.SERVER_ADDRESS));
assertEquals(8080L, attrs.get(BigQueryTelemetryTracer.SERVER_PORT));
"invalid-host-name-12345.com", attrs.get(HttpTracingRequestInitializer.SERVER_ADDRESS));
assertEquals(8080L, attrs.get(HttpTracingRequestInitializer.SERVER_PORT));
assertEquals("invalid-host-name-12345.com", attrs.get(BigQueryTelemetryTracer.URL_DOMAIN));
assertEquals(
"projects/{+projectId}/datasets", attrs.get(BigQueryTelemetryTracer.URL_TEMPLATE));
Expand Down Expand Up @@ -254,11 +269,26 @@ public void testSimultaneousCallsDoNotAffectResendCountForEachother() {
assertEquals(5, cancelJobSpanCount, "Expected 5 attempts total for cancelJob call");
}

@Test
public void testTracingDisabledNoSpansCollected() {
BigQuery bq =
bigqueryHelper.getOptions().toBuilder()
.setEnableOpenTelemetryTracing(false)
.setOpenTelemetryTracer(tracer)
.build()
.getService();

bq.listDatasets();

List<SpanData> spans = memoryExporter.getFinishedSpanItems();
assertTrue(spans.isEmpty(), "Expected no spans to be collected when tracing is disabled");
}

private static void checkRetryAttribute(SpanData span, int listDataSpanCount) {
Map<AttributeKey<?>, Object> attrs = span.getAttributes().asMap();
Long resendCount = (Long) attrs.get(HttpTracingRequestInitializer.HTTP_REQUEST_RESEND_COUNT);
if (listDataSpanCount == 1) {
assertTrue(resendCount == null || resendCount == 0);
assertNull(resendCount, "Expected no resend count for first attempt");
} else {
assertNotNull(resendCount, "Expected resend count for retry attempt " + listDataSpanCount);
assertEquals((long) (listDataSpanCount - 1), resendCount.longValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,8 +435,9 @@ private void closeAndVerifySpanData(
assertEquals(1, spans.size());
SpanData span = spans.get(0);
assertEquals(SPAN_NAME, span.getName());
assertEquals(BIGQUERY_DOMAIN, span.getAttributes().get(BigQueryTelemetryTracer.SERVER_ADDRESS));
assertEquals(443, span.getAttributes().get(BigQueryTelemetryTracer.SERVER_PORT));
assertEquals(
BIGQUERY_DOMAIN, span.getAttributes().get(HttpTracingRequestInitializer.SERVER_ADDRESS));
assertEquals(443, span.getAttributes().get(HttpTracingRequestInitializer.SERVER_PORT));
assertEquals(
BigQueryTelemetryTracer.BQ_GCP_CLIENT_SERVICE,
span.getAttributes().get(BigQueryTelemetryTracer.GCP_CLIENT_SERVICE));
Expand Down
Loading