Skip to content

Commit 84fe9f0

Browse files
Fix Datadog log-query to use Logs API instead of Error Tracking API (#1043)
Co-authored-by: openhands <[email protected]>
1 parent 54d982f commit 84fe9f0

File tree

1 file changed

+31
-47
lines changed

1 file changed

+31
-47
lines changed

examples/03_github_workflows/04_datadog_debugging/datadog_debugging.py

Lines changed: 31 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,13 @@ def fetch_datadog_errors(
8181
query: str, working_dir: Path, query_type: str = "log-query", limit: int = 5
8282
) -> Path:
8383
"""
84-
Fetch error examples from Datadog Error Tracking and save to a JSON file.
84+
Fetch error examples from Datadog and save to a JSON file.
8585
8686
Args:
8787
query: Datadog query string (search query or error tracking ID)
8888
working_dir: Directory to save the error examples
89-
query_type: Type of query - "log-query" or "log-error-id"
89+
query_type: Type of query - "log-query" (uses Logs API) or
90+
"log-error-id" (uses Error Tracking API)
9091
limit: Maximum number of error examples to fetch (default: 5)
9192
9293
Returns:
@@ -154,32 +155,27 @@ def fetch_datadog_errors(
154155
error_examples.append(error_example)
155156

156157
else: # log-query
157-
# Use Error Tracking Search API
158-
api_url = f"https://api.{dd_site}/api/v2/error-tracking/issues/search"
158+
api_url = f"https://api.{dd_site}/api/v2/logs/events/search"
159159

160160
# Calculate timestamps (30 days back)
161-
now = int(datetime.now().timestamp() * 1000)
162-
thirty_days_ago = int((datetime.now() - timedelta(days=30)).timestamp() * 1000)
161+
now = datetime.now()
162+
thirty_days_ago = now - timedelta(days=30)
163163

164-
# Build the request body for Error Tracking API
164+
# Build the request body for Logs API
165165
request_body = {
166-
"data": {
167-
"attributes": {
168-
"query": query,
169-
"from": thirty_days_ago,
170-
"to": now,
171-
"track": "logs", # Track errors from logs
172-
},
173-
"type": "search_request",
174-
}
166+
"filter": {
167+
"query": query,
168+
"from": thirty_days_ago.isoformat() + "Z",
169+
"to": now.isoformat() + "Z",
170+
},
171+
"page": {"limit": limit},
172+
"sort": "-timestamp",
175173
}
176174

177-
print(f"📡 Fetching up to {limit} error tracking issues from Datadog...")
175+
print(f"📡 Fetching up to {limit} log entries from Datadog...")
178176
print(f" Query: {query}")
179177
print(f" API: {api_url}")
180178

181-
# Add include parameter to get full issue details
182-
params = {"include": "issue"}
183179
headers = {
184180
"Content-Type": "application/json",
185181
"DD-API-KEY": dd_api_key,
@@ -188,7 +184,7 @@ def fetch_datadog_errors(
188184

189185
try:
190186
response = requests.post(
191-
api_url, params=params, headers=headers, json=request_body, timeout=30
187+
api_url, headers=headers, json=request_body, timeout=30
192188
)
193189
response.raise_for_status()
194190
except requests.exceptions.Timeout:
@@ -210,37 +206,25 @@ def fetch_datadog_errors(
210206
print(f"❌ Datadog API error: {response_data['errors']}")
211207
sys.exit(1)
212208

213-
# Extract and format error tracking issues from search results
214-
search_results = response_data.get("data", [])
215-
included_details = {
216-
item["id"]: item for item in response_data.get("included", [])
217-
}
218-
219-
if search_results:
220-
for idx, search_result in enumerate(search_results[:limit], 1):
221-
issue_id = search_result.get("id")
222-
search_attrs = search_result.get("attributes", {})
209+
# Extract and format log entries
210+
log_entries = response_data.get("data", [])
223211

224-
# Get detailed issue info from included section
225-
issue_details = included_details.get(issue_id, {})
226-
issue_attrs = issue_details.get("attributes", {})
212+
if log_entries:
213+
for idx, log_entry in enumerate(log_entries[:limit], 1):
214+
log_id = log_entry.get("id", "")
215+
log_attrs = log_entry.get("attributes", {})
227216

217+
# Extract relevant fields from log entry
228218
error_example = {
229219
"example_number": idx,
230-
"issue_id": issue_id,
231-
"total_count": search_attrs.get("total_count"),
232-
"impacted_users": search_attrs.get("impacted_users"),
233-
"impacted_sessions": search_attrs.get("impacted_sessions"),
234-
"service": issue_attrs.get("service"),
235-
"error_type": issue_attrs.get("error_type"),
236-
"error_message": issue_attrs.get("error_message", ""),
237-
"file_path": issue_attrs.get("file_path"),
238-
"function_name": issue_attrs.get("function_name"),
239-
"first_seen": issue_attrs.get("first_seen"),
240-
"last_seen": issue_attrs.get("last_seen"),
241-
"state": issue_attrs.get("state"),
242-
"platform": issue_attrs.get("platform"),
243-
"languages": issue_attrs.get("languages", []),
220+
"log_id": log_id,
221+
"service": log_attrs.get("service"),
222+
"host": log_attrs.get("host"),
223+
"message": log_attrs.get("message", ""),
224+
"status": log_attrs.get("status"),
225+
"timestamp": log_attrs.get("timestamp"),
226+
"tags": log_attrs.get("tags", []),
227+
"attributes": log_attrs.get("attributes", {}),
244228
}
245229
error_examples.append(error_example)
246230

0 commit comments

Comments
 (0)