-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathspeedtest_cli_datadog.py
More file actions
executable file
·71 lines (52 loc) · 1.73 KB
/
speedtest_cli_datadog.py
File metadata and controls
executable file
·71 lines (52 loc) · 1.73 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging
import functools
from datadog import statsd
import speedtest
log = logging.getLogger('speedtest_datadog')
logging.basicConfig()
log.setLevel(logging.DEBUG)
class DatadogGauge(object):
"""Decorator that allow to put the return of the decorated function
in a statsd.gauge
:ivar str label: label for the gauge
"""
def __init__(self, label):
self.label = label
def __call__(self, func):
"""Decoration"""
@functools.wraps(func)
def wrappee(*args, **kwargs):
"""Wrapper of the function."""
ret = func(*args, **kwargs)
log.debug('%s: %s', self.label, ret)
statsd.gauge(self.label, ret)
return ret
return wrappee
class DatadogGaugeLatency(DatadogGauge):
"""Represent a specification of the decorator for the special case of latency."""
def __call__(self, func):
"""Decoration"""
@functools.wraps(func)
def wrappee(*args, **kwargs):
"""Wrapper of the function."""
ret = func(*args, **kwargs)
log.debug('%s: %s', self.label, ret['latency'])
statsd.gauge(self.label, ret['latency'])
return ret
return wrappee
# Decoration of the functions of speedtest
speedtest.Speedtest.download = DatadogGauge('speedtest.download')(
speedtest.Speedtest.download
)
speedtest.Speedtest.upload = DatadogGauge('speedtest.upload')(
speedtest.Speedtest.upload
)
speedtest.Speedtest.get_best_server = DatadogGaugeLatency('speedtest.latency')(
speedtest.Speedtest.get_best_server
)
# required for the command line entry point.
main = speedtest.main
if __name__ == '__main__':
main()