Making API Calls
Use the built-in requests library (bundled with the SDK):
def query(self):
host = self.activation_config.get("host")
token = self.activation_config.get("api_token")
response = requests.get(
f"https://{host}/api/v1/health",
headers={"Authorization": f"Bearer {token}"},
verify=True, timeout=30
)
data = response.json()
โ ๏ธ Always set timeout=30. Without it, a hung API blocks the entire EEC โ all extensions stop polling.
Parsing JSON to Metrics
for node in data.get("nodes", []):
dims = {
"device.address": host,
"node.name": node["name"]
}
self.report_metric("my_ext.cpu", node["cpu_pct"], dims)
self.report_metric("my_ext.memory", node["mem_used"], dims)
๐ก Dimensions must match what's in your extension.yaml topology rules โ same keys, same values.
๐ Try it
This load balancer extension has cumulative byte counters but the keys are missing the .count suffix. Fix bytes.in and bytes.out.
extension.yamlYAML
Loading...
Error Handling Pattern
try:
response = requests.get(url, timeout=30)
response.raise_for_status()
data = response.json()
self.report_status(Status(StatusValue.OK))
except requests.RequestException as e:
self.report_dt_event(
title="API Connection Failed",
description=str(e),
event_type="ERROR_EVENT"
)
self.report_status(Status(StatusValue.GENERIC_ERROR, str(e)))
return # Skip metric reporting this cycle
HTTP Status Codes to Handle
| Code | Meaning | Action |
|---|---|---|
200 | Success | Parse response, report metrics |
401 | Unauthorized | Report error event, check token |
403 | Forbidden | Token lacks required permissions |
404 | Not found | Wrong endpoint URL |
429 | Rate limited | Increase polling interval |
500+ | Server error | Retry next cycle, report event |
๐ Try itExercise 2
Add a pool.health gauge metric with unit Percent to this extension.
extension.yamlYAML
Loading...
What's Next
Module 11 โ Add topology and screens to your Python extension.