1313# limitations under the License.
1414
1515from opencensus .common .http_handler import get_request
16- import os
1716
1817_GCP_METADATA_URI = 'http://metadata/computeMetadata/v1/'
1918_GCP_METADATA_URI_HEADER = {'Metadata-Flavor' : 'Google' }
2019
21- # GCE common attributes
22- # See: https://cloud.google.com/appengine/docs/flexible/python/runtime#
23- # environment_variables
24- _GCE_ATTRIBUTES = {
25- # ProjectID is the identifier of the GCP project associated with this
26- # resource, such as "my-project".
27- 'project_id' : 'project/project-id' ,
28-
29- # instance_id is the numeric VM instance identifier assigned by
30- # Compute Engine.
31- 'instance_id' : 'instance/id' ,
32-
33- # zone is the Compute Engine zone in which the VM is running.
34- 'zone' : 'instance/zone'
35- }
20+ # ID of the GCP project associated with this resource, such as "my-project"
21+ PROJECT_ID_KEY = 'project_id'
3622
37- _GKE_ATTRIBUTES = {
38- # ProjectID is the identifier of the GCP project associated with this
39- # resource, such as "my-project".
40- 'project_id' : 'project/project-id' ,
41-
42- # instance_id is the numeric VM instance identifier assigned by
43- # Compute Engine.
44- 'instance_id' : 'instance/id' ,
45-
46- # zone is the Compute Engine zone in which the VM is running.
47- 'zone' : 'instance/zone' ,
48-
49- # cluster_name is the name for the cluster the container is running in.
50- 'cluster_name' : 'instance/attributes/cluster-name'
51- }
23+ # Numeric VM instance identifier assigned by GCE
24+ INSTANCE_ID_KEY = 'instance_id'
5225
53- # Following attributes are derived from environment variables. They are
54- # configured via yaml file. For details refer to:
55- # https://cloud.google.com/kubernetes-engine/docs/tutorials/
56- # custom-metrics-autoscaling#exporting_metrics_from_the_application
57- _GKE_ENV_ATTRIBUTES = {
58- # ContainerName is the name of the container.
59- 'container_name' : 'CONTAINER_NAME' ,
26+ # The GCE zone in which the VM is running
27+ ZONE_KEY = 'zone'
6028
61- # namespace_id is the identifier for the cluster namespace the container
62- # is running in
63- 'namespace_id' : 'NAMESPACE' ,
29+ # GKE cluster name
30+ CLUSTER_NAME_KEY = 'instance/attributes/cluster-name'
6431
65- # pod_id is the identifier for the pod the container is running in.
66- 'pod_id' : 'HOSTNAME'
32+ # GCE common attributes
33+ # See: https://cloud.google.com/appengine/docs/flexible/python/runtime#environment_variables # noqa
34+ _GCE_ATTRIBUTES = {
35+ PROJECT_ID_KEY : 'project/project-id' ,
36+ INSTANCE_ID_KEY : 'instance/id' ,
37+ ZONE_KEY : 'instance/zone'
6738}
6839
69- # Kubenertes environment variables
70- _KUBERNETES_SERVICE_HOST = 'KUBERNETES_SERVICE_HOST'
7140
72- gcp_metadata_map = {}
41+ _GCP_METADATA_MAP = {}
7342
7443
7544class GcpMetadataConfig (object ):
@@ -90,23 +59,19 @@ def _initialize_metadata_service(cls):
9059 if cls .inited :
9160 return
9261
93- instance_id = cls ._get_attribute ( 'instance_id ' )
62+ instance_id = cls .get_attribute ( 'instance/id ' )
9463
9564 if instance_id is not None :
9665 cls .is_running = True
9766
98- gcp_metadata_map ['instance_id' ] = instance_id
99-
100- attributes = _GCE_ATTRIBUTES
101- if _KUBERNETES_SERVICE_HOST in os .environ :
102- attributes = _GKE_ATTRIBUTES
67+ _GCP_METADATA_MAP ['instance_id' ] = instance_id
10368
10469 # fetch attributes from metadata request
105- for attribute_key , attribute_uri in attributes .items ():
106- if attribute_key not in gcp_metadata_map :
107- attribute_value = cls ._get_attribute ( attribute_key )
108- if attribute_value is not None :
109- gcp_metadata_map [attribute_key ] = attribute_value
70+ for attribute_key , attribute_uri in _GCE_ATTRIBUTES .items ():
71+ if attribute_key not in _GCP_METADATA_MAP :
72+ attribute_value = cls .get_attribute ( attribute_uri )
73+ if attribute_value is not None : # pragma: NO COVER
74+ _GCP_METADATA_MAP [attribute_key ] = attribute_value
11075
11176 cls .inited = True
11277
@@ -118,35 +83,19 @@ def is_running_on_gcp(cls):
11883 def get_gce_metadata (self ):
11984 """for GCP GCE instance"""
12085 if self .is_running_on_gcp ():
121- return gcp_metadata_map
86+ return _GCP_METADATA_MAP
12287
12388 return dict ()
12489
125- def get_gke_metadata (self ):
126- """for GCP GKE container."""
127- gke_metadata = {}
128-
129- if self .is_running_on_gcp ():
130- gke_metadata = gcp_metadata_map
131-
132- # fetch attributes from Environment Variables
133- for attribute_key , attribute_env in _GKE_ENV_ATTRIBUTES .items ():
134- attribute_value = os .environ .get (attribute_env )
135- if attribute_value is not None :
136- gke_metadata [attribute_key ] = attribute_value
137-
138- return gke_metadata
139-
14090 @staticmethod
141- def _get_attribute ( attribute_key ):
91+ def get_attribute ( attribute_uri ):
14292 """
14393 Fetch the requested instance metadata entry.
14494 :param attribute_uri: attribute_uri: attribute name relative to the
14595 computeMetadata/v1 prefix
14696 :return: The value read from the metadata service or None
14797 """
148- attribute_value = get_request (_GCP_METADATA_URI +
149- _GKE_ATTRIBUTES [attribute_key ],
98+ attribute_value = get_request (_GCP_METADATA_URI + attribute_uri ,
15099 _GCP_METADATA_URI_HEADER )
151100
152101 if attribute_value is not None and isinstance (attribute_value , bytes ):
0 commit comments