|
| 1 | +# Copyright 2021 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import os |
| 16 | + |
| 17 | +from google.cloud.logging_v2.resource import Resource |
| 18 | +from google.cloud.logging_v2._helpers import retrieve_metadata_server |
| 19 | + |
| 20 | +_GAE_SERVICE_ENV = "GAE_SERVICE" |
| 21 | +_GAE_VERSION_ENV = "GAE_VERSION" |
| 22 | +_GAE_INSTANCE_ENV = "GAE_INSTANCE" |
| 23 | +_GAE_ENV_VARS = [_GAE_SERVICE_ENV, _GAE_VERSION_ENV, _GAE_INSTANCE_ENV] |
| 24 | +"""Environment variables set in App Engine environment.""" |
| 25 | + |
| 26 | +_CLOUD_RUN_SERVICE_ID = "K_SERVICE" |
| 27 | +_CLOUD_RUN_REVISION_ID = "K_REVISION" |
| 28 | +_CLOUD_RUN_CONFIGURATION_ID = "K_CONFIGURATION" |
| 29 | +_CLOUD_RUN_ENV_VARS = [ |
| 30 | + _CLOUD_RUN_SERVICE_ID, |
| 31 | + _CLOUD_RUN_REVISION_ID, |
| 32 | + _CLOUD_RUN_CONFIGURATION_ID, |
| 33 | +] |
| 34 | +"""Environment variables set in Cloud Run environment.""" |
| 35 | + |
| 36 | +_FUNCTION_TARGET = "FUNCTION_TARGET" |
| 37 | +_FUNCTION_SIGNATURE = "FUNCTION_SIGNATURE_TYPE" |
| 38 | +_FUNCTION_NAME = "FUNCTION_NAME" |
| 39 | +_FUNCTION_REGION = "FUNCTION_REGION" |
| 40 | +_FUNCTION_ENTRY = "ENTRY_POINT" |
| 41 | +_FUNCTION_ENV_VARS = [_FUNCTION_TARGET, _FUNCTION_SIGNATURE, _CLOUD_RUN_SERVICE_ID] |
| 42 | +_LEGACY_FUNCTION_ENV_VARS = [_FUNCTION_NAME, _FUNCTION_REGION, _FUNCTION_ENTRY] |
| 43 | +"""Environment variables set in Cloud Functions environments.""" |
| 44 | + |
| 45 | + |
| 46 | +_REGION_ID = "instance/region" |
| 47 | +_ZONE_ID = "instance/zone" |
| 48 | +_GCE_INSTANCE_ID = "instance/id" |
| 49 | +"""Attribute in metadata server for compute region and instance.""" |
| 50 | + |
| 51 | +_GKE_CLUSTER_NAME = "instance/attributes/cluster-name" |
| 52 | +"""Attribute in metadata server when in GKE environment.""" |
| 53 | + |
| 54 | + |
| 55 | +def _create_functions_resource(project): |
| 56 | + """Create a standardized Cloud Functions resource. |
| 57 | + Args: |
| 58 | + project (str): The project ID to pass on to the resource |
| 59 | + Returns: |
| 60 | + google.cloud.logging.Resource |
| 61 | + """ |
| 62 | + region = retrieve_metadata_server(_REGION_ID) |
| 63 | + if _FUNCTION_NAME in os.environ: |
| 64 | + function_name = os.environ.get(_FUNCTION_NAME) |
| 65 | + elif _CLOUD_RUN_SERVICE_ID in os.environ: |
| 66 | + function_name = os.environ.get(_CLOUD_RUN_SERVICE_ID) |
| 67 | + else: |
| 68 | + function_name = "" |
| 69 | + resource = Resource( |
| 70 | + type="cloud_function", |
| 71 | + labels={ |
| 72 | + "project_id": project, |
| 73 | + "function_name": function_name, |
| 74 | + "region": region if region else "", |
| 75 | + }, |
| 76 | + ) |
| 77 | + return resource |
| 78 | + |
| 79 | + |
| 80 | +def _create_kubernetes_resource(project): |
| 81 | + """Create a standardized Kubernetes resource. |
| 82 | + Args: |
| 83 | + project (str): The project ID to pass on to the resource |
| 84 | + Returns: |
| 85 | + google.cloud.logging.Resource |
| 86 | + """ |
| 87 | + zone = retrieve_metadata_server(_ZONE_ID) |
| 88 | + cluster_name = retrieve_metadata_server(_GKE_CLUSTER_NAME) |
| 89 | + |
| 90 | + resource = Resource( |
| 91 | + type="k8s_container", |
| 92 | + labels={ |
| 93 | + "project_id": project, |
| 94 | + "location": zone if zone else "", |
| 95 | + "cluster_name": cluster_name if cluster_name else "", |
| 96 | + }, |
| 97 | + ) |
| 98 | + return resource |
| 99 | + |
| 100 | + |
| 101 | +def _create_compute_resource(project): |
| 102 | + """Create a standardized Compute Engine resource. |
| 103 | + Args: |
| 104 | + project (str): The project ID to pass on to the resource |
| 105 | + Returns: |
| 106 | + google.cloud.logging.Resource |
| 107 | + """ |
| 108 | + instance = retrieve_metadata_server(_GCE_INSTANCE_ID) |
| 109 | + zone = retrieve_metadata_server(_ZONE_ID) |
| 110 | + resource = Resource( |
| 111 | + type="gce_instance", |
| 112 | + labels={ |
| 113 | + "project_id": project, |
| 114 | + "instance_id": instance if instance else "", |
| 115 | + "zone": zone if zone else "", |
| 116 | + }, |
| 117 | + ) |
| 118 | + return resource |
| 119 | + |
| 120 | + |
| 121 | +def _create_cloud_run_resource(project): |
| 122 | + """Create a standardized Cloud Run resource. |
| 123 | + Args: |
| 124 | + project (str): The project ID to pass on to the resource |
| 125 | + Returns: |
| 126 | + google.cloud.logging.Resource |
| 127 | + """ |
| 128 | + region = retrieve_metadata_server(_REGION_ID) |
| 129 | + resource = Resource( |
| 130 | + type="cloud_run_revision", |
| 131 | + labels={ |
| 132 | + "project_id": project, |
| 133 | + "service_name": os.environ.get(_CLOUD_RUN_SERVICE_ID, ""), |
| 134 | + "revision_name": os.environ.get(_CLOUD_RUN_REVISION_ID, ""), |
| 135 | + "location": region if region else "", |
| 136 | + "configuration_name": os.environ.get(_CLOUD_RUN_CONFIGURATION_ID, ""), |
| 137 | + }, |
| 138 | + ) |
| 139 | + return resource |
| 140 | + |
| 141 | + |
| 142 | +def _create_app_engine_resource(project): |
| 143 | + """Create a standardized App Engine resource. |
| 144 | + Args: |
| 145 | + project (str): The project ID to pass on to the resource |
| 146 | + Returns: |
| 147 | + google.cloud.logging.Resource |
| 148 | + """ |
| 149 | + zone = retrieve_metadata_server(_ZONE_ID) |
| 150 | + resource = Resource( |
| 151 | + type="gae_app", |
| 152 | + labels={ |
| 153 | + "project_id": project, |
| 154 | + "module_id": os.environ.get(_GAE_SERVICE_ENV, ""), |
| 155 | + "version_id": os.environ.get(_GAE_VERSION_ENV, ""), |
| 156 | + "zone": zone if zone else "", |
| 157 | + }, |
| 158 | + ) |
| 159 | + return resource |
| 160 | + |
| 161 | + |
| 162 | +def _create_global_resource(project): |
| 163 | + return Resource(type="global", labels={"project_id": project}) |
| 164 | + |
| 165 | + |
| 166 | +def detect_resource(project): |
| 167 | + """Return the default monitored resource based on the local environment. |
| 168 | + Args: |
| 169 | + project (str): The project ID to pass on to the resource |
| 170 | + Returns: |
| 171 | + google.cloud.logging.Resource: The default resource based on the environment |
| 172 | + """ |
| 173 | + gke_cluster_name = retrieve_metadata_server(_GKE_CLUSTER_NAME) |
| 174 | + gce_instance_name = retrieve_metadata_server(_GCE_INSTANCE_ID) |
| 175 | + |
| 176 | + if all([env in os.environ for env in _GAE_ENV_VARS]): |
| 177 | + # App Engine Flex or Standard |
| 178 | + return _create_app_engine_resource(project) |
| 179 | + elif gke_cluster_name is not None: |
| 180 | + # Kubernetes Engine |
| 181 | + return _create_kubernetes_resource(project) |
| 182 | + elif all([env in os.environ for env in _LEGACY_FUNCTION_ENV_VARS]) or all( |
| 183 | + [env in os.environ for env in _FUNCTION_ENV_VARS] |
| 184 | + ): |
| 185 | + # Cloud Functions |
| 186 | + return _create_functions_resource(project) |
| 187 | + elif all([env in os.environ for env in _CLOUD_RUN_ENV_VARS]): |
| 188 | + # Cloud Run |
| 189 | + return _create_cloud_run_resource(project) |
| 190 | + elif gce_instance_name is not None: |
| 191 | + # Compute Engine |
| 192 | + return _create_compute_resource(project) |
| 193 | + else: |
| 194 | + # use generic global resource |
| 195 | + return _create_global_resource(project) |
0 commit comments