|
| 1 | +#! /usr/bin/env python3 |
1 | 2 | """Wavefront SDK Usage Example.""" |
2 | 3 |
|
| 4 | +import platform |
3 | 5 | import sys |
4 | 6 | import time |
5 | 7 | import uuid |
|
11 | 13 | def send_metrics(wavefront_client): |
12 | 14 | """Send a metric.""" |
13 | 15 | wavefront_client.send_metric( |
14 | | - 'python.proxy.new york.power.usage', |
15 | | - 42422.0, None, 'localhost', None) |
| 16 | + 'python.sdk.example.new_york.power.usage', # metric name (str) |
| 17 | + time.time() % 3600, # metric value (float) |
| 18 | + None, # metric timestamp (int) |
| 19 | + platform.node(), # metric source (str) |
| 20 | + {}) # metric tags (dict) |
16 | 21 |
|
17 | 22 |
|
18 | 23 | def send_delta_counter(wavefront_client): |
19 | 24 | """Send a delta counter.""" |
20 | 25 | wavefront_client.send_delta_counter( |
21 | | - 'python.delta.proxy.counter', |
22 | | - 1.0, 'localhost', None) |
| 26 | + 'python.sdk.example.delta.proxy.counter', # counter name (str) |
| 27 | + 1.0, # counter value (float) |
| 28 | + platform.node(), # source name (str) |
| 29 | + None) # counter tags (dict) |
23 | 30 |
|
24 | 31 |
|
25 | 32 | def send_histogram(wavefront_client): |
26 | 33 | """Send a histogram.""" |
27 | 34 | wavefront_client.send_distribution( |
28 | | - 'python.proxy.request.latency', |
29 | | - [(30, 20), (5.1, 10)], {histogram_granularity.DAY, |
30 | | - histogram_granularity.HOUR, |
31 | | - histogram_granularity.MINUTE}, |
32 | | - None, 'appServer1', {'region': 'us-west'}) |
| 35 | + 'python.sdk.example.request.latency', # histogram name (str) |
| 36 | + [(30, 20), (5.1, 10)], # histogram centroids (list) |
| 37 | + {histogram_granularity.DAY, |
| 38 | + histogram_granularity.HOUR, |
| 39 | + histogram_granularity.MINUTE}, # granularities (set) |
| 40 | + None, # histogram timestamp (int) |
| 41 | + platform.node(), # histogram source (str) |
| 42 | + {'region': 'us-west'}) # histogram tags (dict) |
33 | 43 |
|
34 | 44 |
|
35 | 45 | def send_tracing_span(wavefront_client): |
36 | 46 | """Send a tracing span.""" |
37 | 47 | wavefront_client.send_span( |
38 | | - 'getAllUsersFromPythonProxy', 1533529977, 343500, 'localhost', |
39 | | - uuid.UUID('7b3bf470-9456-11e8-9eb6-529269fb1459'), |
40 | | - uuid.UUID('0313bafe-9457-11e8-9eb6-529269fb1459'), |
41 | | - [uuid.UUID('2f64e538-9457-11e8-9eb6-529269fb1459')], None, |
42 | | - [('application', 'Wavefront'), |
43 | | - ('http.method', 'GET')], None) |
| 48 | + 'getAllUsersFromPythonProxy', # span name (str) |
| 49 | + 1533529977, # start milliseconds (int) |
| 50 | + 343500, # duration milliseconds (int) |
| 51 | + platform.node(), # span source (str) |
| 52 | + uuid.UUID('7b3bf470-9456-11e8-9eb6-529269fb1459'), # trace ID (UUID) |
| 53 | + uuid.UUID('0313bafe-9457-11e8-9eb6-529269fb1459'), # span ID (UUID) |
| 54 | + [ |
| 55 | + uuid.UUID('2f64e538-9457-11e8-9eb6-529269fb1459') |
| 56 | + ], # parents (list[UUID]) |
| 57 | + None, # follows from (list[UUID]) |
| 58 | + [ |
| 59 | + ('application', 'Wavefront'), |
| 60 | + ('http.method', 'GET') |
| 61 | + ], # span tags (list[tuple]) |
| 62 | + None # span log |
| 63 | + ) |
44 | 64 |
|
45 | 65 |
|
46 | 66 | def send_event(wavefront_client): |
47 | 67 | """Send an event.""" |
48 | 68 | wavefront_client.send_event( |
49 | | - 'event_via_proxy', |
50 | | - 1590650592, |
51 | | - 1590650692, |
52 | | - "localhost", |
53 | | - ["env:", "test"], |
54 | | - {"severity": "info", |
55 | | - "type": "backup", |
56 | | - "details": "broker backup"}) |
57 | | - |
58 | | - |
59 | | -if __name__ == '__main__': |
60 | | - # Either "proxy://our.proxy.lb.com:2878" |
61 | | - # Or "https://someToken@DOMAIN.wavefront.com" |
| 69 | + 'python_sdk_example_event', # event name (str) |
| 70 | + 1590650592, # event start milliseconds (int) |
| 71 | + 1590650692, # event end milliseconds (int) |
| 72 | + platform.node(), # event source (str) |
| 73 | + ["env:", "test"], # event tags (list[str] or tuple[str]) |
| 74 | + { |
| 75 | + "severity": "info", |
| 76 | + "type": "backup", |
| 77 | + "details": "broker backup" |
| 78 | + } # event annotations (dict) |
| 79 | + ) |
| 80 | + |
| 81 | + |
| 82 | +def main(): |
| 83 | + """Send sample metrics in a loop.""" |
62 | 84 | wavefront_proxy_url = sys.argv[1] |
63 | 85 |
|
64 | 86 | client_factory = WavefrontClientFactory() |
65 | 87 | client_factory.add_client(wavefront_proxy_url) |
66 | | - wavefront_client = client_factory.get_client() |
| 88 | + wfront_client = client_factory.get_client() |
67 | 89 |
|
68 | 90 | try: |
69 | 91 | while True: |
70 | | - send_metrics(wavefront_client) |
71 | | - send_histogram(wavefront_client) |
72 | | - send_tracing_span(wavefront_client) |
73 | | - send_delta_counter(wavefront_client) |
74 | | - send_event(wavefront_client) |
| 92 | + send_metrics(wfront_client) |
| 93 | + send_histogram(wfront_client) |
| 94 | + send_tracing_span(wfront_client) |
| 95 | + send_delta_counter(wfront_client) |
| 96 | + send_event(wfront_client) |
75 | 97 |
|
76 | 98 | time.sleep(15) |
77 | 99 | finally: |
78 | | - wavefront_client.close() |
| 100 | + wfront_client.close() |
| 101 | + |
| 102 | + |
| 103 | +if __name__ == '__main__': |
| 104 | + # Either "proxy://our.proxy.lb.com:2878" |
| 105 | + # Or "https://someToken@DOMAIN.wavefront.com" |
| 106 | + # should be passed as an input in sys.argv[1] |
| 107 | + main() |
0 commit comments