Skip to content

Commit

Permalink
tracing: add otel.scope.name,version for every span
Browse files Browse the repository at this point in the history
  • Loading branch information
odeke-em committed Sep 10, 2024
1 parent ceb7da2 commit c32adc8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
5 changes: 3 additions & 2 deletions examples/trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 11 additions & 2 deletions google/cloud/spanner_v1/_opentelemetry_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
"""
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit c32adc8

Please sign in to comment.