Files
wehub-resource-sync ec2b666284
Continuous Integration / Pre-commit Linter (push) Waiting to run
Continuous Integration / Mypy Check (Python 3.10) (push) Waiting to run
Continuous Integration / Mypy Check (Python 3.11) (push) Waiting to run
Continuous Integration / Mypy Check (Python 3.12) (push) Waiting to run
Continuous Integration / Mypy Check (Python 3.13) (push) Waiting to run
Continuous Integration / Unit Tests (Python 3.10) (push) Waiting to run
Continuous Integration / Unit Tests (Python 3.11) (push) Waiting to run
Continuous Integration / Unit Tests (Python 3.12) (push) Waiting to run
Continuous Integration / Unit Tests (Python 3.13) (push) Waiting to run
Continuous Integration / Unit Tests (Python 3.14) (push) Waiting to run
Continuous Integration / A2A v0.3 Tests (Python 3.10) (push) Waiting to run
Continuous Integration / A2A v0.3 Tests (Python 3.11) (push) Waiting to run
Continuous Integration / A2A v0.3 Tests (Python 3.12) (push) Waiting to run
Continuous Integration / A2A v0.3 Tests (Python 3.13) (push) Waiting to run
Continuous Integration / A2A v0.3 Tests (Python 3.14) (push) Waiting to run
Copybara PR Handler / close-imported-pr (push) Waiting to run
chore: import upstream snapshot with attribution
2026-07-13 13:25:13 +08:00

120 lines
3.4 KiB
Python

# Copyright 2026 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
from unittest import mock
from google.adk.telemetry.setup import maybe_set_otel_providers
import pytest
@pytest.fixture
def mock_os_environ():
initial_env = os.environ.copy()
with mock.patch.dict(os.environ, initial_env, clear=False) as m:
yield m
@pytest.mark.parametrize(
"env_vars, should_setup_trace, should_setup_metrics, should_setup_logs",
[
(
{"OTEL_EXPORTER_OTLP_TRACES_ENDPOINT": "some-endpoint"},
True,
False,
False,
),
(
{"OTEL_EXPORTER_OTLP_METRICS_ENDPOINT": "some-endpoint"},
False,
True,
False,
),
(
{"OTEL_EXPORTER_OTLP_LOGS_ENDPOINT": "some-endpoint"},
False,
False,
True,
),
(
{
"OTEL_EXPORTER_OTLP_TRACES_ENDPOINT": "some-endpoint",
"OTEL_EXPORTER_OTLP_METRICS_ENDPOINT": "some-endpoint",
"OTEL_EXPORTER_OTLP_LOGS_ENDPOINT": "some-endpoint",
},
True,
True,
True,
),
(
{"OTEL_EXPORTER_OTLP_ENDPOINT": "some-endpoint"},
True,
True,
True,
),
],
)
def test_maybe_set_otel_providers(
env_vars: dict[str, str],
should_setup_trace: bool,
should_setup_metrics: bool,
should_setup_logs: bool,
monkeypatch: pytest.MonkeyPatch,
mock_os_environ, # pylint: disable=unused-argument,redefined-outer-name
):
"""
Test initializing correct providers in setup_otel
when providing OTel env variables.
"""
# Arrange.
for k, v in env_vars.items():
monkeypatch.setenv(k, v)
trace_provider_mock = mock.MagicMock()
monkeypatch.setattr(
"opentelemetry.trace.set_tracer_provider",
trace_provider_mock,
)
meter_provider_mock = mock.MagicMock()
monkeypatch.setattr(
"opentelemetry.metrics.set_meter_provider",
meter_provider_mock,
)
logs_provider_mock = mock.MagicMock()
monkeypatch.setattr(
"opentelemetry._logs.set_logger_provider",
logs_provider_mock,
)
monkeypatch.setattr(
"google.adk.telemetry.setup._get_otel_span_exporter",
lambda: mock.MagicMock(),
)
monkeypatch.setattr(
"google.adk.telemetry.setup._get_otel_metrics_exporter",
lambda: mock.MagicMock(),
)
monkeypatch.setattr(
"google.adk.telemetry.setup._get_otel_logs_exporter",
lambda: mock.MagicMock(),
)
# Act.
maybe_set_otel_providers()
# Assert.
# If given telemetry type was enabled,
# the corresponding provider should be set.
assert trace_provider_mock.call_count == (1 if should_setup_trace else 0)
assert meter_provider_mock.call_count == (1 if should_setup_metrics else 0)
assert logs_provider_mock.call_count == (1 if should_setup_logs else 0)