forked from Unleash/unleash-client-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.py
44 lines (35 loc) · 1.47 KB
/
metrics.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
import json
import requests
from UnleashClient.constants import REQUEST_TIMEOUT, APPLICATION_HEADERS, METRICS_URL
from UnleashClient.utils import LOGGER, log_resp_info
# pylint: disable=broad-except
def send_metrics(url: str,
request_body: dict,
custom_headers: dict,
custom_options: dict) -> bool:
"""
Attempts to send metrics to Unleash server
Notes:
* If unsuccessful (i.e. not HTTP status code 200), message will be logged
:param url:
:param request_body:
:param custom_headers:
:param custom_options:
:return: true if registration successful, false if registration unsuccessful or exception.
"""
try:
LOGGER.info("Sending messages to with unleash @ %s", url)
LOGGER.info("unleash metrics information: %s", request_body)
resp = requests.post(url + METRICS_URL,
data=json.dumps(request_body),
headers={**custom_headers, **APPLICATION_HEADERS},
timeout=REQUEST_TIMEOUT, **custom_options)
if resp.status_code != 202:
log_resp_info(resp)
LOGGER.warning("Unleash CLient metrics submission failed.")
return False
LOGGER.info("Unleash Client metrics successfully sent!")
return True
except requests.RequestException as exc:
LOGGER.warning("Unleash Client metrics submission failed due to exception: %s", exc)
return False