Buckets:

download
raw
3.77 kB
# Copyright (c) 2025-2026 Yutaro Maeda. All rights reserved.
# Licensed under the Apache License 2.0. See LICENSE file for details.
"""Datadog integration for FaultRay -- events and custom metrics."""
from __future__ import annotations
import logging
import time
import httpx
logger = logging.getLogger(__name__)
class DatadogClient:
"""Client for Datadog Events and Metrics APIs."""
def __init__(
self,
api_key: str,
app_key: str = "",
base_url: str = "https://api.datadoghq.com",
) -> None:
self.api_key = api_key
self.app_key = app_key
self.base_url = base_url
def _headers(self) -> dict[str, str]:
headers = {
"DD-API-KEY": self.api_key,
"Content-Type": "application/json",
}
if self.app_key:
headers["DD-APPLICATION-KEY"] = self.app_key
return headers
async def send_event(
self,
title: str,
text: str,
alert_type: str = "info",
tags: list[str] | None = None,
) -> dict:
"""Send a simulation event to Datadog (POST /api/v1/events)."""
async with httpx.AsyncClient() as client:
resp = await client.post(
f"{self.base_url}/api/v1/events",
headers=self._headers(),
json={
"title": title,
"text": text,
"alert_type": alert_type,
"tags": tags or ["source:faultray"],
"source_type_name": "faultray",
},
timeout=10.0,
)
resp.raise_for_status()
return resp.json()
async def submit_metrics(
self,
resilience_score: float,
security_score: float = 0.0,
risk_exposure: float = 0.0,
tags: list[str] | None = None,
) -> dict:
"""Submit custom metrics to Datadog (POST /api/v2/series)."""
now = int(time.time())
metric_tags = tags or ["source:faultray"]
series = [
{
"metric": "faultray.resilience_score",
"type": 3, # gauge
"points": [{"timestamp": now, "value": resilience_score}],
"tags": metric_tags,
},
{
"metric": "faultray.security_score",
"type": 3,
"points": [{"timestamp": now, "value": security_score}],
"tags": metric_tags,
},
{
"metric": "faultray.risk_exposure",
"type": 3,
"points": [{"timestamp": now, "value": risk_exposure}],
"tags": metric_tags,
},
]
async with httpx.AsyncClient() as client:
resp = await client.post(
f"{self.base_url}/api/v2/series",
headers=self._headers(),
json={"series": series},
timeout=10.0,
)
resp.raise_for_status()
return resp.json()
async def fetch_metrics(
self,
query: str,
from_ts: int = 0,
to_ts: int = 0,
) -> dict:
"""Fetch metrics from Datadog (GET /api/v1/query)."""
now = int(time.time())
if not from_ts:
from_ts = now - 3600
if not to_ts:
to_ts = now
async with httpx.AsyncClient() as client:
resp = await client.get(
f"{self.base_url}/api/v1/query",
headers=self._headers(),
params={"from": from_ts, "to": to_ts, "query": query},
timeout=10.0,
)
resp.raise_for_status()
return resp.json()

Xet Storage Details

Size:
3.77 kB
·
Xet hash:
c3d158a3b24c84b11a82773cbdf496c90a5f307c3523e1d3dceb10ef76e605cc

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.