Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1 +1 @@
`opentelemetry-resourcedetector-gcp`: detect Cloud Run Jobs and `cloud.resource_id`
`opentelemetry-resourcedetector-gcp`: detect Cloud Run Jobs and populate `cloud.resource_id` for Cloud Run services/jobs and Cloud Functions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`opentelemetry-resourcedetector-gcp`: detect Cloud Run worker pools (`CLOUD_RUN_WORKER_POOL`, `CLOUD_RUN_REVISION`) and Cloud Run instances (`CLOUD_RUN_INSTANCE`) and populate `cloud.resource_id`
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[BREAKING] `opentelemetry-resourcedetector-gcp`: detect Cloud Run functions and Cloud Functions (2nd gen) as `cloud.platform=gcp_cloud_run` (previously `gcp_cloud_functions`) with `cloud.resource_id=//run.googleapis.com/.../services/{name}` by checking `K_CONFIGURATION` before `FUNCTION_TARGET`
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

_CLOUD_RUN_SERVICE_RESOURCE_ID = "//run.googleapis.com/projects/{project_id}/locations/{region}/services/{name}"
_CLOUD_RUN_JOB_RESOURCE_ID = "//run.googleapis.com/projects/{project_id}/locations/{region}/jobs/{name}"
_CLOUD_RUN_WORKER_POOL_RESOURCE_ID = "//run.googleapis.com/projects/{project_id}/locations/{region}/workerPools/{name}"
_CLOUD_RUN_INSTANCE_RESOURCE_ID = "//run.googleapis.com/projects/{project_id}/locations/{region}/instances/{name}"
_CLOUD_FUNCTIONS_RESOURCE_ID = (
"//cloudfunctions.googleapis.com/projects/{project_id}/locations/{region}/functions/{name}"
)
Expand All @@ -31,12 +33,16 @@ def detect(self) -> Resource:

if _gke.on_gke():
return _gke_resource()
if _faas.on_cloud_functions():
return _cloud_functions_resource()
if _faas.on_cloud_run():
return _cloud_run_resource()
if _faas.on_cloud_functions():
return _cloud_functions_resource()
if _faas.on_cloud_run_job():
return _cloud_run_job_resource()
if _faas.on_cloud_run_worker_pool():
return _cloud_run_worker_pool_resource()
if _faas.on_cloud_run_instance():
return _cloud_run_instance_resource()
if _gae.on_app_engine():
return _gae_resource()
if _gce.on_gce():
Expand Down Expand Up @@ -106,6 +112,27 @@ def _cloud_run_job_resource() -> Resource:
)


def _cloud_run_worker_pool_resource() -> Resource:
extra_attrs: dict[str, AttributeValue] = {}
if (revision := _faas.cloud_run_worker_pool_revision()) is not None:
extra_attrs[ResourceAttributes.FAAS_VERSION] = revision
return _faas_resource(
platform=ResourceAttributes.GCP_CLOUD_RUN,
resource_id_template=_CLOUD_RUN_WORKER_POOL_RESOURCE_ID,
name=_faas.cloud_run_worker_pool_name(),
extra_attrs=extra_attrs,
)


def _cloud_run_instance_resource() -> Resource:
return _faas_resource(
platform=ResourceAttributes.GCP_CLOUD_RUN,
resource_id_template=_CLOUD_RUN_INSTANCE_RESOURCE_ID,
name=_faas.cloud_run_instance_name(),
extra_attrs={},
)


def _faas_resource(
platform: str,
resource_id_template: str,
Expand All @@ -127,7 +154,7 @@ def _faas_resource(
region=region,
name=name,
)
return _make_resource(attrs, project_id=project_id)
return _make_resource(attrs)


def _gae_resource() -> Resource:
Expand Down Expand Up @@ -155,13 +182,11 @@ def _gae_resource() -> Resource:
)


