Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,18 @@ def get_secret_version_with_tracing(
# applications (e.g., web servers), configure the TracerProvider once at
# application startup.
#
# 1. Initialize the OpenTelemetry TracerProvider with a Console exporter
# Initialize the OpenTelemetry TracerProvider with a Console exporter
provider = TracerProvider()
provider.add_span_processor(SimpleSpanProcessor(ConsoleSpanExporter()))
trace.set_tracer_provider(provider)

# 2. Enable Google Cloud client library tracing
# Enable Google Cloud client library tracing
os.environ["GOOGLE_SDK_PYTHON_TRACING_ENABLED"] = "true"

# 3. Instantiate the client (automatically discovers the active TracerProvider)
# Instantiate the client (automatically discovers the active TracerProvider)
client = secretmanager.SecretManagerServiceClient()

# 4. Construct the resource name and invoke the RPC
# Construct the resource name and invoke the RPC
name = f"projects/{project_id}/secrets/{secret_id}/versions/{version_id}"

try:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,18 @@ async def get_secret_version_with_tracing_async(
# applications (e.g., web servers), configure the TracerProvider once at
# application startup.
#
# 1. Initialize the OpenTelemetry TracerProvider with a Console exporter
# Initialize the OpenTelemetry TracerProvider with a Console exporter
provider = TracerProvider()
provider.add_span_processor(SimpleSpanProcessor(ConsoleSpanExporter()))
trace.set_tracer_provider(provider)

# 2. Enable Google Cloud client library tracing
# Enable Google Cloud client library tracing
os.environ["GOOGLE_SDK_PYTHON_TRACING_ENABLED"] = "true"

# 3. Instantiate the async client
# Instantiate the async client
client = secretmanager.SecretManagerServiceAsyncClient()

# 4. Construct resource name and invoke the async RPC
# Construct resource name and invoke the async RPC
name = f"projects/{project_id}/secrets/{secret_id}/versions/{version_id}"

try:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"""Unit tests for secretmanager_trace_async snippet."""
"""Unit tests for get_secret_version_trace_async snippet."""

import os
from unittest import mock

from get_secret_version_trace_async import (
get_secret_version_with_tracing_async,
)
from google.api_core import _observability
from google.api_core.client_options import ClientOptions
from google.api_core.gapic_v1 import method_async
Expand All @@ -28,10 +31,6 @@
import grpc
import pytest

from secretmanager_trace_async import (
get_secret_version_with_tracing_async,
)

# Ensure OTel tracing capabilities are enabled in the environment
os.environ["GOOGLE_SDK_EXPERIMENTAL_PYTHON_TRACING_ENABLED"] = "true"
os.environ["GOOGLE_SDK_PYTHON_TRACING_ENABLED"] = "true"
Expand Down Expand Up @@ -62,7 +61,10 @@ def patched_init(self, *args, **kwargs):
kind="grpc_asyncio",
)

with mock.patch("secretmanager_trace_async.TracerProvider", return_value=provider):
with mock.patch(
"get_secret_version_trace_async.TracerProvider",
return_value=provider,
):
with mock.patch.object(
SecretManagerServiceGrpcAsyncIOTransport, "__init__", patched_init
):
Expand All @@ -72,11 +74,11 @@ def patched_init(self, *args, **kwargs):
version_id="1",
)

# 1. Verify returned SecretVersion metadata
# Verify returned SecretVersion metadata
assert version.name == "projects/test-project/secrets/my-secret/versions/1"
assert version.state == secretmanager.SecretVersion.State.ENABLED

# 2. Inspect emitted spans
# Inspect emitted spans
spans = exporter.get_finished_spans()
assert len(spans) == 2, f"Expected 2 spans (T3 + T4), but got {len(spans)}"

Expand All @@ -89,15 +91,15 @@ def patched_init(self, *args, **kwargs):
t3_span = t3_spans[0]
t4_span = t4_spans[0]

# 3. Verify T3 logical client span attributes
# Verify T3 logical client span attributes
assert (
t3_span.name
== "google.cloud.secretmanager.v1.SecretManagerService/GetSecretVersion"
)
assert t3_span.attributes.get("rpc.system.name") == "grpc"
assert t3_span.attributes.get("rpc.response.status_code") == "OK"

# 4. Verify T4 wire attempt span attributes
# Verify T4 wire attempt span attributes
assert (
t4_span.name
== "google.cloud.secretmanager.v1.SecretManagerService/GetSecretVersion"
Expand All @@ -109,5 +111,5 @@ def patched_init(self, *args, **kwargs):
assert t4_span.attributes.get("rpc.grpc.status_code") == 0
assert t4_span.attributes.get("rpc.response.status_code") == "OK"

# 5. Verify span hierarchy / parent-child linkage
# Verify span hierarchy / parent-child linkage
assert t4_span.parent.span_id == t3_span.context.span_id
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@ def get_secret_version_with_client_options(
Returns:
The SecretVersion metadata object.
"""
# 1. Initialize an independent TracerProvider (no global registration required)
# Initialize an independent TracerProvider (no global registration required)
provider = TracerProvider()
provider.add_span_processor(SimpleSpanProcessor(ConsoleSpanExporter()))

# 2. Pass the TracerProvider directly via ClientOptions
# Pass the TracerProvider directly via ClientOptions
# Tracing is activated specifically for this client instance, even without
# setting the GOOGLE_SDK_PYTHON_TRACING_ENABLED environment variable.
options = ClientOptions(tracer_provider=provider)
client = secretmanager.SecretManagerServiceClient(client_options=options)

# 3. Construct resource name and invoke RPC
# Construct resource name and invoke RPC
name = f"projects/{project_id}/secrets/{secret_id}/versions/{version_id}"

try:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,21 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"""Unit tests for secretmanager_trace_client_options snippet."""
"""Unit tests for get_secret_version_trace_client_options snippet."""

import os
from unittest import mock

from get_secret_version_trace_client_options import (
get_secret_version_with_client_options,
)
from google.api_core.gapic_v1 import method
from google.cloud import secretmanager
from google.cloud.secretmanager_v1.services.secret_manager_service.transports.grpc import (
SecretManagerServiceGrpcTransport,
)
import grpc

from secretmanager_trace_client_options import (
get_secret_version_with_client_options,
)

os.environ["GOOGLE_SDK_EXPERIMENTAL_PYTHON_TRACING_ENABLED"] = "true"


Expand Down Expand Up @@ -57,7 +56,8 @@ def patched_init(self, *args, **kwargs):
monkeypatch.delenv("GOOGLE_SDK_PYTHON_TRACING_ENABLED", raising=False)

with mock.patch(
"secretmanager_trace_client_options.TracerProvider", return_value=provider
"get_secret_version_trace_client_options.TracerProvider",
return_value=provider,
):
version = get_secret_version_with_client_options(
project_id="test-project",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,21 @@ def get_secret_version_cloud_trace(
# applications (e.g., web servers), configure the TracerProvider once at
# application startup.
#
# 1. Initialize OpenTelemetry TracerProvider with CloudTraceSpanExporter
# Initialize OpenTelemetry TracerProvider with CloudTraceSpanExporter
# NOTE: We also initialize ConsoleSpanExporter to display results from this sample directly.
provider = TracerProvider()
cloud_exporter = CloudTraceSpanExporter()
provider.add_span_processor(BatchSpanProcessor(cloud_exporter))
provider.add_span_processor(SimpleSpanProcessor(ConsoleSpanExporter()))
trace.set_tracer_provider(provider)

# 2. Enable Google Cloud client library tracing
# Enable Google Cloud client library tracing
os.environ["GOOGLE_SDK_PYTHON_TRACING_ENABLED"] = "true"

# 3. Instantiate the client (automatically discovers the active TracerProvider)
# Instantiate the client (automatically discovers the active TracerProvider)
client = secretmanager.SecretManagerServiceClient()

# 4. Construct resource name and invoke RPC
# Construct resource name and invoke RPC
name = f"projects/{project_id}/secrets/{secret_id}/versions/{version_id}"

try:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"""Unit tests for secretmanager_trace_cloud_trace snippet."""
"""Unit tests for get_secret_version_trace_cloud_trace snippet."""

import os
import sys
Expand Down Expand Up @@ -45,10 +45,10 @@ def shutdown(self):
mock_module.CloudTraceSpanExporter = MockCloudTraceSpanExporter
sys.modules["opentelemetry.exporter.cloud_trace"] = mock_module

# flake8: E402 is ignored here because we must patch sys.modules with a mock
# CloudTraceSpanExporter before importing the snippet module in environments
# where opentelemetry-exporter-gcp-trace is not installed.
from secretmanager_trace_cloud_trace import ( # noqa: E402
# flake8: E402 and I100 are ignored here because we must patch sys.modules with
# a mock CloudTraceSpanExporter before importing the snippet module in
# environments where opentelemetry-exporter-gcp-trace is not installed.
from get_secret_version_trace_cloud_trace import ( # noqa: E402, I100
get_secret_version_cloud_trace,
)

Expand All @@ -69,7 +69,7 @@ def patched_init(self, *args, **kwargs):
monkeypatch.setattr(SecretManagerServiceGrpcTransport, "__init__", patched_init)

with mock.patch(
"secretmanager_trace_cloud_trace.CloudTraceSpanExporter"
"get_secret_version_trace_cloud_trace.CloudTraceSpanExporter"
) as mock_cloud_exporter_cls:
instance = MockCloudTraceSpanExporter()
mock_cloud_exporter_cls.return_value = instance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"""Unit tests for secretmanager_trace snippet."""
"""Unit tests for get_secret_version_trace snippet."""

import os
from unittest import mock

from get_secret_version_trace import (
get_secret_version_with_tracing,
)
from google.api_core.client_options import ClientOptions
from google.api_core.gapic_v1 import method
from google.cloud import secretmanager
Expand All @@ -26,8 +29,6 @@
)
import grpc

from secretmanager_trace import get_secret_version_with_tracing

# Ensure OTel tracing capabilities are enabled in the environment
os.environ["GOOGLE_SDK_EXPERIMENTAL_PYTHON_TRACING_ENABLED"] = "true"
os.environ["GOOGLE_SDK_PYTHON_TRACING_ENABLED"] = "true"
Expand All @@ -51,7 +52,10 @@ def patched_init(self, *args, **kwargs):
kind="grpc",
)

with mock.patch("secretmanager_trace.TracerProvider", return_value=provider):
with mock.patch(
"get_secret_version_trace.TracerProvider",
return_value=provider,
):
with mock.patch.object(
SecretManagerServiceGrpcTransport, "__init__", patched_init
):
Expand All @@ -61,11 +65,11 @@ def patched_init(self, *args, **kwargs):
version_id="1",
)

# 1. Verify returned SecretVersion metadata
# Verify returned SecretVersion metadata
assert version.name == "projects/test-project/secrets/my-secret/versions/1"
assert version.state == secretmanager.SecretVersion.State.ENABLED

# 2. Inspect emitted spans
# Inspect emitted spans
spans = exporter.get_finished_spans()
assert len(spans) == 2, f"Expected 2 spans (T3 + T4), but got {len(spans)}"

Expand All @@ -78,15 +82,15 @@ def patched_init(self, *args, **kwargs):
t3_span = t3_spans[0]
t4_span = t4_spans[0]

# 3. Verify T3 logical client span attributes
# Verify T3 logical client span attributes
assert (
t3_span.name
== "google.cloud.secretmanager.v1.SecretManagerService/GetSecretVersion"
)
assert t3_span.attributes.get("rpc.system.name") == "grpc"
assert t3_span.attributes.get("rpc.response.status_code") == "OK"

# 4. Verify T4 wire attempt span attributes
# Verify T4 wire attempt span attributes
assert (
t4_span.name
== "google.cloud.secretmanager.v1.SecretManagerService/GetSecretVersion"
Expand All @@ -98,5 +102,5 @@ def patched_init(self, *args, **kwargs):
assert t4_span.attributes.get("rpc.grpc.status_code") == 0
assert t4_span.attributes.get("rpc.response.status_code") == "OK"

# 5. Verify span hierarchy / parent-child linkage
# Verify span hierarchy / parent-child linkage
assert t4_span.parent.span_id == t3_span.context.span_id
Loading