Skip to content

Commit

Permalink
Merge pull request #137 from th-ch/add-synthetics-script-to-extract-u…
Browse files Browse the repository at this point in the history
…ptime

Add script to extract Synthetics uptimes
  • Loading branch information
nicholas-devlin authored Nov 26, 2024
2 parents 50363b2 + 39c74a2 commit 04f7c2a
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
72 changes: 72 additions & 0 deletions synthetics/uptime-reporting/extract-synthetics-uptimes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import csv
import sys
from datetime import datetime
from time import time

from datadog_api_client import ApiClient, Configuration
from datadog_api_client.v1.api.synthetics_api import (
SyntheticsApi,
SyntheticsFetchUptimesPayload,
)

UPTIME_PERIOD_IN_SECONDS = 60 * 60 * 24 * 30 # 1 month

configuration = Configuration()
## https://github.com/DataDog/datadog-api-client-python?tab=readme-ov-file#authentication
# configuration.api_key["apiKeyAuth"] = "XXX"
# configuration.api_key["appKeyAuth"] = "XXX"

with ApiClient(configuration) as api_client:
api_instance = SyntheticsApi(api_client)

print("Fetching tests...")
response = api_instance.list_tests()
print(f"Found {len(response["tests"])} tests, fetching uptimes...")
public_ids = [test["public_id"] for test in response["tests"]]
tests = {test["public_id"]: {"name": test["name"]} for test in response["tests"]}

to_ts_in_seconds = int(time())
from_ts_in_seconds = to_ts_in_seconds - UPTIME_PERIOD_IN_SECONDS
resp = api_instance.fetch_uptimes(
SyntheticsFetchUptimesPayload(
public_ids=list(tests.keys()),
from_ts=from_ts_in_seconds,
to_ts=to_ts_in_seconds,
)
)
for uptime in resp:
public_id = uptime["public_id"]
if uptime["overall"].get("errors"):
tests[public_id]["uptime"] = "No uptime data"
tests[public_id]["alert_periods"] = []
else:
overall_uptime = uptime["overall"]
tests[public_id]["uptime"] = "{:0.2f}%".format(overall_uptime["uptime"])
tests[public_id]["alert_periods"] = []
for i, [timestamp, status] in enumerate(overall_uptime["history"]):
if status == 1: # 1 represents an alert state
alert_start = timestamp
alert_end = (
overall_uptime["history"][i + 1][0]
if i + 1 < len(overall_uptime["history"])
else to_ts_in_seconds
)
format_alert = lambda timestamp: datetime.fromtimestamp(
timestamp
).strftime("%Y-%m-%d %H:%M:%S")
tests[public_id]["alert_periods"].append(
f"FROM {format_alert(alert_start)} TO {format_alert(alert_end)}"
)

output = csv.writer(sys.stdout)
output.writerow(["Test name", "Uptime Percentage", "Alert periods"])
for test in tests.values():
output.writerow(
[
test["name"],
test["uptime"],
";".join(test["alert_periods"])
if test["alert_periods"]
else "No alert periods",
]
)
11 changes: 11 additions & 0 deletions synthetics/uptime-reporting/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Python script to export Synthetics uptimes and alert periods

```sh
# Create a virtual env
python3 -m venv .venv
source .venv/bin/activate
# Install the Datadog Python client
python -m pip install -r requirements.txt
# Run the script
DD_API_KEY="<API KEY>" DD_APP_KEY="<APPLICATION KEY>" python extract-synthetics-uptimes.py
```
1 change: 1 addition & 0 deletions synthetics/uptime-reporting/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
datadog-api-client==2.30.0

0 comments on commit 04f7c2a

Please sign in to comment.