diff --git a/examples/trace.py b/examples/trace.py index d0611c8272..fd61f3aa04 100644 --- a/examples/trace.py +++ b/examples/trace.py @@ -35,11 +35,12 @@ def main(): BatchSpanProcessor(traceExporter)) trace.set_tracer_provider(tracerProvider) # Retrieve the set shared tracer. - tracer = tracerProvider.get_tracer('cloud.google.com/python/spanner', spanner.__version__) + tracer = tracerProvider.get_tracer('MyPackage') # Setup the Cloud Spanner Client. spanner_client = spanner.Client(project_id) - # Alternatively you can directly pass in the tracerProvider into the spanner client. + # Alternatively you can directly pass in the tracerProvider into + # the spanner client, otherwise the global tracer shall be used. if False: opts = dict(tracer_provider=tracerProvider) spanner_client = spanner.Client(project_id, observability_options=opts) diff --git a/google/cloud/spanner_v1/_opentelemetry_tracing.py b/google/cloud/spanner_v1/_opentelemetry_tracing.py index 944852cd59..337fbecec4 100644 --- a/google/cloud/spanner_v1/_opentelemetry_tracing.py +++ b/google/cloud/spanner_v1/_opentelemetry_tracing.py @@ -19,12 +19,16 @@ from google.api_core.exceptions import GoogleAPICallError from google.cloud.spanner_v1 import SpannerClient -from google.cloud.spanner_v1 import gapic_version as TRACER_VERSION +from google.cloud.spanner_v1 import gapic_version as LIB_VERSION try: from opentelemetry import trace from opentelemetry.trace.status import Status, StatusCode from opentelemetry.semconv.trace import SpanAttributes + from opentelemetry.semconv.attributes import ( + OTEL_SCOPE_NAME, + OTEL_SCOPE_VERSION, + ) HAS_OPENTELEMETRY_INSTALLED = True DB_SYSTEM = SpanAttributes.DB_SYSTEM @@ -34,10 +38,13 @@ DB_STATEMENT = SpanAttributes.DB_STATEMENT except ImportError: HAS_OPENTELEMETRY_INSTALLED = False + DB_STATEMENT = 'db.statement' EXTENDED_TRACING_ENABLED = os.environ.get('SPANNER_ENABLE_EXTENDED_TRACING', '') == 'true' -TRACER_NAME = 'cloud.google.com/python/spanner' +LIB_FQNAME = 'cloud.google.com/python/spanner' +TRACER_NAME = LIB_FQNAME +TRACER_VERSION = LIB_VERSION def get_tracer(tracer_provider=None): """ @@ -81,6 +88,8 @@ def trace_call(name, session, extra_attributes=None, observability_options=None) DB_CONNECTION_STRING: spanner_endpoint, DB_NAME: session._database.name, NET_HOST_NAME: spanner_endpoint, + OTEL_SCOPE_NAME: LIB_FQNAME, + OTEL_SCOPE_VERSION: TRACER_VERSION, } if extra_attributes: