mirror of
https://github.com/encounter/adk-python.git
synced 2026-07-09 18:19:28 -07:00
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: 807230668
This commit is contained in:
committed by
Copybara-Service
parent
b9735b2193
commit
7870480c63
@@ -17,10 +17,10 @@ from typing import Dict
|
||||
from typing import Optional
|
||||
from unittest import mock
|
||||
|
||||
from google.adk import telemetry
|
||||
from google.adk.agents.llm_agent import Agent
|
||||
from google.adk.events.event import Event
|
||||
from google.adk.flows.llm_flows.functions import handle_function_calls_async
|
||||
from google.adk.telemetry import tracing
|
||||
from google.adk.tools.function_tool import FunctionTool
|
||||
from google.genai import types
|
||||
|
||||
@@ -65,7 +65,7 @@ async def test_simple_function_with_mocked_tracer(monkeypatch):
|
||||
mock_start_as_current_span_func.return_value = returned_context_manager_mock
|
||||
|
||||
monkeypatch.setattr(
|
||||
telemetry.tracer, 'start_as_current_span', mock_start_as_current_span_func
|
||||
tracing.tracer, 'start_as_current_span', mock_start_as_current_span_func
|
||||
)
|
||||
|
||||
mock_adk_trace_tool_call = mock.Mock()
|
||||
|
||||
@@ -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
|
||||
)
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user