feat(otel): add --otel_to_cloud experimental support

Cloud Trace, Cloud Monitoring and Cloud Logging integrations are set up via OTel if otel_to_cloud CLI param/fast_api arg is provided.

This is similar to current Cloud Trace integration via trace_to_cloud, just extended to Monitoring and Logging as well.

PiperOrigin-RevId: 807385680
This commit is contained in:
Kacper Jawoszek
2025-09-15 14:32:22 -07:00
committed by Copybara-Service
parent d6d4b144e9
commit 1ae0b82f56
15 changed files with 430 additions and 30 deletions
+2 -2
View File
@@ -16,10 +16,10 @@ import gc
import sys
from unittest import mock
from google.adk import telemetry
from google.adk.agents import base_agent
from google.adk.agents.llm_agent import Agent
from google.adk.models.base_llm import BaseLlm
from google.adk.telemetry import tracing
from google.adk.tools import FunctionTool
from google.adk.utils.context_utils import Aclosing
from google.genai.types import Part
@@ -74,7 +74,7 @@ def mock_start_as_current_span(monkeypatch: pytest.MonkeyPatch) -> mock.Mock:
tracer, 'start_as_current_span', mock_start_as_current_span
)
do_replace(telemetry.tracer)
do_replace(tracing.tracer)
do_replace(base_agent.tracer)
return mock_start_as_current_span
@@ -0,0 +1,57 @@
# Copyright 2025 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.
from unittest import mock
from google.adk.telemetry.google_cloud import get_gcp_exporters
import pytest
@pytest.mark.parametrize("enable_cloud_tracing", [True, False])
@pytest.mark.parametrize("enable_cloud_metrics", [True, False])
@pytest.mark.parametrize("enable_cloud_logging", [True, False])
def test_get_gcp_exporters(
enable_cloud_tracing: bool,
enable_cloud_metrics: bool,
enable_cloud_logging: bool,
monkeypatch: pytest.MonkeyPatch,
):
"""
Test initializing correct providers in setup_otel
when enabling telemetry via Google O11y.
"""
# Arrange.
# Mocking google.auth.default to improve the test time.
auth_mock = mock.MagicMock()
auth_mock.return_value = ("", "project-id")
monkeypatch.setattr(
"google.auth.default",
auth_mock,
)
# Act.
otel_hooks = get_gcp_exporters(
enable_cloud_tracing=enable_cloud_tracing,
enable_cloud_metrics=enable_cloud_metrics,
enable_cloud_logging=enable_cloud_logging,
)
# Assert.
# If given telemetry type was enabled,
# the corresponding provider should be set.
assert len(otel_hooks.span_processors) == (1 if enable_cloud_tracing else 0)
assert len(otel_hooks.metric_readers) == (1 if enable_cloud_metrics else 0)
assert len(otel_hooks.log_record_processors) == (
1 if enable_cloud_logging else 0
)
+3 -3
View File
@@ -23,9 +23,9 @@ from google.adk.agents.llm_agent import LlmAgent
from google.adk.models.llm_request import LlmRequest
from google.adk.models.llm_response import LlmResponse
from google.adk.sessions.in_memory_session_service import InMemorySessionService
from google.adk.telemetry import trace_call_llm
from google.adk.telemetry import trace_merged_tool_calls
from google.adk.telemetry import trace_tool_call
from google.adk.telemetry.tracing import trace_call_llm
from google.adk.telemetry.tracing import trace_merged_tool_calls
from google.adk.telemetry.tracing import trace_tool_call
from google.adk.tools.base_tool import BaseTool
from google.genai import types
import pytest