-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement GAE resource detection (#351)
- Loading branch information
Showing
6 changed files
with
282 additions
and
0 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
88 changes: 88 additions & 0 deletions
88
...try-resourcedetector-gcp/src/opentelemetry/resourcedetector/gcp_resource_detector/_gae.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,88 @@ | ||
# 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/app_engine.go | ||
|
||
import os | ||
|
||
from opentelemetry.resourcedetector.gcp_resource_detector import ( | ||
_faas, | ||
_gce, | ||
_metadata, | ||
) | ||
|
||
_GAE_SERVICE_ENV = "GAE_SERVICE" | ||
_GAE_VERSION_ENV = "GAE_VERSION" | ||
_GAE_INSTANCE_ENV = "GAE_INSTANCE" | ||
_GAE_ENV = "GAE_ENV" | ||
_GAE_STANDARD = "standard" | ||
|
||
|
||
def on_app_engine_standard() -> bool: | ||
return os.environ.get(_GAE_ENV) == _GAE_STANDARD | ||
|
||
|
||
def on_app_engine() -> bool: | ||
return _GAE_SERVICE_ENV in os.environ | ||
|
||
|
||
def service_name() -> str: | ||
"""The service name of the app engine service. | ||
Check that ``on_app_engine()`` is true before calling this, or it may throw exceptions. | ||
""" | ||
return os.environ[_GAE_SERVICE_ENV] | ||
|
||
|
||
def service_version() -> str: | ||
"""The service version of the app engine service. | ||
Check that ``on_app_engine()`` is true before calling this, or it may throw exceptions. | ||
""" | ||
return os.environ[_GAE_VERSION_ENV] | ||
|
||
|
||
def service_instance() -> str: | ||
"""The service instance of the app engine service. | ||
Check that ``on_app_engine()`` is true before calling this, or it may throw exceptions. | ||
""" | ||
return os.environ[_GAE_INSTANCE_ENV] | ||
|
||
|
||
def flex_availability_zone_and_region() -> _gce.ZoneAndRegion: | ||
"""The zone and region in which this program is running. | ||
Check that ``on_app_engine()`` is true before calling this, or it may throw exceptions. | ||
""" | ||
return _gce.availability_zone_and_region() | ||
|
||
|
||
def standard_availability_zone() -> str: | ||
"""The zone the app engine service is running in. | ||
Check that ``on_app_engine_standard()`` is true before calling this, or it may throw exceptions. | ||
""" | ||
zone = _metadata.get_metadata()["instance"]["zone"] | ||
# zone is of the form "projects/233510669999/zones/us15" | ||
return zone[zone.rfind("/") + 1 :] | ||
|
||
|
||
def standard_cloud_region() -> str: | ||
"""The region the app engine service is running in. | ||
Check that ``on_app_engine_standard()`` is true before calling this, or it may throw exceptions. | ||
""" | ||
return _faas.faas_cloud_region() |
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,89 @@ | ||
# 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 _gae | ||
|
||
|
||
# Reset stuff before every test | ||
# pylint: disable=unused-argument | ||
@pytest.fixture(autouse=True) | ||
def autouse(fake_get_metadata): | ||
pass | ||
|
||
|
||
def test_detects_on_gae(monkeypatch: pytest.MonkeyPatch) -> None: | ||
monkeypatch.setenv("GAE_SERVICE", "fake-service") | ||
assert _gae.on_app_engine() | ||
|
||
|
||
def test_detects_not_on_gae() -> None: | ||
assert not _gae.on_app_engine() | ||
|
||
|
||
def test_detects_on_gae_standard(monkeypatch: pytest.MonkeyPatch) -> None: | ||
monkeypatch.setenv("GAE_ENV", "standard") | ||
assert _gae.on_app_engine_standard() | ||
|
||
|
||
def test_detects_not_on_gae_standard(monkeypatch: pytest.MonkeyPatch) -> None: | ||
monkeypatch.setenv("GAE_SERVICE", "fake-service") | ||
assert _gae.on_app_engine() | ||
assert not _gae.on_app_engine_standard() | ||
|
||
|
||
def test_detects_gae_service_name(monkeypatch: pytest.MonkeyPatch) -> None: | ||
monkeypatch.setenv("GAE_SERVICE", "fake-service") | ||
assert _gae.service_name() == "fake-service" | ||
|
||
|
||
def test_detects_gae_service_version(monkeypatch: pytest.MonkeyPatch) -> None: | ||
monkeypatch.setenv("GAE_VERSION", "fake-version") | ||
assert _gae.service_version() == "fake-version" | ||
|
||
|
||
def test_detects_gae_service_instance(monkeypatch: pytest.MonkeyPatch) -> None: | ||
monkeypatch.setenv("GAE_INSTANCE", "fake-instance") | ||
assert _gae.service_instance() == "fake-instance" | ||
|
||
|
||
def test_detects_gae_flex_zone_and_region( | ||
fake_get_metadata: MagicMock, | ||
) -> None: | ||
fake_get_metadata.return_value = { | ||
"instance": {"zone": "projects/233510669999/zones/us-east4-b"} | ||
} | ||
zone_and_region = _gae.flex_availability_zone_and_region() | ||
assert zone_and_region.zone == "us-east4-b" | ||
assert zone_and_region.region == "us-east4" | ||
|
||
|
||
def test_gae_standard_zone( | ||
fake_get_metadata: MagicMock, | ||
) -> None: | ||
fake_get_metadata.return_value = { | ||
"instance": {"zone": "projects/233510669999/zones/us15"} | ||
} | ||
assert _gae.standard_availability_zone() == "us15" | ||
|
||
|
||
def test_gae_standard_region( | ||
fake_get_metadata: MagicMock, | ||
) -> None: | ||
fake_get_metadata.return_value = { | ||
"instance": {"region": "projects/233510669999/regions/us-east4"} | ||
} | ||
assert _gae.standard_cloud_region() == "us-east4" |