Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: detect monitored resources on all GCP environments (#200)
- Loading branch information
1 parent
22f836a
commit 4eda681
Showing
8 changed files
with
493 additions
and
42 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
195 changes: 195 additions & 0 deletions
195
google/cloud/logging_v2/handlers/_monitored_resources.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,195 @@ | ||
# Copyright 2021 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 | ||
# | ||
# http://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. | ||
|
||
import os | ||
|
||
from google.cloud.logging_v2.resource import Resource | ||
from google.cloud.logging_v2._helpers import retrieve_metadata_server | ||
|
||
_GAE_SERVICE_ENV = "GAE_SERVICE" | ||
_GAE_VERSION_ENV = "GAE_VERSION" | ||
_GAE_INSTANCE_ENV = "GAE_INSTANCE" | ||
_GAE_ENV_VARS = [_GAE_SERVICE_ENV, _GAE_VERSION_ENV, _GAE_INSTANCE_ENV] | ||
"""Environment variables set in App Engine environment.""" | ||
|
||
_CLOUD_RUN_SERVICE_ID = "K_SERVICE" | ||
_CLOUD_RUN_REVISION_ID = "K_REVISION" | ||
_CLOUD_RUN_CONFIGURATION_ID = "K_CONFIGURATION" | ||
_CLOUD_RUN_ENV_VARS = [ | ||
_CLOUD_RUN_SERVICE_ID, | ||
_CLOUD_RUN_REVISION_ID, | ||
_CLOUD_RUN_CONFIGURATION_ID, | ||
] | ||
"""Environment variables set in Cloud Run environment.""" | ||
|
||
_FUNCTION_TARGET = "FUNCTION_TARGET" | ||
_FUNCTION_SIGNATURE = "FUNCTION_SIGNATURE_TYPE" | ||
_FUNCTION_NAME = "FUNCTION_NAME" | ||
_FUNCTION_REGION = "FUNCTION_REGION" | ||
_FUNCTION_ENTRY = "ENTRY_POINT" | ||
_FUNCTION_ENV_VARS = [_FUNCTION_TARGET, _FUNCTION_SIGNATURE, _CLOUD_RUN_SERVICE_ID] | ||
_LEGACY_FUNCTION_ENV_VARS = [_FUNCTION_NAME, _FUNCTION_REGION, _FUNCTION_ENTRY] | ||
"""Environment variables set in Cloud Functions environments.""" | ||
|
||
|
||
_REGION_ID = "instance/region" | ||
_ZONE_ID = "instance/zone" | ||
_GCE_INSTANCE_ID = "instance/id" | ||
"""Attribute in metadata server for compute region and instance.""" | ||
|
||
_GKE_CLUSTER_NAME = "instance/attributes/cluster-name" | ||
"""Attribute in metadata server when in GKE environment.""" | ||
|
||
|
||
def _create_functions_resource(project): | ||
"""Create a standardized Cloud Functions resource. | ||
Args: | ||
project (str): The project ID to pass on to the resource | ||
Returns: | ||
google.cloud.logging.Resource | ||
""" | ||
region = retrieve_metadata_server(_REGION_ID) | ||
if _FUNCTION_NAME in os.environ: | ||
function_name = os.environ.get(_FUNCTION_NAME) | ||
elif _CLOUD_RUN_SERVICE_ID in os.environ: | ||
function_name = os.environ.get(_CLOUD_RUN_SERVICE_ID) | ||
else: | ||
function_name = "" | ||
resource = Resource( | ||
type="cloud_function", | ||
labels={ | ||
"project_id": project, | ||
"function_name": function_name, | ||
"region": region if region else "", | ||
}, | ||
) | ||
return resource | ||
|
||
|
||
def _create_kubernetes_resource(project): | ||
"""Create a standardized Kubernetes resource. | ||
Args: | ||
project (str): The project ID to pass on to the resource | ||
Returns: | ||
google.cloud.logging.Resource | ||
""" | ||
zone = retrieve_metadata_server(_ZONE_ID) | ||
cluster_name = retrieve_metadata_server(_GKE_CLUSTER_NAME) | ||
|
||
resource = Resource( | ||
type="k8s_container", | ||
labels={ | ||
"project_id": project, | ||
"location": zone if zone else "", | ||
"cluster_name": cluster_name if cluster_name else "", | ||
}, | ||
) | ||
return resource | ||
|
||
|
||
def _create_compute_resource(project): | ||
"""Create a standardized Compute Engine resource. | ||
Args: | ||
project (str): The project ID to pass on to the resource | ||
Returns: | ||
google.cloud.logging.Resource | ||
""" | ||
instance = retrieve_metadata_server(_GCE_INSTANCE_ID) | ||
zone = retrieve_metadata_server(_ZONE_ID) | ||
resource = Resource( | ||
type="gce_instance", | ||
labels={ | ||
"project_id": project, | ||
"instance_id": instance if instance else "", | ||
"zone": zone if zone else "", | ||
}, | ||
) | ||
return resource | ||
|
||
|
||
def _create_cloud_run_resource(project): | ||
"""Create a standardized Cloud Run resource. | ||
Args: | ||
project (str): The project ID to pass on to the resource | ||
Returns: | ||
google.cloud.logging.Resource | ||
""" | ||
region = retrieve_metadata_server(_REGION_ID) | ||
resource = Resource( | ||
type="cloud_run_revision", | ||
labels={ | ||
"project_id": project, | ||
"service_name": os.environ.get(_CLOUD_RUN_SERVICE_ID, ""), | ||
"revision_name": os.environ.get(_CLOUD_RUN_REVISION_ID, ""), | ||
"location": region if region else "", | ||
"configuration_name": os.environ.get(_CLOUD_RUN_CONFIGURATION_ID, ""), | ||
}, | ||
) | ||
return resource | ||
|
||
|
||
def _create_app_engine_resource(project): | ||
"""Create a standardized App Engine resource. | ||
Args: | ||
project (str): The project ID to pass on to the resource | ||
Returns: | ||
google.cloud.logging.Resource | ||
""" | ||
zone = retrieve_metadata_server(_ZONE_ID) | ||
resource = Resource( | ||
type="gae_app", | ||
labels={ | ||
"project_id": project, | ||
"module_id": os.environ.get(_GAE_SERVICE_ENV, ""), | ||
"version_id": os.environ.get(_GAE_VERSION_ENV, ""), | ||
"zone": zone if zone else "", | ||
}, | ||
) | ||
return resource | ||
|
||
|
||
def _create_global_resource(project): | ||
return Resource(type="global", labels={"project_id": project}) | ||
|
||
|
||
def detect_resource(project): | ||
"""Return the default monitored resource based on the local environment. | ||
Args: | ||
project (str): The project ID to pass on to the resource | ||
Returns: | ||
google.cloud.logging.Resource: The default resource based on the environment | ||
""" | ||
gke_cluster_name = retrieve_metadata_server(_GKE_CLUSTER_NAME) | ||
gce_instance_name = retrieve_metadata_server(_GCE_INSTANCE_ID) | ||
|
||
if all([env in os.environ for env in _GAE_ENV_VARS]): | ||
# App Engine Flex or Standard | ||
return _create_app_engine_resource(project) | ||
elif gke_cluster_name is not None: | ||
# Kubernetes Engine | ||
return _create_kubernetes_resource(project) | ||
elif all([env in os.environ for env in _LEGACY_FUNCTION_ENV_VARS]) or all( | ||
[env in os.environ for env in _FUNCTION_ENV_VARS] | ||
): | ||
# Cloud Functions | ||
return _create_functions_resource(project) | ||
elif all([env in os.environ for env in _CLOUD_RUN_ENV_VARS]): | ||
# Cloud Run | ||
return _create_cloud_run_resource(project) | ||
elif gce_instance_name is not None: | ||
# Compute Engine | ||
return _create_compute_resource(project) | ||
else: | ||
# use generic global resource | ||
return _create_global_resource(project) |
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
Submodule environment
updated
28 files
+4 −0 | .gitignore | |
+9 −1 | README.md | |
+2 −0 | deployable/go/.dockerignore | |
+42 −0 | deployable/go/Dockerfile | |
+11 −0 | deployable/go/go.mod | |
+441 −0 | deployable/go/go.sum | |
+111 −0 | deployable/go/main.go | |
+18 −6 | deployable/python/snippets.py | |
+105 −0 | envctl/env_scripts/go/cloudrun.sh | |
+5 −6 | envctl/env_scripts/python/cloudrun.sh | |
+1 −1 | noxfile.py | |
+5 −0 | renovate.json | |
+34 −0 | tests/common/appengine.py | |
+24 −9 | tests/common/common.py | |
+13 −0 | tests/go/__init__.py | |
+26 −0 | tests/go/test_appengine_flex_container.py | |
+26 −0 | tests/go/test_appengine_standard.py | |
+26 −0 | tests/go/test_cloudrun.py | |
+26 −0 | tests/go/test_compute.py | |
+26 −0 | tests/go/test_functions.py | |
+26 −0 | tests/go/test_kubernetes.py | |
+2 −1 | tests/python/test_appengine_flex_container.py | |
+2 −1 | tests/python/test_appengine_flex_python.py | |
+2 −1 | tests/python/test_appengine_standard.py | |
+15 −0 | tests/python/test_cloudrun.py | |
+10 −0 | tests/python/test_compute.py | |
+10 −0 | tests/python/test_functions.py | |
+14 −0 | tests/python/test_kubernetes.py |
Oops, something went wrong.