def _make_resource(attrs: Mapping[str, AttributeValue], project_id: str | None = None) -> Resource:
if project_id is None:
project_id = _metadata.get_metadata()["project"]["projectId"]
def _make_resource(attrs: Mapping[str, AttributeValue]) -> Resource:
return Resource(
{
ResourceAttributes.CLOUD_PROVIDER: "gcp",
ResourceAttributes.CLOUD_ACCOUNT_ID: project_id,
ResourceAttributes.CLOUD_ACCOUNT_ID: _metadata.get_metadata()["project"]["projectId"],
**attrs,
}
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Copyright The OpenTelemetry Authors
# SPDX-License-Identifier: Apache-2.0

# Implementation in this file copied from
# https://github.com/GoogleCloudPlatform/opentelemetry-operations-go/blob/v1.8.0/detectors/gcp/faas.go
# Implementation in this file adapted from
# https://github.com/open-telemetry/opentelemetry-go-contrib/blob/main/detectors/gcp/internal/faas.go

import logging
import os
Expand All @@ -14,6 +14,9 @@
_CLOUD_RUN_JOB_ENV = "CLOUD_RUN_JOB"
_CLOUD_RUN_EXECUTION_ENV = "CLOUD_RUN_EXECUTION"
_CLOUD_RUN_TASK_INDEX_ENV = "CLOUD_RUN_TASK_INDEX"
_CLOUD_RUN_WORKER_POOL_ENV = "CLOUD_RUN_WORKER_POOL"
_CLOUD_RUN_REVISION_ENV = "CLOUD_RUN_REVISION"
_CLOUD_RUN_INSTANCE_ENV = "CLOUD_RUN_INSTANCE"
_FAAS_SERVICE_ENV = "K_SERVICE"
_FAAS_REVISION_ENV = "K_REVISION"

Expand All @@ -32,6 +35,14 @@ def on_cloud_run_job() -> bool:
return _CLOUD_RUN_JOB_ENV in os.environ


def on_cloud_run_worker_pool() -> bool:
return _CLOUD_RUN_WORKER_POOL_ENV in os.environ


def on_cloud_run_instance() -> bool:
return _CLOUD_RUN_INSTANCE_ENV in os.environ


def faas_name() -> str:
"""The name of the Cloud Run or Cloud Function.

Expand Down Expand Up @@ -68,6 +79,26 @@ def cloud_run_job_task_index() -> int | None:
return None


def cloud_run_worker_pool_name() -> str:
"""The name of the Cloud Run worker pool.

Check that on_cloud_run_worker_pool() is true before calling this, or it may throw exceptions.
"""
return os.environ[_CLOUD_RUN_WORKER_POOL_ENV]


def cloud_run_worker_pool_revision() -> str | None:
return os.environ.get(_CLOUD_RUN_REVISION_ENV)


def cloud_run_instance_name() -> str:
"""The name of the Cloud Run instance.

Check that on_cloud_run_instance() is true before calling this, or it may throw exceptions.
"""
return os.environ[_CLOUD_RUN_INSTANCE_ENV]


def faas_version() -> str:
"""The version/revision of the Cloud Run or Cloud Function.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@
'faas.version': 'fake-revision',
})
# ---
# name: test_detects_cloud_run_instance
dict({
'cloud.account.id': 'fakeProject',
'cloud.platform': 'gcp_cloud_run',
'cloud.provider': 'gcp',
'cloud.region': 'us-east4',
'cloud.resource_id': '//run.googleapis.com/projects/fakeProject/locations/us-east4/instances/fake-instance',
'faas.instance': '0087244a',
'faas.name': 'fake-instance',
})
# ---
# name: test_detects_cloud_run_job
dict({
'cloud.account.id': 'fakeProject',
Expand All @@ -36,6 +47,18 @@
'gcp.cloud_run.job.task_index': 0,
})
# ---
# name: test_detects_cloud_run_worker_pool
dict({
'cloud.account.id': 'fakeProject',
'cloud.platform': 'gcp_cloud_run',
'cloud.provider': 'gcp',
'cloud.region': 'us-east4',
'cloud.resource_id': '//run.googleapis.com/projects/fakeProject/locations/us-east4/workerPools/fake-worker-pool',
'faas.instance': '0087244a',
'faas.name': 'fake-worker-pool',
'faas.version': 'fake-worker-pool-00001',
})
# ---
# name: test_detects_empty_as_fallback
dict({
})
Expand Down
34 changes: 34 additions & 0 deletions resource/opentelemetry-resourcedetector-gcp/tests/test_faas.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,24 @@ def test_detects_not_on_cloud_run_job() -> None:
assert not _faas.on_cloud_run_job()


