Skip to content

Commit b5ca757

Browse files
committed
feat(opentelemetry): add request_tag to span attributes
1 parent e49afa6 commit b5ca757

File tree

4 files changed

+17
-6
lines changed

4 files changed

+17
-6
lines changed

google/cloud/spanner_v1/_opentelemetry_tracing.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,11 @@ def trace_call(
122122
if extra_attributes:
123123
attributes.update(extra_attributes)
124124

125+
if "request_options" in attributes:
126+
request_options = attributes.pop("request_options")
127+
if request_options and request_options.request_tag:
128+
attributes["spanner.request_tag"] = request_options.request_tag
129+
125130
if extended_tracing_globally_disabled:
126131
enable_extended_tracing = False
127132

google/cloud/spanner_v1/snapshot.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,11 @@ def read(
409409
method=streaming_read_method,
410410
request=read_request,
411411
metadata=metadata,
412-
trace_attributes={"table_id": table, "columns": columns},
412+
trace_attributes={
413+
"table_id": table,
414+
"columns": columns,
415+
"request_options": request_options,
416+
},
413417
column_info=column_info,
414418
lazy_decode=lazy_decode,
415419
)
@@ -601,7 +605,7 @@ def execute_sql(
601605
method=execute_streaming_sql_method,
602606
request=execute_sql_request,
603607
metadata=metadata,
604-
trace_attributes={"db.statement": sql},
608+
trace_attributes={"db.statement": sql, "request_options": request_options},
605609
column_info=column_info,
606610
lazy_decode=lazy_decode,
607611
)

tests/unit/test_snapshot.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@
116116
class _Derived(_SnapshotBase):
117117
"""A minimally-implemented _SnapshotBase-derived class for testing"""
118118

119+
transaction_tag = None
120+
119121
# Use a simplified implementation of _build_transaction_options_pb
120122
# that always returns the same transaction options.
121123
TRANSACTION_OPTIONS = TransactionOptions()
@@ -1282,7 +1284,10 @@ def _execute_sql_helper(
12821284

12831285
expected_attributes = dict(
12841286
BASE_ATTRIBUTES,
1285-
**{"db.statement": SQL_QUERY_WITH_PARAM, "x_goog_spanner_request_id": req_id},
1287+
**{
1288+
"db.statement": SQL_QUERY_WITH_PARAM,
1289+
"x_goog_spanner_request_id": req_id,
1290+
},
12861291
)
12871292
if request_options and request_options.request_tag:
12881293
expected_attributes["spanner.request_tag"] = request_options.request_tag

tests/unit/test_spanner_metrics_tracer_factory.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,9 @@
1414
# limitations under the License.
1515

1616
import pytest
17-
import unittest
18-
from unittest import mock
1917
from google.cloud.spanner_v1.metrics.spanner_metrics_tracer_factory import (
2018
SpannerMetricsTracerFactory,
2119
)
22-
from opentelemetry.sdk.resources import Resource
2320

2421
pytest.importorskip("opentelemetry")
2522

0 commit comments

Comments
 (0)