-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathdynatrace.py
341 lines (293 loc) · 11.1 KB
/
dynatrace.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# Copyright 2020 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
`dynatrace.py`
Datadog backend implementation.
"""
import json
import logging
import pprint
import requests
from retrying import retry
from slo_generator.constants import NO_DATA
LOGGER = logging.getLogger(__name__)
class DynatraceBackend:
"""Backend for querying metrics from Datadog.
Args:
client (obj, optional): Existing `requests.Session` to pass.
api_url (str): Dynatrace API URL.
api_token (str): Dynatrace token.
"""
def __init__(self, client=None, api_url=None, api_token=None):
self.client = client
if client is None:
self.client = DynatraceClient(api_url, api_token)
def query_sli(self, timestamp, window, slo_config):
"""Query SLI value from a given Dynatrace SLO.
Args:
timestamp (int): UNIX timestamp.
window (int): Window (in seconds).
slo_config (dict): SLO configuration.
Returns:
float: SLI value.
"""
measurement = slo_config["spec"]["service_level_indicator"]
start = (timestamp - window) * 1000
end = timestamp * 1000
slo_id = measurement["slo_id"]
data = self.retrieve_slo(start, end, slo_id)
LOGGER.debug(f"Result SLO: {pprint.pformat(data)}")
sli_value = round(data["evaluatedPercentage"] / 100, 4)
return sli_value
def good_bad_ratio(self, timestamp, window, slo_config):
"""Query SLI value from good and valid queries.
Args:
timestamp (int): UNIX timestamp.
window (int): Window (in seconds).
slo_config (dict): SLO configuration.
Returns:
tuple: Good event count, Bad event count.
"""
measurement = slo_config["spec"]["service_level_indicator"]
start = (timestamp - window) * 1000
end = timestamp * 1000
query_good = measurement["query_good"]
query_valid = measurement["query_valid"]
# Good query
good_event_response = self.query(start=start, end=end, **query_good)
LOGGER.debug(f"Result good: {pprint.pformat(good_event_response)}")
good_event_count = DynatraceBackend.count(good_event_response)
# Good query
valid_event_response = self.query(start=start, end=end, **query_valid)
LOGGER.debug(f"Result valid: {pprint.pformat(valid_event_response)}")
valid_event_count = DynatraceBackend.count(valid_event_response)
# Return good, bad
bad_event_count = valid_event_count - good_event_count
return (good_event_count, bad_event_count)
def threshold(self, timestamp, window, slo_config):
"""Compute SLI by counting the number of values below and above a
threshold.
Args:
timestamp (int): UNIX timestamp.
window (int): Window (in seconds).
slo_config (dict): SLO configuration.
Returns:
tuple: Good event count, Bad event count.
"""
measurement = slo_config["spec"]["service_level_indicator"]
start = (timestamp - window) * 1000
end = timestamp * 1000
query_valid = measurement["query_valid"]
threshold = measurement["threshold"]
good_below_threshold = measurement.get("good_below_threshold", True)
response = self.query(start=start, end=end, **query_valid)
LOGGER.debug(f"Result valid: {pprint.pformat(response)}")
return DynatraceBackend.count_threshold(
response, threshold, good_below_threshold
)
def query( # noqa: PLR0913
self,
start,
end,
metric_selector=None,
entity_selector=None,
aggregation="SUM",
):
"""Query Dynatrace Metrics V2.
Args:
start (int): Start timestamp (in milliseconds).
end (int): End timestamp (in milliseconds).
metric_selector (str): Metric selector.
entity_selector (str): Entity selector.
aggregation (str): Aggregation.
Returns:
dict: Dynatrace API response.
"""
params = {
"from": start,
"end": end,
"metricSelector": metric_selector,
"entitySelector": entity_selector,
"aggregation": aggregation,
"includeData": True,
}
return self.client.request("get", "metrics/query", version="v2", **params)
def retrieve_slo(self, start, end, slo_id):
"""Query Dynatrace SLO V2.
Args:
start (int): Start timestamp (in milliseconds).
end (int): End timestamp (in milliseconds).
slo_id (int): SLO ID.
Returns:
dict: Dynatrace API response.
"""
params = {"from": start, "to": end, "timeFrame": "GTF"}
endpoint = "slo/" + slo_id
return self.client.request("get", endpoint, version="v2", **params)
@staticmethod
def count(response):
"""Count events in time series data.
Args:
response (dict): Dynatrace API response.
Returns:
int: Event count.
"""
try:
datapoints = response["result"][0]["data"]
values = []
for point in datapoints:
point_values = [
point
for point in point["values"]
if point is not None and point > 0
]
values.extend(point_values)
return sum(values)
except (IndexError, KeyError) as exception:
LOGGER.warning("Couldn't find any values in timeseries response")
LOGGER.debug(exception)
return NO_DATA # no events in timeseries
@staticmethod
def count_threshold(response, threshold, good_below_threshold=True):
"""Create 2 buckets based on response and a value threshold, and return
number of events contained in each bucket.
Args:
response (dict): Dynatrace API response.
threshold (int): Threshold.
good_below_threshold (bool): If true, good events are < threshold.
Returns:
tuple: Number of good events, Number of bad events.
"""
try:
datapoints = response["result"][0]["data"]
below = []
above = []
for point in datapoints:
points_below = [
point
for point in point["values"]
if point is not None and point < threshold
]
points_above = [
point
for point in point["values"]
if point is not None and point > threshold
]
below.extend(points_below)
above.extend(points_above)
if good_below_threshold:
return len(below), len(above)
return len(above), len(below)
except (IndexError, KeyError, ZeroDivisionError) as exception:
LOGGER.warning("Couldn't find any values in timeseries response")
LOGGER.debug(exception)
return NO_DATA, NO_DATA # no events in timeseries
def retry_http(response):
"""Retry on specific HTTP errors:
* 429: Rate limited to 50 reqs/minute.
Args:
response (dict): Dynatrace API response.
Returns:
bool: True to retry, False otherwise.
"""
retry_codes = [429]
returned_code = response.get("error", {})
if isinstance(returned_code, str):
code = 200
else:
code = int(returned_code.get("code", 200))
return code in retry_codes
class DynatraceClient:
"""Small wrapper around requests to query Dynatrace API.
Args:
api_url (str): Dynatrace API URL.
api_token (str): Dynatrace token.
"""
# Keys to extract response data for each endpoint
ENDPOINT_KEYS = {"metrics": "metrics", "metrics/query": "result"}
def __init__(self, api_url, api_key):
self.client = requests.Session()
self.url = api_url.rstrip("/")
self.token = api_key
@retry(
retry_on_result=retry_http,
wait_exponential_multiplier=1000,
wait_exponential_max=10000,
stop_max_delay=10000,
)
def request( # noqa: PLR0913
self,
method,
endpoint,
name=None,
version="v1",
post_data=None,
key=None,
**params,
):
"""Request Dynatrace API.
Args:
method (str): Requests method between ['post', 'put', 'get'].
endpoint (str): API endpoint.
name (str): API resource name.
version (str): API version. Default: v1.
post_data (dict): JSON data.
key (str): Key to extract data from JSON response.
params (dict): Params to send with request.
Returns:
obj: API response.
"""
req = getattr(self.client, method)
url = f"{self.url}/api/{version}/{endpoint}"
params["Api-Token"] = self.token
headers = {
"Accept": "application/json",
"Content-Type": "application/json",
"User-Agent": "slo-generator",
}
if name:
url += f"/{name}"
params_str = "&".join(
f"{key}={val}" for key, val in params.items() if val is not None
)
url += f"?{params_str}"
LOGGER.debug(f'Running "{method}" request to {url} ...')
if method in ["put", "post"]:
response = req(url, headers=headers, json=post_data)
else:
response = req(url, headers=headers)
LOGGER.debug(f"Response: {response}")
data = DynatraceClient.to_json(response)
next_page_key = data.get("nextPageKey")
if next_page_key:
params = {"nextPageKey": next_page_key, "Api-Token": self.token}
LOGGER.debug(f"Requesting next page: {next_page_key}")
data_next = self.request(method, endpoint, name, version, **params)
next_page_key = data_next.get("nextPageKey")
if not key:
key = DynatraceClient.ENDPOINT_KEYS.get(endpoint, "result")
data[key].extend(data_next[key])
return data
@staticmethod
def to_json(resp):
"""Decode JSON response from Python requests response as utf-8 and
replace \n characters.
Args:
resp (requests.Response): API response.
Returns:
dict: API JSON response.
"""
res = resp.content.decode("utf-8").replace("\n", "")
data = json.loads(res)
return data