def test_detects_on_cloud_run_worker_pool(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("CLOUD_RUN_WORKER_POOL", "fake-worker-pool")
assert _faas.on_cloud_run_worker_pool()


def test_detects_not_on_cloud_run_worker_pool() -> None:
assert not _faas.on_cloud_run_worker_pool()


def test_detects_on_cloud_run_instance(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("CLOUD_RUN_INSTANCE", "fake-instance")
assert _faas.on_cloud_run_instance()


def test_detects_not_on_cloud_run_instance() -> None:
assert not _faas.on_cloud_run_instance()


def test_detects_faas_name(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("K_SERVICE", "fake-service")
assert _faas.faas_name() == "fake-service"
Expand Down Expand Up @@ -69,6 +87,22 @@ def test_cloud_run_job_task_index_malformed(monkeypatch: pytest.MonkeyPatch) ->
assert _faas.cloud_run_job_task_index() is None


def test_detects_cloud_run_worker_pool_name(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("CLOUD_RUN_WORKER_POOL", "fake-worker-pool")
assert _faas.cloud_run_worker_pool_name() == "fake-worker-pool"


def test_detects_cloud_run_worker_pool_revision(monkeypatch: pytest.MonkeyPatch) -> None:
assert _faas.cloud_run_worker_pool_revision() is None
monkeypatch.setenv("CLOUD_RUN_REVISION", "fake-worker-pool-00001")
assert _faas.cloud_run_worker_pool_revision() == "fake-worker-pool-00001"


def test_detects_cloud_run_instance_name(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("CLOUD_RUN_INSTANCE", "fake-instance")
assert _faas.cloud_run_instance_name() == "fake-instance"


def test_detects_faas_version(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("K_REVISION", "fake-revision")
assert _faas.faas_version() == "fake-revision"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,10 @@ def test_detects_cloud_functions(
fake_metadata: _metadata.Metadata,
monkeypatch: pytest.MonkeyPatch,
):
# Cloud Functions (1st gen) sets FUNCTION_TARGET, K_SERVICE, and K_REVISION,
# without K_CONFIGURATION (which is only set on Cloud Run-backed services).
monkeypatch.setenv("FUNCTION_TARGET", "fake-function-target")
# Note all K_* environment variables are set since Cloud Functions executes within Cloud
# Run. This tests that the detector can differentiate between them
monkeypatch.setenv("K_CONFIGURATION", "fake-configuration")
monkeypatch.setenv("FUNCTION_SIGNATURE_TYPE", "http")
monkeypatch.setenv("K_SERVICE", "fake-service")
monkeypatch.setenv("K_REVISION", "fake-revision")
fake_metadata.update(
Expand All @@ -148,6 +148,41 @@ def test_detects_cloud_functions(
assert dict(GoogleCloudResourceDetector().detect().attributes) == snapshot


def test_detects_cloud_run_function(
fake_metadata: _metadata.Metadata,
monkeypatch: pytest.MonkeyPatch,
):
# Cloud Run functions (including functions deployed via `gcloud run deploy --function`
# and Cloud Functions 2nd gen) set both K_CONFIGURATION and FUNCTION_TARGET/FUNCTION_SIGNATURE_TYPE.
# They run as Cloud Run services and should detect as gcp_cloud_run with a run.googleapis.com resource ID.
monkeypatch.setenv("FUNCTION_TARGET", "fake-function-target")
monkeypatch.setenv("FUNCTION_SIGNATURE_TYPE", "http")
monkeypatch.setenv("K_CONFIGURATION", "fake-configuration")
monkeypatch.setenv("K_SERVICE", "fake-service")
monkeypatch.setenv("K_REVISION", "fake-revision")
fake_metadata.update(
{
"project": {"projectId": "fakeProject"},
"instance": {
"id": "0087244a",
"region": "projects/233510669999/regions/us-east4",
},
}
)

attrs = dict(GoogleCloudResourceDetector().detect().attributes)
assert attrs == {
"cloud.provider": "gcp",
"cloud.account.id": "fakeProject",
"cloud.platform": "gcp_cloud_run",
"cloud.region": "us-east4",
"cloud.resource_id": "//run.googleapis.com/projects/fakeProject/locations/us-east4/services/fake-service",
"faas.instance": "0087244a",
"faas.name": "fake-service",
"faas.version": "fake-revision",
}


def test_detects_cloud_run_job(
snapshot,
fake_metadata: _metadata.Metadata,
Expand Down Expand Up @@ -197,6 +232,72 @@ def test_detects_cloud_run_job_without_optional_env_vars(
}


def test_detects_cloud_run_worker_pool(
snapshot,
fake_metadata: _metadata.Metadata,
monkeypatch: pytest.MonkeyPatch,
):
monkeypatch.setenv("CLOUD_RUN_WORKER_POOL", "fake-worker-pool")
monkeypatch.setenv("CLOUD_RUN_REVISION", "fake-worker-pool-00001")
fake_metadata.update(
{
"project": {"projectId": "fakeProject"},
"instance": {
"id": "0087244a",
"region": "projects/233510669999/regions/us-east4",
},
}
)

assert dict(GoogleCloudResourceDetector().detect().attributes) == snapshot


def test_detects_cloud_run_worker_pool_without_optional_env_vars(
fake_metadata: _metadata.Metadata,
monkeypatch: pytest.MonkeyPatch,
):
monkeypatch.setenv("CLOUD_RUN_WORKER_POOL", "fake-worker-pool")
fake_metadata.update(
{
"project": {"projectId": "fakeProject"},
"instance": {
"id": "0087244a",
"region": "projects/233510669999/regions/us-east4",
},
}
)

attrs = dict(GoogleCloudResourceDetector().detect().attributes)
assert attrs == {
"cloud.provider": "gcp",
"cloud.account.id": "fakeProject",
"cloud.platform": "gcp_cloud_run",
"cloud.region": "us-east4",
"cloud.resource_id": "//run.googleapis.com/projects/fakeProject/locations/us-east4/workerPools/fake-worker-pool",
"faas.name": "fake-worker-pool",
"faas.instance": "0087244a",
}


def test_detects_cloud_run_instance(
snapshot,
fake_metadata: _metadata.Metadata,
monkeypatch: pytest.MonkeyPatch,
):
monkeypatch.setenv("CLOUD_RUN_INSTANCE", "fake-instance")
fake_metadata.update(
{
"project": {"projectId": "fakeProject"},
"instance": {
"id": "0087244a",
"region": "projects/233510669999/regions/us-east4",
},
}
)

assert dict(GoogleCloudResourceDetector().detect().attributes) == snapshot


@pytest.mark.parametrize(
("env_vars", "project_id", "region"),
(
Expand Down Expand Up @@ -230,6 +331,18 @@ def test_detects_cloud_run_job_without_optional_env_vars(
"projects/233510669999/regions/us-east4",
id="cloud-run-job-empty-name",
),
pytest.param(
{"CLOUD_RUN_WORKER_POOL": ""},
"fakeProject",
"projects/233510669999/regions/us-east4",
id="cloud-run-worker-pool-empty-name",
),
pytest.param(
{"CLOUD_RUN_INSTANCE": ""},
"fakeProject",
"projects/233510669999/regions/us-east4",
id="cloud-run-instance-empty-name",
),
),
)
def test_serverless_omits_cloud_resource_id_when_component_missing(
Expand Down
Loading