-
Notifications
You must be signed in to change notification settings - Fork 47
/
pyformance_usecase.py
executable file
·86 lines (68 loc) · 2.88 KB
/
pyformance_usecase.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
#!/usr/bin/env python
# Copyright (C) 2016-2018 SignalFx, Inc. All rights reserved.
import argparse
import os
import logging
import sys
import time
sys.path.insert(0, os.path.join(
os.path.dirname(os.path.abspath(__file__)), '..'))
# import the signalfx pyformance library
import signalfx.pyformance as pyf # noqa
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='SignalFx metrics reporting demo')
parser.add_argument('token', help='Your SignalFx API access token')
options = parser.parse_args()
# @pyf.count_calls counts the number of times callme is invoked
# and adds the metric to the global pyformance registry
@pyf.count_calls
def callme():
logging.info('Called me!')
# pyf.gauge() adds a gauge to the global pyformance registry
pyf.gauge('demo.time').set_value(time.time())
# @pyf.count_calls_with_dims counts the number of times
# callme_with_dims is invoked. The dimension
# ('dimension_key'='dimension_value') is included on the metric
@pyf.count_calls_with_dims(dimension_key='dimension_value')
def callme_with_dims():
logging.info('Called me with dimensions!')
# pyf.gauge() adds a gauge to the global pyformance registry
# and accepts dimensions as keyword arguments
pyf.gauge('demo.time2',
dimension_key='dimension_value').set_value(time.time())
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
# the signalfx reporter uses the pyformance global_registry by default
sfx = pyf.SignalFxReporter(options.token)
sfx.start()
# a custom registry can be created with SignalFx Pyformance MetricsRegistry
custom_registry = pyf.MetricsRegistry()
# a custom registry can be set as the global registry using
# pyf.set_global_registry(custom_registry)
# the new registry must be passed to a new SignalFx Pyformance Reporter
custom_sfx = pyf.SignalFxReporter(options.token, registry=custom_registry)
custom_sfx.start()
# @ style decorators only work with the global registry
def custom_callme():
logging.info('Called me!')
# metrics may be registered with the new registry
custom_registry.gauge('demo.time.custom').set_value(time.time())
def custom_callme_with_dims():
logging.info('Called me with dimensions!')
# pass dimensions as keyword arguments in the metric function
# calls in the signalfx pyformance library
custom_registry.gauge(
'demo.time2.custom',
dimension_key='dimension_value').set_value(time.time())
pyf.gauge('demo.pid').set_value(os.getpid())
try:
while True:
callme()
callme_with_dims()
custom_callme()
custom_callme_with_dims()
time.sleep(0.5)
except KeyboardInterrupt:
pass
sfx.stop()
custom_sfx.stop()