From 4f0e958fb869fbe79d9c69ea9f9f3d4b500eefc2 Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Fri, 15 Nov 2024 11:11:20 +0100 Subject: [PATCH 1/2] Set `db.query.text` OpenTelemetry attribute --- google/cloud/bigquery/opentelemetry_tracing.py | 8 +++++--- tests/unit/test_opentelemetry_tracing.py | 3 ++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/google/cloud/bigquery/opentelemetry_tracing.py b/google/cloud/bigquery/opentelemetry_tracing.py index b5f6bf991..cd469e300 100644 --- a/google/cloud/bigquery/opentelemetry_tracing.py +++ b/google/cloud/bigquery/opentelemetry_tracing.py @@ -29,9 +29,7 @@ HAS_OPENTELEMETRY = False _warned_telemetry = False -_default_attributes = { - "db.system": "BigQuery" -} # static, default values assigned to all spans +_default_attributes = {"db.system": "BigQuery"} # static, default values assigned to all spans @contextmanager @@ -161,4 +159,8 @@ def _set_job_attributes(job_ref): if total_bytes_processed is not None: job_attributes["total_bytes_processed"] = total_bytes_processed + query = getattr(job_ref, "query", None) + if query is not None: + job_attributes["db.query.text"] = query + return job_attributes diff --git a/tests/unit/test_opentelemetry_tracing.py b/tests/unit/test_opentelemetry_tracing.py index 546cc02bd..c6117d240 100644 --- a/tests/unit/test_opentelemetry_tracing.py +++ b/tests/unit/test_opentelemetry_tracing.py @@ -42,7 +42,6 @@ TEST_SPAN_ATTRIBUTES = {"foo": "baz"} -@pytest.mark.skipif(opentelemetry is None, reason="Require `opentelemetry`") @pytest.fixture def setup(): importlib.reload(opentelemetry_tracing) @@ -132,6 +131,7 @@ def test_default_job_attributes(setup): expected_attributes = { "db.system": "BigQuery", "db.name": "test_project_id", + "db.query.text": "SELECT 1", "location": "test_location", "num_child_jobs": "0", "job_id": "test_job_id", @@ -149,6 +149,7 @@ def test_default_job_attributes(setup): test_job_ref.job_id = "test_job_id" test_job_ref.location = "test_location" test_job_ref.project = "test_project_id" + test_job_ref.query = "SELECT 1" test_job_ref.num_child_jobs = "0" test_job_ref.parent_job_id = "parent_job_id" test_job_ref.created = time_created From 4d16e6a212bc278916971da8add33ba4a0630bbd Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Thu, 21 Nov 2024 09:17:45 +0100 Subject: [PATCH 2/2] Fix black --- google/cloud/bigquery/opentelemetry_tracing.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/google/cloud/bigquery/opentelemetry_tracing.py b/google/cloud/bigquery/opentelemetry_tracing.py index cd469e300..5192fb807 100644 --- a/google/cloud/bigquery/opentelemetry_tracing.py +++ b/google/cloud/bigquery/opentelemetry_tracing.py @@ -29,7 +29,9 @@ HAS_OPENTELEMETRY = False _warned_telemetry = False -_default_attributes = {"db.system": "BigQuery"} # static, default values assigned to all spans +_default_attributes = { + "db.system": "BigQuery" +} # static, default values assigned to all spans @contextmanager