-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Cloud Run and Cloud Functions faas resource detection
- Loading branch information
Showing
7 changed files
with
239 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
...ry-resourcedetector-gcp/src/opentelemetry/resourcedetector/gcp_resource_detector/_faas.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Copyright 2024 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 | ||
# | ||
# https://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. | ||
|
||
# Implementation in this file copied from | ||
# https://github.com/GoogleCloudPlatform/opentelemetry-operations-go/blob/v1.8.0/detectors/gcp/faas.go | ||
|
||
import os | ||
|
||
from opentelemetry.resourcedetector.gcp_resource_detector import _metadata | ||
|
||
_CLOUD_RUN_CONFIG_ENV = "K_CONFIGURATION" | ||
_CLOUD_FUNCTION_TARGET_ENV = "FUNCTION_TARGET" | ||
_FAAS_SERVICE_ENV = "K_SERVICE" | ||
_FAAS_REVISION_ENV = "K_REVISION" | ||
|
||
|
||
def on_cloud_run() -> bool: | ||
return _CLOUD_RUN_CONFIG_ENV in os.environ | ||
|
||
|
||
def on_cloud_functions() -> bool: | ||
return _CLOUD_FUNCTION_TARGET_ENV in os.environ | ||
|
||
|
||
def faas_name() -> str: | ||
"""The name of the Cloud Run or Cloud Function. | ||
Check that on_cloud_run() or on_cloud_functions() is true before calling this, or it may | ||
throw exceptions. | ||
""" | ||
return os.environ[_FAAS_SERVICE_ENV] | ||
|
||
|
||
def faas_version() -> str: | ||
"""The version/revision of the Cloud Run or Cloud Function. | ||
Check that on_cloud_run() or on_cloud_functions() is true before calling this, or it may | ||
throw exceptions. | ||
""" | ||
return os.environ[_FAAS_REVISION_ENV] | ||
|
||
|
||
def faas_instance() -> str: | ||
return str(_metadata.get_metadata()["instance"]["id"]) | ||
|
||
|
||
def faas_cloud_region() -> str: | ||
region = _metadata.get_metadata()["instance"]["region"] | ||
return region[region.rfind("/") + 1 :] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,6 +45,7 @@ class Instance(TypedDict): | |
id: Union[int, str] | ||
machineType: str | ||
name: str | ||
region: str | ||
zone: str | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# Copyright 2024 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 | ||
# | ||
# https://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.mock import MagicMock | ||
|
||
import pytest | ||
from opentelemetry.resourcedetector.gcp_resource_detector import _faas | ||
|
||
|
||
# Reset stuff before every test | ||
# pylint: disable=unused-argument | ||
@pytest.fixture(autouse=True) | ||
def autouse(fake_get_metadata): | ||
pass | ||
|
||
|
||
def test_detects_on_cloud_run(monkeypatch: pytest.MonkeyPatch) -> None: | ||
monkeypatch.setenv("K_CONFIGURATION", "fake-configuration") | ||
assert _faas.on_cloud_run() | ||
|
||
|
||
def test_detects_not_on_cloud_run() -> None: | ||
assert not _faas.on_cloud_run() | ||
|
||
|
||
def test_detects_on_cloud_functions(monkeypatch: pytest.MonkeyPatch) -> None: | ||
monkeypatch.setenv("FUNCTION_TARGET", "fake-function-target") | ||
assert _faas.on_cloud_functions() | ||
|
||
|
||
def test_detects_not_on_cloud_functions() -> None: | ||
assert not _faas.on_cloud_functions() | ||
|
||
|
||
def test_detects_faas_name(monkeypatch: pytest.MonkeyPatch) -> None: | ||
monkeypatch.setenv("K_SERVICE", "fake-service") | ||
assert _faas.faas_name() == "fake-service" | ||
|
||
|
||
def test_detects_faas_version(monkeypatch: pytest.MonkeyPatch) -> None: | ||
monkeypatch.setenv("K_REVISION", "fake-revision") | ||
assert _faas.faas_version() == "fake-revision" | ||
|
||
|
||
def test_detects_faas_instance(fake_get_metadata: MagicMock) -> None: | ||
fake_get_metadata.return_value = {"instance": {"id": "0087244a"}} | ||
assert _faas.faas_instance() == "0087244a" | ||
|
||
|
||
def test_detects_faas_region(fake_get_metadata: MagicMock) -> None: | ||
fake_get_metadata.return_value = { | ||
"instance": {"region": "projects/233510669999/regions/us-east4"} | ||
} | ||
assert _faas.faas_cloud_region() == "us-east4" |