Created
November 9, 2017 22:40
-
-
Save KyleJamesWalker/e2063639e371ba46834a0fa522ed19a3 to your computer and use it in GitHub Desktop.
Monitoring AWS Lambda functions with Datadog Example
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
"""Send metrics over stdout for datadog lambda monitoring. | |
Monitoring details: | |
https://www.datadoghq.com/blog/monitoring-lambda-functions-datadog/ | |
""" | |
import time | |
from copy import copy | |
CONFIG = { | |
'tags': {}, | |
'required_tags': ['dept', 'env', 'app'], | |
'metrics_types': ['count', 'gauge'] | |
} | |
def tag_str(tags): | |
"""Converts a dictionary to a datadog tag string. | |
Args: | |
tags (dict): Tags to convert | |
Returns: | |
str: Datadog tags | |
""" | |
return ','.join(["{}:{}".format(key, val) for key, val in tags.items()]) | |
def post(metric_value, metric_type, metric_name, **kwargs): | |
"""Post a given metric | |
Args: | |
metric_value (str): Value of the metric that can be converted to a str | |
metric_type (str): Datadog metric type to post (count, or gauge) | |
metric_name (str): Name of the metric | |
kwargs (str): Keyword arguments to tag metric with | |
""" | |
metric_ts = int(time.time()) | |
post_tags = copy(CONFIG['tags']) | |
post_tags.update(kwargs) | |
print('MONITORING|{epoch_ts}|{m_value}|{m_type}|{m_name}|#{tags}'.format( | |
epoch_ts=metric_ts, | |
m_value=metric_value, | |
m_type=metric_type, | |
m_name=metric_name, | |
tags=tag_str(post_tags) | |
)) | |
def gauge(metric_value, metric_name, **kwargs): | |
"""Post a given gauge metric | |
Args: | |
metric_value (str): Value of the metric that can be converted to a str | |
metric_name (str): Name of the metric | |
kwargs (str): Keyword arguments to tag metric with | |
""" | |
return post(metric_value, 'gauge', metric_name, **kwargs) | |
def count(metric_value, metric_name, **kwargs): | |
"""Post a given count metric | |
Args: | |
metric_value (str): Value of the metric that can be converted to a str | |
metric_name (str): Name of the metric | |
kwargs (str): Keyword arguments to tag metric with | |
""" | |
return post(metric_value, 'count', metric_name, **kwargs) | |
def set_constant_tags(tags): | |
"""Set constant tags. | |
Sets tags that will be sent with all metrics posted. | |
Args: | |
tags (dict): Dictiony of tags | |
""" | |
if not all(tag in tags for tag in CONFIG['required_tags']): | |
raise ValueError("Not all required tags have been set") | |
CONFIG['tags'] = copy(tags) | |
def _example(): | |
set_constant_tags({ | |
'dept': 'dmds', | |
'env': 'test', | |
'app': 'simple_example', | |
'bonus': 'new_tag' | |
}) | |
count(1, 'example.counter') | |
gauge(1.3, 'example.gauge') | |
count(1, 'example.counter', server_node='fancy') | |
count(1, 'example.counter', server_node='pants') | |
if __name__ == '__main__': | |
_example() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment