forked from census-instrumentation/opencensus-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitored_resource.py
More file actions
89 lines (68 loc) · 2.96 KB
/
monitored_resource.py
File metadata and controls
89 lines (68 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# Copyright 2018 Google Inc.
#
# 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 opencensus.common import resource
from opencensus.common.monitored_resource import aws_identity_doc_utils
from opencensus.common.monitored_resource import gcp_metadata_config
# Supported environments (resource types)
_GCE_INSTANCE = "gce_instance"
_GKE_CONTAINER = "gke_container"
_AWS_EC2_INSTANCE = "aws_ec2_instance"
# Kubenertes environment variables
_KUBERNETES_SERVICE_HOST = 'KUBERNETES_SERVICE_HOST'
def is_gke_environment():
"""Whether the environment is a GKE container instance.
The KUBERNETES_SERVICE_HOST environment variable must be set.
"""
return _KUBERNETES_SERVICE_HOST in os.environ
def is_gce_environment():
"""Whether the environment is a virtual machine on GCE."""
return gcp_metadata_config.GcpMetadataConfig.is_running_on_gcp()
def is_aws_environment():
"""Whether the environment is a virtual machine instance on EC2."""
return aws_identity_doc_utils.AwsIdentityDocumentUtils.is_running_on_aws()
def get_instance():
"""Get a resource based on the application environment.
Returns a `Resource` configured for the current environment, or None if the
environment is unknown or unsupported.
Supported environments include:
1. 'gke_container'
- https://cloud.google.com/monitoring/api/resources#tag_gke_container
2. 'gce_instance'
- https://cloud.google.com/monitoring/api/resources#tag_gce_instance
3. 'aws_ec2_instance'
- https://cloud.google.com/monitoring/api/resources#tag_aws_ec2_instance
:rtype: :class:`opencensus.common.resource.Resource` or None
:return: A `Resource` configured for the current environment.
"""
resources = []
env_resource = resource.get_from_env()
if env_resource is not None:
resources.append(env_resource)
if is_gke_environment():
resources.append(resource.Resource(
_GKE_CONTAINER,
gcp_metadata_config.GcpMetadataConfig().get_gke_metadata()))
if is_gce_environment():
resources.append(resource.Resource(
_GCE_INSTANCE,
gcp_metadata_config.GcpMetadataConfig().get_gce_metadata()))
elif is_aws_environment():
resources.append(resource.Resource(
_AWS_EC2_INSTANCE,
(aws_identity_doc_utils.AwsIdentityDocumentUtils()
.get_aws_metadata())))
if not resources:
return None
return resource.merge_resources(resources[0], *resources[1:])