forked from googleapis/python-spanner
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use modernized and standardized OpenTelemetry when tracing
This change modernizes trace span attributes by using OpenTelemetry's semantic conventions that are standardized and allow for much better common ground adoption by broader systems, even more as Google Cloud Tracing & Monitoring pushes towards OpenTelemetry more. With this change we've made the replacement of these fields, directly with imports from `opentelemetry.semconv.trace.SpanAttributes`, as: * "db.type" => DB_SYSTEM aka "db.system" * "db.url" => DB_CONNECTION_STRING aka "db.connection_string" * "db.instance" => DB_NAME aka "db.name" * "net.host.name" => NET_HOST_NAME aka "net.host.name" While here, also updated opentelemetry-(api, sdk) dependencies to use versions "1.25.0", then opentelemetry-(instrumentation) to "0.46b0" Also added in an option toggled by environment variable ENABLE_EXTENDED_TRACING=true which allows spans to be annotated with the SQL statement keyed by "db.statement" Fixes googleapis#1170 Fixes googleapis#1171 Fixes googleapis#1173
- Loading branch information
Showing
15 changed files
with
282 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# -*- 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.sdk.trace import TracerProvider | ||
from opentelemetry.sdk.trace.export import BatchExportSpanProcessor | ||
from opentelemetry.sdk.trace.sampling import ALWAYS_ON | ||
from opentelemetry import trace | ||
|
||
|
||
def main(): | ||
# Setup OpenTelemetry, trace and Cloud Trace exporter. | ||
sampler = ALWAYS_ON | ||
tracerProvider = TracerProvider(sampler=sampler) | ||
tracerProvider.add_span_processor( | ||
BatchExportSpanProcessor(CloudTraceSpanExporter())) | ||
trace.set_tracer_provider(tracerProvider) | ||
tracer = trace.get_tracer(__name__) | ||
|
||
# Setup the Cloud Spanner Client. | ||
project_id = os.environ.get('SPANNER_PROJECT_ID') | ||
spanner_client = spanner.Client(project_id) | ||
instance = spanner_client.instance('test-instance') | ||
database = instance.database('test-db') | ||
|
||
# Now run our queries | ||
with tracer.start_as_current_span('QueryDatabase'): | ||
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. | ||
data = snapshot.execute_sql('SELECT CURRENT_TIMESTAMPx()') | ||
for row in data: | ||
print(row) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.