Skip to content

Commit 8cd6273

Browse files
Fixing the delay in calling traces
1 parent dd43fad commit 8cd6273

1 file changed

Lines changed: 13 additions & 25 deletions

File tree

packages/altimate-code/src/skills/validate/batch_validate.py

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,11 @@
2222
import argparse
2323
import json
2424
import os
25-
import ssl
2625
import sys
27-
import urllib.error
28-
import urllib.request
2926
from datetime import datetime, timezone
3027
from pathlib import Path
3128

29+
import requests
3230
from dotenv import load_dotenv
3331

3432
# ---------------------------------------------------------------------------
@@ -195,31 +193,21 @@ def fetch_traces_by_session_id(session_id):
195193
# ---------------------------------------------------------------------------
196194
# Validation API call
197195
# ---------------------------------------------------------------------------
196+
197+
# Session reuses TCP connection and SSL handshake across all traces.
198+
_SESSION = requests.Session()
199+
200+
198201
def validate_trace(trace_id):
199202
"""Call the validation API for a single trace. Returns (response_dict, http_status)."""
200-
payload = json.dumps({"trace_id": trace_id}).encode("utf-8")
201-
req = urllib.request.Request(
202-
API_URL,
203-
data=payload,
204-
headers={
205-
"Authorization": f"Bearer {API_TOKEN}",
206-
"Content-Type": "application/json",
207-
},
208-
method="POST",
209-
)
210-
ctx = ssl.create_default_context()
211-
212203
try:
213-
with urllib.request.urlopen(req, timeout=300, context=ctx) as resp:
214-
body = json.loads(resp.read().decode("utf-8"))
215-
return body, resp.status
216-
except urllib.error.HTTPError as e:
217-
error_body = e.read().decode("utf-8", errors="replace")
218-
try:
219-
error_json = json.loads(error_body)
220-
except json.JSONDecodeError:
221-
error_json = {"detail": error_body}
222-
return error_json, e.code
204+
resp = _SESSION.post(
205+
API_URL,
206+
json={"trace_id": trace_id},
207+
headers={"Authorization": f"Bearer {API_TOKEN}"},
208+
timeout=300,
209+
)
210+
return resp.json(), resp.status_code
223211
except Exception as e:
224212
return {"detail": str(e)}, 0
225213

0 commit comments

Comments
 (0)