-
Notifications
You must be signed in to change notification settings - Fork 4
/
lambda_dd_metrics.py
98 lines (84 loc) · 3.43 KB
/
lambda_dd_metrics.py
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
90
91
92
93
94
95
96
97
98
#!/usr/bin/env python
'''
Simple interface for reporting metrics to DataDog.
'''
from __future__ import print_function
from functools import wraps
import time
class DataDogMetrics(object):
'''
Datadog supports printing to stdout to report metrics. This only gives us
counts and gauges:
https://www.datadoghq.com/blog/monitoring-lambda-functions-datadog
Another method would be via the API but that one only supports gauges and
requires auth, which I'd rather not do until they've added support for
histograms and counts.
'''
def __init__(self, service_prefix, stats_group=None):
self.service_prefix = service_prefix
self.default_tags = ['group:%s' % stats_group] if stats_group is not None else []
def incr(self, metric_name, count=1, tags=None):
'''
Incr - Increment a counter metric, providing a count of occurrences per
second.
'''
full_metric_name = self._build_metric_name(metric_name)
all_tags = self._build_tags(tags)
return self._print_metric('count', full_metric_name, count, all_tags)
def gauge(self, metric_name, value, tags=None):
'''
Gauge - Gauges are a constant data type. They are not subject to
averaging, and they don't change unless you change them. That is, once
you set a gauge value, it will be a flat line on the graph until you
change it again.
'''
full_metric_name = self._build_metric_name(metric_name)
all_tags = self._build_tags(tags)
return self._print_metric('gauge', full_metric_name, value, all_tags)
def histogram(self, metric_name, value, tags=None):
'''
Histogram - Send a histogram, tracking multiple samples of a metric
'''
full_metric_name = self._build_metric_name(metric_name)
all_tags = self._build_tags(tags)
return self._print_metric('histogram', full_metric_name, value, all_tags)
def timer(self, metric_name, tags=None):
'''
Timer - A convenient decorator that automatically records the runtime
of your function and reports it as a histogram.
'''
def decorator(function):
@wraps(function)
def wrapper(*args, **kwargs):
start_time = time.time()
ret_val = function(*args, **kwargs)
duration = time.time() - start_time
self.histogram(metric_name, duration, tags)
return ret_val
return wrapper
return decorator
def timing(self, metric_name, delta, tags=None):
'''
Timing - Track a duration event
'''
return self.histogram(metric_name, delta, tags)
def set(self, metric_name, value, tags=None):
'''
Set - Send a metric that tracks the number of unique items in a set
'''
# NOT SUPPORTED YET
def _build_tags(self, tags=None):
return (tags or []) + self.default_tags
def _build_metric_name(self, metric_name):
return '{0}.{1}'.format(self.service_prefix, metric_name)
def _print_metric(self, metric_type, metric_name, value, tags):
unix_epoch_timestamp = int(time.time())
metric = 'MONITORING|{0}|{1}|{2}|{3}'.format(
unix_epoch_timestamp,
value,
metric_type,
metric_name)
if tags:
metric += '|#{}'.format(','.join(tags))
print(metric)
return metric