Skip to content
This repository was archived by the owner on Sep 17, 2025. It is now read-only.

Add utils to automatically detect monitored resources.#292

Merged
liyanhui1228 merged 7 commits intocensus-instrumentation:masterfrom
mayurkale22:monitoredresource_utils
Sep 14, 2018
Merged

Add utils to automatically detect monitored resources.#292
liyanhui1228 merged 7 commits intocensus-instrumentation:masterfrom
mayurkale22:monitoredresource_utils

Conversation

@mayurkale22
Copy link
Copy Markdown
Member

This is according to OpenCensus MonitoredResource specs
issues #204 #205

/cc @songy23

# GAE common attributes
# See: https://cloud.google.com/appengine/docs/flexible/python/runtime#
# environment_variables
GAE_ATTRIBUTES = {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GAE attributes should work as before.

_GCE_ATTRIBUTES = {
# ProjectID is the identifier of the GCP project associated with this
# resource, such as "my-project".
'project_id': 'project/project-id',
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

project_id, instance_id and zone also apply to GKE attributes.

"""
raise NotImplementedError

def _get_resource_label_format(self):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be specific to Stackdriver Trace.


if is_gke_environment():
return monitored_resource.GcpGkeMonitoredResource()
elif is_gae_environment():
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be gce instead of gae. Same for the functions below.

return '/g.co/r/{res_type}/'.format(res_type=self.get_resource_type())


class GcpGceMonitoredResource(MonitoredResource):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should also be a GcpGaeMonitoredResource for backwards-compatibility in Python

@songy23 songy23 requested a review from rghetia September 6, 2018 18:54
if r.status_code != 404:
_aws_metadata_cache[_aws_identity_document] = r.json()

except Exception as e:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you be specific in the exception type here?

_aws_identity_document = 'aws_identity_document'


def initialize_aws_identity_document():
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a doc string explaining what this function is dong?


def initialize_aws_identity_document():
try:
r = requests.get(_AWS_INSTANCE_IDENTITY_DOCUMENT_URI,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of adding the requests dependency, we could use Python builtin module urllib, and be sure to add some sort of retry logic in this function in case the request would fail accidentally.

The idea is that we should avoid adding new dependencies if we don't have to.

pass


class AwsIdentityDocumentUtils(object):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why having a class with every methods in it are staticmethod? We can just have independent functions.

"""
labels_list = []
if _aws_identity_document in _aws_metadata_cache:
from opencensus.trace.attributes import Attributes
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move the import to the top of the file is better.

"""MonitoredResource returns the resource type and resource labels.
"""

def get_resource_type(self):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can make resource_type a property of the class by adding @property decorator to the method. And the method name could just be resource_type.

"""
raise NotImplementedError

def get_resource_labels(self):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

https://cloud.google.com/monitoring/api/resources#tag_gce_instance
"""

def get_resource_type(self):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here and below.

"""

def get_resource_type(self):
return "gce_instance"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid using magic strings in functions. We could define it at the top of the file like GCE_INSTANCE = "gce_instance".

Comment thread setup.py Outdated

install_requires = [
'google-api-core >= 0.1.1, < 2.0.0',
'google-api-core >= 0.1.1, < 2.0.0', 'requests'
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After using urllib, we won't need this dependency.

@liyanhui1228
Copy link
Copy Markdown
Contributor

Be sure to add more unit tests, we will need to have 100% test coverage in order to make the CI happy. To skip test coverage check for some specific lines, adding # pragma: NO COVER to the end of the line.

Comment thread opencensus/common/http_handler.py Outdated
response_content = None
except URLError:
response_content = None
except socket.timeout:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

except (HTTPError, URLError, socket.timeout):
    response_content = None

"""Initialize metadata service once and load gcp metadata into map
This method should only be called once.
"""
global inited
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a little weird to declare global variable in a class as we aren't using them anywhere outside. Can we declare the variables inside the class?

class GcpMetadataConfig(object):
    inited = False
    is_running_on_gcp = False

"""A Google Container Engine (GKE) container instance.
KUBERNETES_SERVICE_HOST environment variable must be set.
"""
if _KUBERNETES_SERVICE_HOST in os.environ:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return _KUBERNETES_SERVICE_HOST in os.environ

@liyanhui1228 liyanhui1228 merged commit d665a14 into census-instrumentation:master Sep 14, 2018
@mayurkale22 mayurkale22 deleted the monitoredresource_utils branch September 14, 2018 23:39
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants