FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-spanner/examples/trace.py at main · googleapis/python-spanner · GitHub
This repository was archived by the owner on Jun 8, 2026. It is now read-only.
googleapis
/
python-spanner
Public archive
Notifications
You must be signed in to change notification settings
Fork
103
Star
155
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
python-spanner
/
examples
/
trace.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
104 lines (87 loc) · 4.2 KB
Breadcrumbs
python-spanner
/
examples
/
trace.py
Copy path
File metadata and controls
104 lines (87 loc) · 4.2 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# -*- coding: utf-8 -*-
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License
import
os
import
time
import
google
.
cloud
.
spanner
as
spanner
from
opentelemetry
.
exporter
.
cloud_trace
import
CloudTraceSpanExporter
from
opentelemetry
.
exporter
.
otlp
.
proto
.
grpc
.
trace_exporter
import
OTLPSpanExporter
from
opentelemetry
.
sdk
.
trace
import
TracerProvider
from
opentelemetry
.
sdk
.
trace
.
export
import
BatchSpanProcessor
from
opentelemetry
.
sdk
.
trace
.
sampling
import
ALWAYS_ON
from
opentelemetry
import
trace
from
opentelemetry
.
propagate
import
set_global_textmap
from
opentelemetry
.
trace
.
propagation
.
tracecontext
import
TraceContextTextMapPropagator
# Setup common variables that'll be used between Spanner and traces.
project_id
=
os
.
environ
.
get
(
'SPANNER_PROJECT_ID'
,
'test-project'
)
def
spanner_with_cloud_trace
():
# [START spanner_opentelemetry_traces_cloudtrace_usage]
# Setup OpenTelemetry, trace and Cloud Trace exporter.
tracer_provider
=
TracerProvider
(
sampler
=
ALWAYS_ON
)
trace_exporter
=
CloudTraceSpanExporter
(
project_id
=
project_id
)
tracer_provider
.
add_span_processor
(
BatchSpanProcessor
(
trace_exporter
))
# Setup the Cloud Spanner Client.
spanner_client
=
spanner
.
Client
(
project_id
,
observability_options
=
dict
(
tracer_provider
=
tracer_provider
,
enable_extended_tracing
=
True
,
enable_end_to_end_tracing
=
True
),
)
# [END spanner_opentelemetry_traces_cloudtrace_usage]
return
spanner_client
def
spanner_with_otlp
():
# [START spanner_opentelemetry_traces_otlp_usage]
# Setup OpenTelemetry, trace and OTLP exporter.
tracer_provider
=
TracerProvider
(
sampler
=
ALWAYS_ON
)
otlp_exporter
=
OTLPSpanExporter
(
endpoint
=
"http://localhost:4317"
)
tracer_provider
.
add_span_processor
(
BatchSpanProcessor
(
otlp_exporter
))
# Setup the Cloud Spanner Client.
spanner_client
=
spanner
.
Client
(
project_id
,
observability_options
=
dict
(
tracer_provider
=
tracer_provider
,
enable_extended_tracing
=
True
,
enable_end_to_end_tracing
=
True
),
)
# [END spanner_opentelemetry_traces_otlp_usage]
return
spanner_client
def
main
():
# Setup OpenTelemetry, trace and Cloud Trace exporter.
tracer_provider
=
TracerProvider
(
sampler
=
ALWAYS_ON
)
trace_exporter
=
CloudTraceSpanExporter
(
project_id
=
project_id
)
tracer_provider
.
add_span_processor
(
BatchSpanProcessor
(
trace_exporter
))
# Setup the Cloud Spanner Client.
# Change to "spanner_client = spanner_with_otlp" to use OTLP exporter
spanner_client
=
spanner_with_cloud_trace
()
instance
=
spanner_client
.
instance
(
'test-instance'
)
database
=
instance
.
database
(
'test-db'
)
# Set W3C Trace Context as the global propagator for end to end tracing.
set_global_textmap
(
TraceContextTextMapPropagator
())
# Retrieve a tracer from our custom tracer provider.
tracer
=
tracer_provider
.
get_tracer
(
'MyApp'
)
# Now run our queries
with
tracer
.
start_as_current_span
(
'QueryInformationSchema'
):
with
database
.
snapshot
()
as
snapshot
:
with
tracer
.
start_as_current_span
(
'InformationSchema'
):
info_schema
=
snapshot
.
execute_sql
(
'SELECT * FROM INFORMATION_SCHEMA.TABLES'
)
for
row
in
info_schema
:
print
(
row
)
with
tracer
.
start_as_current_span
(
'ServerTimeQuery'
):
with
database
.
snapshot
()
as
snapshot
:
# Purposefully issue a bad SQL statement to examine exceptions
# that get recorded and a ERROR span status.
try
:
data
=
snapshot
.
execute_sql
(
'SELECT CURRENT_TIMESTAMPx()'
)
for
row
in
data
:
print
(
row
)
except
Exception
as
e
:
print
(
e
)
if
__name__
==
'__main__'
:
main
()
Back
|
FazBrowse Home
|
New Git URL