Skip to content
Merged
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
@@ -0,0 +1 @@
`opentelemetry-resourcedetector-gcp`: detect Cloud Run Jobs and `cloud.resource_id`
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
from opentelemetry.sdk.resources import Resource, ResourceDetector
from opentelemetry.util.types import AttributeValue

_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_FUNCTIONS_RESOURCE_ID = (
"//cloudfunctions.googleapis.com/projects/{project_id}/locations/{region}/functions/{name}"
)


class GoogleCloudResourceDetector(ResourceDetector):
def detect(self) -> Resource:
Expand All @@ -29,6 +35,8 @@ def detect(self) -> Resource:
return _cloud_functions_resource()
if _faas.on_cloud_run():
return _cloud_run_resource()
if _faas.on_cloud_run_job():
return _cloud_run_job_resource()
if _gae.on_app_engine():
return _gae_resource()
if _gce.on_gce():
Expand Down Expand Up @@ -67,29 +75,61 @@ def _gce_resource() -> Resource:


def _cloud_run_resource() -> Resource:
return _make_resource(
{
ResourceAttributes.CLOUD_PLATFORM_KEY: ResourceAttributes.GCP_CLOUD_RUN,
ResourceAttributes.FAAS_NAME: _faas.faas_name(),
ResourceAttributes.FAAS_VERSION: _faas.faas_version(),
ResourceAttributes.FAAS_INSTANCE: _faas.faas_instance(),
ResourceAttributes.CLOUD_REGION: _faas.faas_cloud_region(),
}
return _faas_resource(
platform=ResourceAttributes.GCP_CLOUD_RUN,
resource_id_template=_CLOUD_RUN_SERVICE_RESOURCE_ID,
name=_faas.faas_name(),
extra_attrs={ResourceAttributes.FAAS_VERSION: _faas.faas_version()},
)


def _cloud_functions_resource() -> Resource:
return _make_resource(
{
ResourceAttributes.CLOUD_PLATFORM_KEY: ResourceAttributes.GCP_CLOUD_FUNCTIONS,
ResourceAttributes.FAAS_NAME: _faas.faas_name(),
ResourceAttributes.FAAS_VERSION: _faas.faas_version(),
ResourceAttributes.FAAS_INSTANCE: _faas.faas_instance(),
ResourceAttributes.CLOUD_REGION: _faas.faas_cloud_region(),
}
return _faas_resource(
platform=ResourceAttributes.GCP_CLOUD_FUNCTIONS,
resource_id_template=_CLOUD_FUNCTIONS_RESOURCE_ID,
name=_faas.faas_name(),
extra_attrs={ResourceAttributes.FAAS_VERSION: _faas.faas_version()},
)


def _cloud_run_job_resource() -> Resource:
extra_attrs: dict[str, AttributeValue] = {}
if (execution := _faas.cloud_run_job_execution()) is not None:
extra_attrs[ResourceAttributes.GCP_CLOUD_RUN_JOB_EXECUTION] = execution
if (task_index := _faas.cloud_run_job_task_index()) is not None:
extra_attrs[ResourceAttributes.GCP_CLOUD_RUN_JOB_TASK_INDEX] = task_index
return _faas_resource(
platform=ResourceAttributes.GCP_CLOUD_RUN,
resource_id_template=_CLOUD_RUN_JOB_RESOURCE_ID,
name=_faas.cloud_run_job_name(),
extra_attrs=extra_attrs,
)


def _faas_resource(
platform: str,
resource_id_template: str,
name: str,
extra_attrs: Mapping[str, AttributeValue],
) -> Resource:
project_id = _metadata.get_metadata()["project"]["projectId"]
region = _faas.faas_cloud_region()
attrs: dict[str, AttributeValue] = {
ResourceAttributes.CLOUD_PLATFORM_KEY: platform,
ResourceAttributes.FAAS_NAME: name,
ResourceAttributes.FAAS_INSTANCE: _faas.faas_instance(),
ResourceAttributes.CLOUD_REGION: region,
**extra_attrs,
}
if project_id and region and name:
attrs[ResourceAttributes.CLOUD_RESOURCE_ID] = resource_id_template.format(
project_id=project_id,
region=region,
name=name,
)
return _make_resource(attrs, project_id=project_id)


def _gae_resource() -> Resource:
if _gae.on_app_engine_standard():
zone = _gae.standard_availability_zone()
Expand All @@ -115,11 +155,13 @@ def _gae_resource() -> Resource:
)


def _make_resource(attrs: Mapping[str, AttributeValue]) -> 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"]
return Resource(
{
ResourceAttributes.CLOUD_PROVIDER: "gcp",
ResourceAttributes.CLOUD_ACCOUNT_ID: _metadata.get_metadata()["project"]["projectId"],
ResourceAttributes.CLOUD_ACCOUNT_ID: project_id,
**attrs,
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ class ResourceAttributes:
CLOUD_PLATFORM_KEY = "cloud.platform"
CLOUD_PROVIDER = "cloud.provider"
CLOUD_REGION = "cloud.region"
CLOUD_RESOURCE_ID = "cloud.resource_id"
FAAS_INSTANCE = "faas.instance"
FAAS_NAME = "faas.name"
FAAS_VERSION = "faas.version"
GCP_APP_ENGINE = "gcp_app_engine"
GCP_CLOUD_FUNCTIONS = "gcp_cloud_functions"
GCP_CLOUD_RUN = "gcp_cloud_run"
GCP_CLOUD_RUN_JOB_EXECUTION = "gcp.cloud_run.job.execution"
GCP_CLOUD_RUN_JOB_TASK_INDEX = "gcp.cloud_run.job.task_index"
GCP_COMPUTE_ENGINE = "gcp_compute_engine"
GCP_KUBERNETES_ENGINE = "gcp_kubernetes_engine"
HOST_ID = "host.id"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@
# Implementation in this file copied from
# https://github.com/GoogleCloudPlatform/opentelemetry-operations-go/blob/v1.8.0/detectors/gcp/faas.go

import logging
import os

from opentelemetry.resourcedetector.gcp_resource_detector import _metadata

_CLOUD_RUN_CONFIG_ENV = "K_CONFIGURATION"
_CLOUD_FUNCTION_TARGET_ENV = "FUNCTION_TARGET"
_CLOUD_RUN_JOB_ENV = "CLOUD_RUN_JOB"
_CLOUD_RUN_EXECUTION_ENV = "CLOUD_RUN_EXECUTION"
_CLOUD_RUN_TASK_INDEX_ENV = "CLOUD_RUN_TASK_INDEX"
_FAAS_SERVICE_ENV = "K_SERVICE"
_FAAS_REVISION_ENV = "K_REVISION"

_logger = logging.getLogger(__name__)


def on_cloud_run() -> bool:
return _CLOUD_RUN_CONFIG_ENV in os.environ
Expand All @@ -22,6 +28,10 @@ def on_cloud_functions() -> bool:
return _CLOUD_FUNCTION_TARGET_ENV in os.environ


def on_cloud_run_job() -> bool:
return _CLOUD_RUN_JOB_ENV in os.environ


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

Expand All @@ -31,6 +41,33 @@ def faas_name() -> str:
return os.environ[_FAAS_SERVICE_ENV]


def cloud_run_job_name() -> str:
"""The name of the Cloud Run job.

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


def cloud_run_job_execution() -> str | None:
return os.environ.get(_CLOUD_RUN_EXECUTION_ENV)


def cloud_run_job_task_index() -> int | None:
raw_index = os.environ.get(_CLOUD_RUN_TASK_INDEX_ENV)
if raw_index is None:
return None
try:
return int(raw_index)
except ValueError:
_logger.warning(
"Can't parse GCP Cloud Run job task index (%s): %r",
_CLOUD_RUN_TASK_INDEX_ENV,
raw_index,
)
return None


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 @@ -5,6 +5,7 @@
'cloud.platform': 'gcp_cloud_functions',
'cloud.provider': 'gcp',
'cloud.region': 'us-east4',
'cloud.resource_id': '//cloudfunctions.googleapis.com/projects/fakeProject/locations/us-east4/functions/fake-service',
'faas.instance': '0087244a',
'faas.name': 'fake-service',
'faas.version': 'fake-revision',
Expand All @@ -16,11 +17,25 @@
'cloud.platform': 'gcp_cloud_run',
'cloud.provider': 'gcp',
'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',
})
# ---
# name: test_detects_cloud_run_job
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/jobs/fake-job',
'faas.instance': '0087244a',
'faas.name': 'fake-job',
'gcp.cloud_run.job.execution': 'fake-job-exec',
'gcp.cloud_run.job.task_index': 0,
})
# ---
# name: test_detects_empty_as_fallback
dict({
})
Expand Down
31 changes: 31 additions & 0 deletions resource/opentelemetry-resourcedetector-gcp/tests/test_faas.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,42 @@ def test_detects_not_on_cloud_functions() -> None:
assert not _faas.on_cloud_functions()


def test_detects_on_cloud_run_job(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("CLOUD_RUN_JOB", "fake-job")
assert _faas.on_cloud_run_job()


def test_detects_not_on_cloud_run_job() -> None:
assert not _faas.on_cloud_run_job()


def test_detects_faas_name(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("K_SERVICE", "fake-service")
assert _faas.faas_name() == "fake-service"


def test_detects_cloud_run_job_name(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("CLOUD_RUN_JOB", "fake-job")
assert _faas.cloud_run_job_name() == "fake-job"


def test_detects_cloud_run_job_execution(monkeypatch: pytest.MonkeyPatch) -> None:
assert _faas.cloud_run_job_execution() is None
monkeypatch.setenv("CLOUD_RUN_EXECUTION", "fake-execution")
assert _faas.cloud_run_job_execution() == "fake-execution"


def test_detects_cloud_run_job_task_index(monkeypatch: pytest.MonkeyPatch) -> None:
assert _faas.cloud_run_job_task_index() is None
monkeypatch.setenv("CLOUD_RUN_TASK_INDEX", "2")
assert _faas.cloud_run_job_task_index() == 2


def test_cloud_run_job_task_index_malformed(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("CLOUD_RUN_TASK_INDEX", "not-an-int")
assert _faas.cloud_run_job_task_index() is None


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 @@ -148,6 +148,114 @@ def test_detects_cloud_functions(
assert dict(GoogleCloudResourceDetector().detect().attributes) == snapshot


def test_detects_cloud_run_job(
snapshot,
fake_metadata: _metadata.Metadata,
monkeypatch: pytest.MonkeyPatch,
):
monkeypatch.setenv("CLOUD_RUN_JOB", "fake-job")
monkeypatch.setenv("CLOUD_RUN_EXECUTION", "fake-job-exec")
monkeypatch.setenv("CLOUD_RUN_TASK_INDEX", "0")
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_job_without_optional_env_vars(
fake_metadata: _metadata.Metadata,
monkeypatch: pytest.MonkeyPatch,
):
monkeypatch.setenv("CLOUD_RUN_JOB", "fake-job")
monkeypatch.setenv("CLOUD_RUN_TASK_INDEX", "not-an-int")
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/jobs/fake-job",
"faas.name": "fake-job",
"faas.instance": "0087244a",
}


@pytest.mark.parametrize(
("env_vars", "project_id", "region"),
(
pytest.param(
{"K_CONFIGURATION": "fake-configuration", "K_SERVICE": "fake-service", "K_REVISION": "fake-revision"},
"",
"projects/233510669999/regions/us-east4",
id="cloud-run-empty-project",
),
pytest.param(
{"K_CONFIGURATION": "fake-configuration", "K_SERVICE": "fake-service", "K_REVISION": "fake-revision"},
"fakeProject",
"projects/233510669999/regions/",
id="cloud-run-empty-region",
),
pytest.param(
{"K_CONFIGURATION": "fake-configuration", "K_SERVICE": "", "K_REVISION": "fake-revision"},
"fakeProject",
"projects/233510669999/regions/us-east4",
id="cloud-run-empty-name",
),
pytest.param(
{"FUNCTION_TARGET": "fake-fn", "K_SERVICE": "fake-service", "K_REVISION": "fake-revision"},
"fakeProject",
"projects/233510669999/regions/",
id="cloud-functions-empty-region",
),
pytest.param(
{"CLOUD_RUN_JOB": ""},
"fakeProject",
"projects/233510669999/regions/us-east4",
id="cloud-run-job-empty-name",
),
),
)
def test_serverless_omits_cloud_resource_id_when_component_missing(
env_vars: dict[str, str],
project_id: str,
region: str,
fake_metadata: _metadata.Metadata,
monkeypatch: pytest.MonkeyPatch,
):
for key, val in env_vars.items():
monkeypatch.setenv(key, val)
fake_metadata.update(
{
"project": {"projectId": project_id},
"instance": {
"id": "0087244a",
"region": region,
},
}
)

attrs = dict(GoogleCloudResourceDetector().detect().attributes)
assert attrs["cloud.provider"] == "gcp"
assert "cloud.resource_id" not in attrs


def test_detects_gae_standard(
snapshot,
fake_metadata: _metadata.Metadata,
Expand Down
Loading