|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import sys |
| 16 | +from unittest import mock |
| 17 | + |
| 18 | +from google.api_core import _observability |
| 19 | +from google.api_core.client_options import ClientOptions |
| 20 | + |
| 21 | + |
| 22 | +def test_is_otel_capabilities_enabled_disabled(monkeypatch): |
| 23 | + monkeypatch.setenv("GOOGLE_CLOUD_PYTHON_TRACING_ENABLED", "false") |
| 24 | + assert not _observability.is_otel_capabilities_enabled() |
| 25 | + |
| 26 | + |
| 27 | +def test_is_otel_capabilities_enabled_otel_missing(monkeypatch): |
| 28 | + monkeypatch.setenv("GOOGLE_CLOUD_PYTHON_TRACING_ENABLED", "true") |
| 29 | + # Simulate OTel not being installed by blocking imports |
| 30 | + monkeypatch.setitem(sys.modules, "opentelemetry.instrumentation.grpc", None) |
| 31 | + |
| 32 | + assert not _observability.is_otel_capabilities_enabled() |
| 33 | + |
| 34 | + |
| 35 | +def test_is_otel_capabilities_enabled_otel_installed(monkeypatch): |
| 36 | + monkeypatch.setenv("GOOGLE_CLOUD_PYTHON_TRACING_ENABLED", "true") |
| 37 | + |
| 38 | + mock_otel = mock.Mock() |
| 39 | + mock_otel_grpc = mock_otel.instrumentation.grpc |
| 40 | + |
| 41 | + monkeypatch.setitem(sys.modules, "opentelemetry", mock_otel) |
| 42 | + monkeypatch.setitem( |
| 43 | + sys.modules, "opentelemetry.instrumentation", mock_otel.instrumentation |
| 44 | + ) |
| 45 | + monkeypatch.setitem( |
| 46 | + sys.modules, "opentelemetry.instrumentation.grpc", mock_otel_grpc |
| 47 | + ) |
| 48 | + |
| 49 | + assert _observability.is_otel_capabilities_enabled() |
| 50 | + |
| 51 | + |
| 52 | +def test_apply_otel_capabilities_to_channel_enabled_otel_installed(monkeypatch): |
| 53 | + mock_channel = mock.Mock() |
| 54 | + mock_intercepted_channel = mock.Mock() |
| 55 | + |
| 56 | + mock_otel = mock.Mock() |
| 57 | + mock_otel_grpc = mock_otel.instrumentation.grpc |
| 58 | + mock_interceptor = mock.Mock() |
| 59 | + |
| 60 | + mock_otel_grpc.client_interceptor.return_value = mock_interceptor |
| 61 | + mock_otel_grpc.intercept_channel.return_value = mock_intercepted_channel |
| 62 | + |
| 63 | + monkeypatch.setitem(sys.modules, "opentelemetry", mock_otel) |
| 64 | + monkeypatch.setitem( |
| 65 | + sys.modules, "opentelemetry.instrumentation", mock_otel.instrumentation |
| 66 | + ) |
| 67 | + monkeypatch.setitem( |
| 68 | + sys.modules, "opentelemetry.instrumentation.grpc", mock_otel_grpc |
| 69 | + ) |
| 70 | + |
| 71 | + result = _observability.apply_otel_capabilities_to_channel(mock_channel) |
| 72 | + |
| 73 | + assert result is mock_intercepted_channel |
| 74 | + mock_otel_grpc.client_interceptor.assert_called_once_with(tracer_provider=None) |
| 75 | + mock_otel_grpc.intercept_channel.assert_called_once_with( |
| 76 | + mock_channel, mock_interceptor |
| 77 | + ) |
| 78 | + |
| 79 | + |
| 80 | +def test_apply_otel_capabilities_to_channel_enabled_via_config(monkeypatch): |
| 81 | + # Tracing enabled via config (tracer_provider is set) |
| 82 | + mock_tracer_provider = object() |
| 83 | + options = ClientOptions(tracer_provider=mock_tracer_provider) |
| 84 | + |
| 85 | + mock_channel = mock.Mock() |
| 86 | + mock_intercepted_channel = mock.Mock() |
| 87 | + |
| 88 | + mock_otel = mock.Mock() |
| 89 | + mock_otel_grpc = mock_otel.instrumentation.grpc |
| 90 | + mock_interceptor = mock.Mock() |
| 91 | + |
| 92 | + mock_otel_grpc.client_interceptor.return_value = mock_interceptor |
| 93 | + mock_otel_grpc.intercept_channel.return_value = mock_intercepted_channel |
| 94 | + |
| 95 | + monkeypatch.setitem(sys.modules, "opentelemetry", mock_otel) |
| 96 | + monkeypatch.setitem( |
| 97 | + sys.modules, "opentelemetry.instrumentation", mock_otel.instrumentation |
| 98 | + ) |
| 99 | + monkeypatch.setitem( |
| 100 | + sys.modules, "opentelemetry.instrumentation.grpc", mock_otel_grpc |
| 101 | + ) |
| 102 | + |
| 103 | + result = _observability.apply_otel_capabilities_to_channel( |
| 104 | + mock_channel, client_options=options |
| 105 | + ) |
| 106 | + |
| 107 | + assert result is mock_intercepted_channel |
| 108 | + mock_otel_grpc.client_interceptor.assert_called_once_with( |
| 109 | + tracer_provider=mock_tracer_provider |
| 110 | + ) |
| 111 | + mock_otel_grpc.intercept_channel.assert_called_once_with( |
| 112 | + mock_channel, mock_interceptor |
| 113 | + ) |
| 114 | + |
| 115 | + |
| 116 | +def test_apply_otel_capabilities_to_channel_enabled_via_dict_config(monkeypatch): |
| 117 | + # Tracing enabled via dict config |
| 118 | + mock_tracer_provider = object() |
| 119 | + options = {"tracer_provider": mock_tracer_provider} |
| 120 | + |
| 121 | + mock_channel = mock.Mock() |
| 122 | + mock_intercepted_channel = mock.Mock() |
| 123 | + |
| 124 | + mock_otel = mock.Mock() |
| 125 | + mock_otel_grpc = mock_otel.instrumentation.grpc |
| 126 | + mock_interceptor = mock.Mock() |
| 127 | + |
| 128 | + mock_otel_grpc.client_interceptor.return_value = mock_interceptor |
| 129 | + mock_otel_grpc.intercept_channel.return_value = mock_intercepted_channel |
| 130 | + |
| 131 | + monkeypatch.setitem(sys.modules, "opentelemetry", mock_otel) |
| 132 | + monkeypatch.setitem( |
| 133 | + sys.modules, "opentelemetry.instrumentation", mock_otel.instrumentation |
| 134 | + ) |
| 135 | + monkeypatch.setitem( |
| 136 | + sys.modules, "opentelemetry.instrumentation.grpc", mock_otel_grpc |
| 137 | + ) |
| 138 | + |
| 139 | + result = _observability.apply_otel_capabilities_to_channel( |
| 140 | + mock_channel, client_options=options |
| 141 | + ) |
| 142 | + |
| 143 | + assert result is mock_intercepted_channel |
| 144 | + mock_otel_grpc.client_interceptor.assert_called_once_with( |
| 145 | + tracer_provider=mock_tracer_provider |
| 146 | + ) |
| 147 | + mock_otel_grpc.intercept_channel.assert_called_once_with( |
| 148 | + mock_channel, mock_interceptor |
| 149 | + ) |
0 commit comments