Skip to content

Commit

Permalink
opentelemetry-tracing: update dependencies from 2021 to 2024
Browse files Browse the repository at this point in the history
This change non-invasively introduces dependencies of opentelemetry
bringing in the latest dependencies and modernizing them.

While here also brought in modern span attributes:
* otel.scope.name
* otel.scope.version

Updates googleapis#1170
Fixes googleapis#1173
Built from PR#1172
  • Loading branch information
odeke-em committed Sep 11, 2024
1 parent a941adb commit 364e24a
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 8 deletions.
28 changes: 27 additions & 1 deletion google/cloud/spanner_v1/_opentelemetry_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,39 @@

from google.api_core.exceptions import GoogleAPICallError
from google.cloud.spanner_v1 import SpannerClient
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.attributes import (
OTEL_SCOPE_NAME,
OTEL_SCOPE_VERSION,
)

HAS_OPENTELEMETRY_INSTALLED = True
except ImportError:
HAS_OPENTELEMETRY_INSTALLED = False

LIB_FQNAME = 'cloud.google.com/python/spanner'
TRACER_NAME = LIB_FQNAME
TRACER_VERSION = LIB_VERSION


def get_tracer(tracer_provider=None):
"""
get_tracer is a utility to unify and simplify retrieval of the tracer, without
leaking implementation details given that retrieving a tracer requires providing
the full qualified library name and version.
When the tracer_provider is set, it'll retrieve the tracer from it, otherwise
it'll fall back to the global tracer provider and use this library's specific semantics.
"""
if not tracer_provider:
# Acquire the global tracer provider.
tracer_provider = trace.get_tracer_provider()

return tracer_provider.get_tracer(TRACER_NAME, TRACER_VERSION)


@contextmanager
def trace_call(name, session, extra_attributes=None):
Expand All @@ -35,14 +59,16 @@ def trace_call(name, session, extra_attributes=None):
yield None
return

tracer = trace.get_tracer(__name__)
tracer = get_tracer()

# Set base attributes that we know for every trace created
attributes = {
"db.type": "spanner",
"db.url": SpannerClient.DEFAULT_ENDPOINT,
"db.instance": session._database.name,
"net.host.name": SpannerClient.DEFAULT_ENDPOINT,
OTEL_SCOPE_NAME: LIB_FQNAME,
OTEL_SCOPE_VERSION: LIB_VERSION,
}

if extra_attributes:
Expand Down
7 changes: 4 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@
]
extras = {
"tracing": [
"opentelemetry-api >= 1.1.0",
"opentelemetry-sdk >= 1.1.0",
"opentelemetry-instrumentation >= 0.20b0, < 0.23dev",
"opentelemetry-api >= 1.24.0",
"opentelemetry-sdk >= 1.24.0",
"opentelemetry-instrumentation >= 0.46b0",
"opentelemetry-semantic-conventions >= 0.46b0",
],
"libcst": "libcst >= 0.2.5",
}
Expand Down
7 changes: 4 additions & 3 deletions testing/constraints-3.7.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ grpc-google-iam-v1==0.12.4
libcst==0.2.5
proto-plus==1.22.0
sqlparse==0.4.4
opentelemetry-api==1.1.0
opentelemetry-sdk==1.1.0
opentelemetry-instrumentation==0.20b0
opentelemetry-api>=1.24.0
opentelemetry-sdk>=1.24.0
opentelemetry-instrumentation==0.46b0
opentelemetry-semantic-conventions==0.46b0
protobuf==3.20.2
deprecated==1.2.14
grpc-interceptor==0.15.4
5 changes: 5 additions & 0 deletions tests/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@
InMemorySpanExporter,
)
from opentelemetry.trace.status import StatusCode
from opentelemetry.semconv.attributes import (
OTEL_SCOPE_NAME,
OTEL_SCOPE_VERSION,
)

trace.set_tracer_provider(TracerProvider())

HAS_OPENTELEMETRY_INSTALLED = True
except ImportError:
HAS_OPENTELEMETRY_INSTALLED = False
OTEL_SCOPE_NAME = "otel.scope.name"

StatusCode = mock.Mock()

Expand Down
10 changes: 9 additions & 1 deletion tests/unit/test__opentelemetry_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
from google.api_core.exceptions import GoogleAPICallError
from google.cloud.spanner_v1 import _opentelemetry_tracing

from tests._helpers import OpenTelemetryBase, HAS_OPENTELEMETRY_INSTALLED
from tests._helpers import (
OpenTelemetryBase,
HAS_OPENTELEMETRY_INSTALLED,
OTEL_SCOPE_NAME,
)


def _make_rpc_error(error_cls, trailing_metadata=None):
Expand Down Expand Up @@ -59,6 +63,7 @@ def test_trace_call(self):
"db.type": "spanner",
"db.url": "spanner.googleapis.com",
"net.host.name": "spanner.googleapis.com",
OTEL_SCOPE_NAME: "cloud.google.com/python/spanner",
}
expected_attributes.update(extra_attributes)

Expand All @@ -84,6 +89,7 @@ def test_trace_error(self):
"db.type": "spanner",
"db.url": "spanner.googleapis.com",
"net.host.name": "spanner.googleapis.com",
OTEL_SCOPE_NAME: "cloud.google.com/python/spanner",
}
expected_attributes.update(extra_attributes)

Expand All @@ -110,6 +116,7 @@ def test_trace_grpc_error(self):
"db.type": "spanner",
"db.url": "spanner.googleapis.com:443",
"net.host.name": "spanner.googleapis.com:443",
OTEL_SCOPE_NAME: "cloud.google.com/python/spanner",
}
expected_attributes.update(extra_attributes)

Expand All @@ -133,6 +140,7 @@ def test_trace_codeless_error(self):
"db.type": "spanner",
"db.url": "spanner.googleapis.com:443",
"net.host.name": "spanner.googleapis.com:443",
OTEL_SCOPE_NAME: "cloud.google.com/python/spanner",
}
expected_attributes.update(extra_attributes)

Expand Down

0 comments on commit 364e24a

Please sign in to comment.