Skip to content

Commit af65849

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

File tree

5 files changed

+43
-44
lines changed

5 files changed

+43
-44
lines changed

examples/trace.py

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from opentelemetry.trace.propagation.tracecontext import TraceContextTextMapPropagator
2828

2929
# Setup common variables that'll be used between Spanner and traces.
30-
project_id = os.environ.get('SPANNER_PROJECT_ID', 'span-cloud-testing')
30+
project_id = os.environ.get('SPANNER_PROJECT_ID', 'test-project')
3131

3232
def spanner_with_cloud_trace():
3333
# [START spanner_opentelemetry_traces_cloudtrace_usage]
@@ -62,13 +62,16 @@ def spanner_with_otlp():
6262

6363

6464
def main():
65+
# Setup OpenTelemetry, trace and Cloud Trace exporter.
66+
tracer_provider = TracerProvider(sampler=ALWAYS_ON)
67+
trace_exporter = CloudTraceSpanExporter(project_id=project_id)
68+
tracer_provider.add_span_processor(BatchSpanProcessor(trace_exporter))
69+
6570
# Setup the Cloud Spanner Client.
6671
# Change to "spanner_client = spanner_with_otlp" to use OTLP exporter
6772
spanner_client = spanner_with_cloud_trace()
68-
instance = spanner_client.instance('suvham-testing')
69-
instance.reload()
70-
database = instance.database('gildb')
71-
tracer_provider = spanner_client.observability_options["tracer_provider"]
73+
instance = spanner_client.instance('test-instance')
74+
database = instance.database('test-db')
7275

7376
# Set W3C Trace Context as the global propagator for end to end tracing.
7477
set_global_textmap(TraceContextTextMapPropagator())
@@ -90,27 +93,12 @@ def main():
9093
# Purposefully issue a bad SQL statement to examine exceptions
9194
# that get recorded and a ERROR span status.
9295
try:
93-
data = snapshot.execute_sql('SELECT CURRENT_TIMESTAMP()')
96+
data = snapshot.execute_sql('SELECT CURRENT_TIMESTAMPx()')
9497
for row in data:
9598
print(row)
9699
except Exception as e:
97100
print(e)
98101

99-
# Example of a read-write transaction with a transaction tag
100-
with tracer.start_as_current_span('TaggedTransaction'):
101-
def update_singer_name(transaction):
102-
transaction.execute_update(
103-
"UPDATE Singers SET FirstName = 'Timothy' WHERE SingerId = 1",
104-
request_options={
105-
"request_tag": "app=concert,env=dev,action=update"
106-
},
107-
)
108-
print("Updated singer's name.")
109-
110-
database.run_in_transaction(
111-
update_singer_name, transaction_tag="app=concert,env=dev"
112-
)
113-
114102

115103
if __name__ == '__main__':
116104
main()

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: 23 additions & 18 deletions
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()
@@ -619,23 +621,23 @@ def test_iteration_w_multiple_span_creation(self):
619621

620622

621623
class Test_SnapshotBase(OpenTelemetryBase):
622-
# def test_ctor(self):
623-
# session = build_session()
624-
# derived = _build_snapshot_derived(session=session)
625-
#
626-
# # Attributes from _SessionWrapper.
627-
# self.assertIs(derived._session, session)
628-
#
629-
# # Attributes from _SnapshotBase.
630-
# self.assertTrue(derived._read_only)
631-
# self.assertFalse(derived._multi_use)
632-
# self.assertEqual(derived._execute_sql_request_count, 0)
633-
# self.assertEqual(derived._read_request_count, 0)
634-
# self.assertIsNone(derived._transaction_id)
635-
# self.assertIsNone(derived._precommit_token)
636-
# self.assertIsInstance(derived._lock, type(Lock()))
637-
#
638-
# self.assertNoSpans()
624+
def test_ctor(self):
625+
session = build_session()
626+
derived = _build_snapshot_derived(session=session)
627+
628+
# Attributes from _SessionWrapper.
629+
self.assertIs(derived._session, session)
630+
631+
# Attributes from _SnapshotBase.
632+
self.assertTrue(derived._read_only)
633+
self.assertFalse(derived._multi_use)
634+
self.assertEqual(derived._execute_sql_request_count, 0)
635+
self.assertEqual(derived._read_request_count, 0)
636+
self.assertIsNone(derived._transaction_id)
637+
self.assertIsNone(derived._precommit_token)
638+
self.assertIsInstance(derived._lock, type(Lock()))
639+
640+
self.assertNoSpans()
639641

640642
def test__build_transaction_selector_pb_single_use(self):
641643
derived = _build_snapshot_derived(multi_use=False)
@@ -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)