-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathexample.py
executable file
·136 lines (116 loc) · 4.69 KB
/
example.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#! /usr/bin/env python3
"""Wavefront SDK Usage Example."""
import platform
import sys
import time
import uuid
from wavefront_sdk.client_factory import WavefrontClientFactory
from wavefront_sdk.entities.histogram import histogram_granularity
def send_metrics(wavefront_client):
"""Send a metric."""
wavefront_client.send_metric(
'python.sdk.example.new_york.power.usage', # metric name (str)
time.time() % 3600, # metric value (float)
None, # metric timestamp (int)
platform.node(), # metric source (str)
{}) # metric tags (dict)
def send_delta_counter(wavefront_client):
"""Send a delta counter."""
wavefront_client.send_delta_counter(
'python.sdk.example.delta.proxy.counter', # counter name (str)
1.0, # counter value (float)
platform.node(), # source name (str)
None) # counter tags (dict)
def send_histogram(wavefront_client):
"""Send a histogram."""
wavefront_client.send_distribution(
'python.sdk.example.request.latency', # histogram name (str)
[(30, 20), (5.1, 10)], # histogram centroids (list)
{histogram_granularity.DAY,
histogram_granularity.HOUR,
histogram_granularity.MINUTE}, # granularities (set)
None, # histogram timestamp (int)
platform.node(), # histogram source (str)
{'region': 'us-west'}) # histogram tags (dict)
def send_tracing_span(wavefront_client):
"""Send a tracing span."""
wavefront_client.send_span(
'getAllUsersFromPythonProxy', # span name (str)
1533529977, # start milliseconds (int)
343500, # duration milliseconds (int)
platform.node(), # span source (str)
uuid.UUID('7b3bf470-9456-11e8-9eb6-529269fb1459'), # trace ID (UUID)
uuid.UUID('0313bafe-9457-11e8-9eb6-529269fb1459'), # span ID (UUID)
[
uuid.UUID('2f64e538-9457-11e8-9eb6-529269fb1459')
], # parents (list[UUID])
None, # follows from (list[UUID])
[
('application', 'Wavefront'),
('http.method', 'GET')
], # span tags (list[tuple])
None # span log
)
def send_event(wavefront_client):
"""Send an event."""
wavefront_client.send_event(
'python_sdk_example_event', # event name (str)
1590650592, # event start milliseconds (int)
1590650692, # event end milliseconds (int)
platform.node(), # event source (str)
["env:", "test"], # event tags (list[str] or tuple[str])
{
"severity": "info",
"type": "backup",
"details": "broker backup"
} # event annotations (dict)
)
def main():
"""Send sample metrics in a loop."""
wavefront_proxy_url = sys.argv[1]
csp_base_url, csp_api_token = None, None
csp_app_id, csp_app_secret, csp_org_id = None, None, None
if len(sys.argv) == 4:
csp_base_url = sys.argv[2]
csp_api_token = sys.argv[3]
elif len(sys.argv) > 4:
csp_base_url = sys.argv[2]
csp_app_id = sys.argv[3]
csp_app_secret = sys.argv[4]
if len(sys.argv) > 5:
csp_org_id = sys.argv[5]
client_factory = WavefrontClientFactory()
client_factory.add_client(wavefront_proxy_url,
csp_base_url=csp_base_url,
csp_api_token=csp_api_token,
csp_app_id=csp_app_id,
csp_app_secret=csp_app_secret,
csp_org_id=csp_org_id)
wfront_client = client_factory.get_client()
try:
while True:
send_metrics(wfront_client)
send_histogram(wfront_client)
send_tracing_span(wfront_client)
send_delta_counter(wfront_client)
send_event(wfront_client)
time.sleep(15)
finally:
wfront_client.close()
if __name__ == '__main__':
# Either "proxy://our.proxy.lb.com:2878"
# Or "https://[email protected]"
# should be passed as an input in sys.argv[1]
# Or "https://DOMAIN.wavefront.com" "https://CSP_ENDPOINT.cloud.vmware.com"
# "CSP_API_TOKEN"
# should be passed as an input in sys.argv[1], sys.argv[2],
# and sys.argv[3]
# Or "https://DOMAIN.wavefront.com" "https://CSP_ENDPOINT.cloud.vmware.com"
# "CSP_APP_ID" "CSP_APP_SECRET"
# should be passed as an input in sys.argv[1], sys.argv[2],
# sys.argv[3], and sys.argv[4]
# Or "https://DOMAIN.wavefront.com" "https://CSP_ENDPOINT.cloud.vmware.com"
# "CSP_APP_ID" "CSP_APP_SECRET" "ORG_ID"
# should be passed as an input in sys.argv[1], sys.argv[2],
# sys.argv[3], sys.argv[4], and sys.argv[5]
main()