-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add easier namespacing for data submission #3718
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,6 +58,9 @@ class __AgentCheckPy3(object): | |
The base class for any Agent based integrations | ||
""" | ||
|
||
# If defined, this will be the prefix of every metric/service check and the source type of events | ||
__NAMESPACE__ = '' | ||
|
||
OK, WARNING, CRITICAL, UNKNOWN = ServiceCheck | ||
|
||
# Used by `self.http` RequestsWrapper | ||
|
@@ -203,6 +206,12 @@ def get_instance_proxy(self, instance, uri, proxies=None): | |
def _context_uid(self, mtype, name, tags=None, hostname=None): | ||
return '{}-{}-{}-{}'.format(mtype, name, tags if tags is None else hash(frozenset(tags)), hostname) | ||
|
||
def _format_namespace(self, s): | ||
if self.__NAMESPACE__: | ||
return '{}.{}'.format(self.__NAMESPACE__, ensure_unicode(s)) | ||
|
||
return ensure_unicode(s) | ||
|
||
def _submit_metric(self, mtype, name, value, tags=None, hostname=None, device_name=None): | ||
if value is None: | ||
# ignore metric sample | ||
|
@@ -234,7 +243,7 @@ def _submit_metric(self, mtype, name, value, tags=None, hostname=None, device_na | |
self.warning(err_msg) | ||
return | ||
|
||
aggregator.submit_metric(self, self.check_id, mtype, ensure_unicode(name), value, tags, hostname) | ||
aggregator.submit_metric(self, self.check_id, mtype, self._format_namespace(name), value, tags, hostname) | ||
|
||
def gauge(self, name, value, tags=None, hostname=None, device_name=None): | ||
self._submit_metric(aggregator.GAUGE, name, value, tags=tags, hostname=hostname, device_name=device_name) | ||
|
@@ -281,7 +290,9 @@ def service_check(self, name, status, tags=None, hostname=None, message=None): | |
else: | ||
message = ensure_unicode(message) | ||
|
||
aggregator.submit_service_check(self, self.check_id, ensure_unicode(name), status, tags, hostname, message) | ||
aggregator.submit_service_check( | ||
self, self.check_id, self._format_namespace(name), status, tags, hostname, message | ||
) | ||
|
||
def event(self, event): | ||
# Enforce types of some fields, considerably facilitates handling in go bindings downstream | ||
|
@@ -295,12 +306,17 @@ def event(self, event): | |
'Error decoding unicode field `{}` to utf-8 encoded string, cannot submit event'.format(key) | ||
) | ||
return | ||
|
||
if event.get('tags'): | ||
event['tags'] = self._normalize_tags_type(event['tags']) | ||
if event.get('timestamp'): | ||
event['timestamp'] = int(event['timestamp']) | ||
if event.get('aggregation_key'): | ||
event['aggregation_key'] = ensure_unicode(event['aggregation_key']) | ||
|
||
if self.__NAMESPACE__: | ||
event.setdefault('source_type_name', self.__NAMESPACE__) | ||
|
||
aggregator.submit_event(self, self.check_id, event) | ||
|
||
# TODO(olivier): implement service_metadata if it's worth it | ||
|
@@ -459,6 +475,9 @@ class __AgentCheckPy2(object): | |
The base class for any Agent based integrations | ||
""" | ||
|
||
# If defined, this will be the prefix of every metric/service check and the source type of events | ||
__NAMESPACE__ = '' | ||
|
||
OK, WARNING, CRITICAL, UNKNOWN = ServiceCheck | ||
|
||
""" | ||
|
@@ -597,6 +616,12 @@ def get_instance_proxy(self, instance, uri, proxies=None): | |
def _context_uid(self, mtype, name, tags=None, hostname=None): | ||
return '{}-{}-{}-{}'.format(mtype, name, tags if tags is None else hash(frozenset(tags)), hostname) | ||
|
||
def _format_namespace(self, s): | ||
if self.__NAMESPACE__: | ||
return '{}.{}'.format(self.__NAMESPACE__, ensure_bytes(s)) | ||
|
||
return ensure_bytes(s) | ||
|
||
def _submit_metric(self, mtype, name, value, tags=None, hostname=None, device_name=None): | ||
if value is None: | ||
# ignore metric sample | ||
|
@@ -628,7 +653,7 @@ def _submit_metric(self, mtype, name, value, tags=None, hostname=None, device_na | |
self.warning(err_msg) | ||
return | ||
|
||
aggregator.submit_metric(self, self.check_id, mtype, ensure_bytes(name), value, tags, hostname) | ||
aggregator.submit_metric(self, self.check_id, mtype, self._format_namespace(name), value, tags, hostname) | ||
|
||
def gauge(self, name, value, tags=None, hostname=None, device_name=None): | ||
self._submit_metric(aggregator.GAUGE, name, value, tags=tags, hostname=hostname, device_name=device_name) | ||
|
@@ -675,7 +700,9 @@ def service_check(self, name, status, tags=None, hostname=None, message=None): | |
else: | ||
message = ensure_bytes(message) | ||
|
||
aggregator.submit_service_check(self, self.check_id, ensure_bytes(name), status, tags, hostname, message) | ||
aggregator.submit_service_check( | ||
self, self.check_id, self._format_namespace(name), status, tags, hostname, message | ||
) | ||
|
||
def event(self, event): | ||
# Enforce types of some fields, considerably facilitates handling in go bindings downstream | ||
|
@@ -689,12 +716,17 @@ def event(self, event): | |
"Error encoding unicode field '%s' to utf-8 encoded string, can't submit event", key | ||
) | ||
return | ||
|
||
if event.get('tags'): | ||
event['tags'] = self._normalize_tags_type(event['tags']) | ||
if event.get('timestamp'): | ||
event['timestamp'] = int(event['timestamp']) | ||
if event.get('aggregation_key'): | ||
event['aggregation_key'] = ensure_bytes(event['aggregation_key']) | ||
|
||
if self.__NAMESPACE__: | ||
event.setdefault('source_type_name', self.__NAMESPACE__) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
|
||
aggregator.submit_event(self, self.check_id, event) | ||
|
||
# TODO(olivier): implement service_metadata if it's worth it | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the only bit that would introduce a change in behavior. I don't really object to it, it's just worth noting.