Skip to content

Commit 4d0704e

Browse files
Set/ Override Open Telemetry enabled PublisherOption in the Publish client
1 parent 31045a6 commit 4d0704e

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

google/cloud/pubsub_v1/publisher/client.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import typing
2323
from typing import Any, Dict, Optional, Sequence, Tuple, Type, Union
2424
import warnings
25+
import sys
2526

2627
from google.api_core import gapic_v1
2728
from google.auth.credentials import AnonymousCredentials # type: ignore
@@ -153,6 +154,22 @@ def __init__(
153154
# The object controlling the message publishing flow
154155
self._flow_controller = FlowController(self.publisher_options.flow_control)
155156

157+
self._open_telemetry_enabled = (
158+
self.publisher_options.enable_open_telemetry_tracing
159+
)
160+
# OpenTelemetry features used by the library are not supported in Python versions <= 3.7.
161+
# Refer https://github.com/open-telemetry/opentelemetry-python/issues/3993#issuecomment-2211976389
162+
if (
163+
self.publisher_options.enable_open_telemetry_tracing
164+
and sys.version_info.major == 3
165+
and sys.version_info.minor < 8
166+
):
167+
warnings.warn(
168+
message="Open Telemetry for Python version 3.7 or lower is not supported. Disabling Open Telemetry tracing.",
169+
category=RuntimeWarning,
170+
)
171+
self._open_telemetry_enabled = False
172+
156173
@classmethod
157174
def from_service_account_file( # type: ignore[override]
158175
cls,

google/cloud/pubsub_v1/types.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ class PublisherOptions(NamedTuple):
177177
enable_open_telemetry_tracing: bool = False # disabled by default
178178
"""Open Telemetry tracing is enabled if this is set to True."""
179179

180+
180181
# Define the type class and default values for flow control settings.
181182
#
182183
# This class is used when creating a publisher or subscriber client, and

tests/unit/pubsub_v1/publisher/test_publisher_client.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,38 @@ def test_init_w_custom_transport(creds):
129129
assert client.batch_settings.max_messages == 100
130130

131131

132+
@pytest.mark.parametrize(
133+
"enable_open_telemetry",
134+
[
135+
True,
136+
False,
137+
],
138+
)
139+
def test_open_telemetry_publisher_options(creds, enable_open_telemetry):
140+
if sys.version_info >= (3, 8) or enable_open_telemetry is False:
141+
client = publisher.Client(
142+
publisher_options=types.PublisherOptions(
143+
enable_open_telemetry_tracing=enable_open_telemetry
144+
),
145+
credentials=creds,
146+
)
147+
assert client._open_telemetry_enabled == enable_open_telemetry
148+
else:
149+
# Open Telemetry is not supported and hence disabled for Python
150+
# versions 3.7 or below
151+
with pytest.warns(
152+
RuntimeWarning,
153+
match="Open Telemetry for Python version 3.7 or lower is not supported. Disabling Open Telemetry tracing.",
154+
):
155+
client = publisher.Client(
156+
publisher_options=types.PublisherOptions(
157+
enable_open_telemetry_tracing=True
158+
),
159+
credentials=creds,
160+
)
161+
assert client._open_telemetry_enabled is False
162+
163+
132164
def test_init_w_api_endpoint(creds):
133165
client_options = {"api_endpoint": "testendpoint.google.com"}
134166
client = publisher.Client(client_options=client_options, credentials=creds)

0 commit comments

Comments
 (0)