Function-level tracing SDK for HICAS workers
Project description
sdk-trace
SDK gắn decorator @trace vào hàm Python để tự động thu thập trace và gửi lên hicas-trace-server.
Cài đặt
pip install -e /path/to/sdk-trace
# hoặc sau khi publish:
pip install sdk-trace
Yêu cầu: Python ≥ 3.10
Bắt đầu nhanh
1. Cấu hình (1 lần duy nhất)
Đặt 3 biến môi trường — SDK tự đọc khi import, không cần gọi thêm gì trong code:
export SDK_TRACE_URL=http://localhost:8899
export SDK_TRACE_API_KEY=ht-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
export SDK_TRACE_PROJECT=ten-project
Hoặc trong file .env:
SDK_TRACE_URL=http://localhost:8899
SDK_TRACE_API_KEY=ht-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
SDK_TRACE_PROJECT=ten-project
2. Gắn decorator
from sdk_trace import trace
@trace(deep=True)
def chay_pipeline(task_id: str) -> dict:
data = lay_du_lieu(task_id) # hàm này cũng được trace tự động
result = xu_ly(data) # và hàm này nữa
return result
chay_pipeline("TASK-001")
# → trace tự động gửi lên server, không cần làm gì thêm
3. Xem kết quả
Mở http://localhost:8899 trong trình duyệt.
Các dạng @trace
@trace — trace hàm đơn lẻ
@trace
def tinh_tong(a: int, b: int) -> int:
return a + b
@trace(deep=True) — trace toàn bộ cây gọi hàm
Tự động bắt mọi hàm con trong project mà không cần gắn @trace từng cái:
def _phan_tich(van_ban: str) -> list[str]:
return van_ban.lower().split() # không có @trace
def _dem_tu(tokens: list) -> dict:
freq = {}
for t in tokens:
freq[t] = freq.get(t, 0) + 1
return freq # không có @trace
@trace(deep=True) # ← chỉ cần gắn ở đây
def phan_tich_van_ban(van_ban: str) -> dict:
tokens = _phan_tich(van_ban)
freq = _dem_tu(tokens)
return {"tokens": len(tokens), "freq": freq}
@trace với nhãn tuỳ chọn
@trace(label="bước 1: chuẩn hoá dữ liệu")
def chuan_hoa(df):
...
Hàm async
@trace(deep=True)
async def xu_ly_async(doc_id: str) -> dict:
data = await fetch_doc(doc_id)
return analyze(data)
Tham số @trace
| Tham số | Kiểu | Mặc định | Mô tả |
|---|---|---|---|
deep |
bool |
False |
Tự bắt mọi hàm con trong project |
label |
str | None |
None |
Nhãn hiển thị thêm trong UI |
exclude |
set[str] |
None |
Tên hàm muốn ẩn hoàn toàn (khi deep=True) |
max_calls_per_site |
int | None |
None |
Giới hạn lần log trong vòng lặp |
Ví dụ kiểm soát vòng lặp
Khi deep=True bắt gặp vòng lặp gọi cùng 1 hàm nhiều lần, dùng max_calls_per_site để tránh phình trace:
@trace(
deep=True,
max_calls_per_site=3, # log chi tiết 3 lần đầu, sau đó gộp: "⋯ +997 lần gọi lặp lại"
exclude={"_log", "_validate_schema"}, # ẩn hẳn 2 helper này
)
def xu_ly_batch(items: list) -> list:
return [_xu_ly_mot(item) for item in items] # 1000 items → vẫn trace được
Lồng nhiều hàm có @trace
Khi nhiều hàm đều có @trace, chỉ hàm ngoài cùng (top-level) flush lên server:
@trace
def buoc_1(x): return x * 2
@trace
def buoc_2(x): return x + 10
@trace
def pipeline(x): # top-level → flush sau khi xong
a = buoc_1(x) # được ghi vào cùng 1 trace
b = buoc_2(a)
return b
pipeline(5) # → 1 trace duy nhất, 3 event
Điều gì được ghi vào trace?
Mỗi lần gọi hàm (event) ghi lại:
| Trường | Nội dung |
|---|---|
func_name / qualname |
Tên hàm |
args |
Snapshot các tham số đầu vào (tên, kiểu, preview, full value) |
result |
Snapshot giá trị trả về |
result_diff |
So sánh kết quả với tham số đầu (tự động) |
exception |
Tên và message exception (nếu có) |
duration_ms |
Thời gian thực thi |
depth / parent |
Vị trí trong cây gọi hàm |
Snapshot ghi preview ngắn + full value (hiển thị qua nút "xem đầy đủ" trong UI):
list→ type, số phần tử, preview 8 phần tử đầu, full 200 phần tửdict→ type, số key, preview 10 key đầu, full 100 keystr→ preview 120 ký tự, full 2000 ký tự- Các kiểu khác →
repr()rút gọn
Cách thủ công (không dùng env vars)
Nếu không muốn dùng env vars, có thể cấu hình và flush thủ công:
from sdk_trace import trace, configure, flush_sync, get_tracer
# Cấu hình 1 lần
configure(
base_url="http://localhost:8899",
api_key="ht-...",
project="my-project",
)
@trace(deep=True)
def pipeline(x):
return process(x)
# Sau khi gọi hàm, flush thủ công nếu không muốn auto-flush
pipeline(data)
# (auto-flush đã chạy rồi vì configure() đã set global client)
# Hoặc dùng async
import asyncio
from sdk_trace import flush
async def main():
pipeline(data)
url = await flush()
print(f"Trace: {url}")
Chạy test
cd sdk-trace
SDK_TRACE_URL=http://localhost:8899 \
SDK_TRACE_API_KEY=ht-... \
SDK_TRACE_PROJECT=test \
python test_sdk.py
Kết quả mong đợi: 16/16 passed 🎉
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sdk_py_trace-0.2.0.tar.gz.
File metadata
- Download URL: sdk_py_trace-0.2.0.tar.gz
- Upload date:
- Size: 28.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3e60fa2f719aa299fac689389eae17ce4b5576b82eee5b0d7031e3948455bff4
|
|
| MD5 |
3935c0e7dd78d8ca29aaf86794130b3d
|
|
| BLAKE2b-256 |
7f06dc6d68d6c372cf85f8fc01a15a178a1ed73345013cf6eecaa4fee9bb4ae2
|
Provenance
The following attestation bundles were made for sdk_py_trace-0.2.0.tar.gz:
Publisher:
publish.yml on ANMCPTools/trace_sdk
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sdk_py_trace-0.2.0.tar.gz -
Subject digest:
3e60fa2f719aa299fac689389eae17ce4b5576b82eee5b0d7031e3948455bff4 - Sigstore transparency entry: 2211568747
- Sigstore integration time:
-
Permalink:
ANMCPTools/trace_sdk@957e5c68be2fb21091c3e25c5a5404583387acc4 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/ANMCPTools
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@957e5c68be2fb21091c3e25c5a5404583387acc4 -
Trigger Event:
release
-
Statement type:
File details
Details for the file sdk_py_trace-0.2.0-py3-none-any.whl.
File metadata
- Download URL: sdk_py_trace-0.2.0-py3-none-any.whl
- Upload date:
- Size: 30.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b8144d2172e59a7efcaf32b49a7dab4f55e1caeb70c389499a9da45cbd07116b
|
|
| MD5 |
24ba730132305dd6e6ec29483ed73d70
|
|
| BLAKE2b-256 |
674d4ec32152629cc955a0e0f822cd964daed415c89d8e057bd92a33dbbe5abd
|
Provenance
The following attestation bundles were made for sdk_py_trace-0.2.0-py3-none-any.whl:
Publisher:
publish.yml on ANMCPTools/trace_sdk
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sdk_py_trace-0.2.0-py3-none-any.whl -
Subject digest:
b8144d2172e59a7efcaf32b49a7dab4f55e1caeb70c389499a9da45cbd07116b - Sigstore transparency entry: 2211568813
- Sigstore integration time:
-
Permalink:
ANMCPTools/trace_sdk@957e5c68be2fb21091c3e25c5a5404583387acc4 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/ANMCPTools
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@957e5c68be2fb21091c3e25c5a5404583387acc4 -
Trigger Event:
release
-
Statement type: