|
| 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 | +"""Opt-in for the ADK telemetry schema version. |
| 16 | +
|
| 17 | +``ADK_TELEMETRY_SCHEMA_VERSION_OPT_IN`` lets a deployment pin which version of |
| 18 | +the ADK telemetry format (span names, span/log attributes, metrics) it emits. |
| 19 | +
|
| 20 | +Why this exists: |
| 21 | +
|
| 22 | +* **Staged migration.** Telemetry format changes are breaking for anyone who |
| 23 | + built dashboards/alerts on the previous shape. The version env lets users |
| 24 | + fall back to the legacy format if they rely on it, and migrate on their own |
| 25 | + schedule. |
| 26 | +* **Fast iteration on managed services.** GCP-managed runtimes (e.g. Agent |
| 27 | + Runtime / Vertex Agent Engine) can pin themselves to the latest version and |
| 28 | + iterate quickly, decoupled from the broader user base. |
| 29 | +* **Eventually removable.** Ideally this knob is phased out once ADK is |
| 30 | + ({almost,} fully) OTel semconv compliant, after which we no longer expect |
| 31 | + breaking telemetry changes. |
| 32 | +
|
| 33 | +Default resolution: |
| 34 | +
|
| 35 | +* ``2`` on Agent Engine, detected via the presence of the |
| 36 | + ``GOOGLE_CLOUD_AGENT_ENGINE_ID`` env var. |
| 37 | +* ``1`` everywhere else. |
| 38 | +
|
| 39 | +Migration plan (steps land incrementally; this comment should be kept in sync |
| 40 | +as each one ships): |
| 41 | +
|
| 42 | +1. The ``invocation`` span is updated to the ``invoke_workflow`` span. |
| 43 | +2. The ``call_llm`` span is removed. |
| 44 | +3. The ``execute_tool`` content-bearing attributes become OTel semconv aligned. |
| 45 | +4. The experimental OTel GenAI semconv becomes the default in both |
| 46 | + ``opentelemetry-instrumentation-google-genai`` and ADK's native |
| 47 | + instrumentation. |
| 48 | +5. ``2`` becomes the global default (this knob flips). |
| 49 | +6. After ~6 months, the env var is phased out entirely, along with support for |
| 50 | + the LEGACY schema. |
| 51 | +""" |
| 52 | + |
| 53 | +from __future__ import annotations |
| 54 | + |
| 55 | +import os |
| 56 | + |
| 57 | +# Env var users set to pin the ADK telemetry schema version ("1" or "2"). |
| 58 | +ADK_TELEMETRY_SCHEMA_VERSION_OPT_IN = "ADK_TELEMETRY_SCHEMA_VERSION_OPT_IN" |
| 59 | + |
| 60 | +# Presence of this env var indicates the process runs on Vertex Agent Engine. |
| 61 | +GOOGLE_CLOUD_AGENT_ENGINE_ID = "GOOGLE_CLOUD_AGENT_ENGINE_ID" |
| 62 | + |
| 63 | +# Legacy telemetry format: top-level ``invocation`` span, no entrypoint |
| 64 | +# ``invoke_workflow`` span/metric. |
| 65 | +SCHEMA_VERSION_LEGACY = 1 |
| 66 | + |
| 67 | +# OTel-semconv-aligned telemetry format: the ``invocation`` span is replaced by |
| 68 | +# an entrypoint ``invoke_workflow {entrypoint}`` span + duration metric. |
| 69 | +SCHEMA_VERSION_SEMCONV_ALIGNED = 2 |
| 70 | + |
| 71 | + |
| 72 | +def resolve_schema_version() -> int: |
| 73 | + """Resolves the active ADK telemetry schema version. |
| 74 | +
|
| 75 | + Precedence: ``ADK_TELEMETRY_SCHEMA_VERSION_OPT_IN`` (if set to a recognized |
| 76 | + value) > ``2`` on Agent Engine > ``1``. |
| 77 | +
|
| 78 | + Returns: |
| 79 | + Either :data:`SCHEMA_VERSION_LEGACY` or |
| 80 | + :data:`SCHEMA_VERSION_SEMCONV_ALIGNED`. |
| 81 | + """ |
| 82 | + opt_in = os.environ.get(ADK_TELEMETRY_SCHEMA_VERSION_OPT_IN, "").strip() |
| 83 | + if opt_in == "1": |
| 84 | + return SCHEMA_VERSION_LEGACY |
| 85 | + if opt_in == "2": |
| 86 | + return SCHEMA_VERSION_SEMCONV_ALIGNED |
| 87 | + |
| 88 | + # Unset/unrecognized: default to v2 on Agent Engine, v1 elsewhere. |
| 89 | + if os.environ.get(GOOGLE_CLOUD_AGENT_ENGINE_ID): |
| 90 | + return SCHEMA_VERSION_SEMCONV_ALIGNED |
| 91 | + return SCHEMA_VERSION_LEGACY |
0 commit comments