Skip to content

Commit 8fc25f1

Browse files
GWealecopybara-github
authored andcommitted
fix: emit standard OTel cloud.resource_id for Agent Engine telemetry
The agent-engine branch of get_gcp_resource() emitted the resource id under a non-standard "cloud.resource.id" key, so the Agent Engine dashboard's cloud.resource_id filter matched no rows and every panel read 0. Use the OTel CLOUD_RESOURCE_ID constant ("cloud.resource_id"). Close #6247 Co-authored-by: George Weale <gweale@google.com> PiperOrigin-RevId: 943528352
1 parent 63561ce commit 8fc25f1

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

src/google/adk/telemetry/google_cloud.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@
4343

4444
logger = logging.getLogger("google_adk." + __name__)
4545

46+
try:
47+
from opentelemetry.semconv._incubating.attributes.cloud_attributes import CLOUD_RESOURCE_ID
48+
except ImportError:
49+
# cloud.resource_id only lives in the private _incubating package; fall back
50+
# to the literal key the Agent Engine dashboard filters on if that path moves.
51+
CLOUD_RESOURCE_ID = "cloud.resource_id"
52+
4653
_GCP_LOG_NAME_ENV_VARIABLE_NAME = "GOOGLE_CLOUD_DEFAULT_LOG_NAME"
4754
_DEFAULT_LOG_NAME = "adk-otel"
4855

@@ -246,7 +253,7 @@ def get_gcp_resource(project_id: Optional[str] = None) -> Resource:
246253
),
247254
}
248255
if cloud_resource_id is not None:
249-
resource_attributes["cloud.resource.id"] = cloud_resource_id
256+
resource_attributes[CLOUD_RESOURCE_ID] = cloud_resource_id
250257

251258
if agent_engine_id:
252259
resource = Resource.create(attributes=resource_attributes).merge(

tests/unittests/telemetry/test_google_cloud.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,25 @@ def test_get_gcp_resource(
113113
)
114114

115115

116+
def test_get_gcp_resource_sets_standard_cloud_resource_id(
117+
monkeypatch: pytest.MonkeyPatch,
118+
):
119+
# Arrange.
120+
monkeypatch.setenv("GOOGLE_CLOUD_AGENT_ENGINE_ID", "1234567890")
121+
monkeypatch.setenv("GOOGLE_CLOUD_LOCATION", "us-central1")
122+
123+
# Act.
124+
otel_resource = get_gcp_resource("my-project")
125+
126+
# Assert.
127+
# The Agent Engine dashboard filters on the OTel-standard key.
128+
assert otel_resource.attributes.get("cloud.resource_id") == (
129+
"//aiplatform.googleapis.com/projects/my-project"
130+
"/locations/us-central1/reasoningEngines/1234567890"
131+
)
132+
assert "cloud.resource.id" not in otel_resource.attributes
133+
134+
116135
@mock.patch.object(mtls, "should_use_client_cert", autospec=True)
117136
def test_use_client_cert_effective_from_mtls(mock_should_use):
118137
mock_should_use.return_value = True

0 commit comments

Comments
 (0)