Tracing ממוקד בנקודות חמות
מדריך קצר להתמקדות ב‑Tracing בנקודות בעלות השפעה גבוהה (Hotspots) בבוט וב‑WebApp.
תרשימי זרימה
זרימת bot.update
graph TD
A[bot.update Request] --> B[Process Update]
B --> C[DB Operations]
C --> D[External APIs]
D --> E[Response]
style A fill:#e1f5fe
style E fill:#c8e6c9
מאפיינים מומלצים ל‑Spans (bot.update)
status: ”ok“/“error“user_id_hash: מזהה מוצפןchat_id_hash: מזהה צ’אט מוצפןretry_count: 0–3duration_msupdate_type: message/callback/inline
זרימת web.request
graph LR
A[HTTP Request] --> B[Flask WebApp]
B --> C[Search with Cache]
C --> D[Response]
style A fill:#fff3e0
style D fill:#f3e5f5
מאפיינים מומלצים ל‑Spans (web.request)
http.status_codecomponent: ”flask.webapp“cache_hit: true/falseuser_id_hashmethod: GET/POST/PUT/DELETEpath
טבלת ”היכן עוטפים“
מודול |
פונקציה |
דקורטור |
מאפיינים נרשמים |
סטטוס |
|---|---|---|---|---|
|
|
|
query, results_count, status |
מיושם |
|
|
|
user_id_hash, file_count, status |
מיושם |
|
|
|
platform, code_size, status |
מיושם |
|
|
|
language, code.length, status |
מיושם |
|
|
|
http.status_code, status, duration_ms, retry_count |
מיושם |
|
|
|
token_type, valid, user_role |
בפייפליין |
|
|
|
key, hit/miss, ttl |
בפייפליין |
דוגמת Trace Viewer
לפני השיפור:
[REQUEST] ────────────────────────── 2500ms
└── [HANDLER] ──────────────────── 2400ms
אחרי השיפור:
[bot.update] ────────────────────────── 2500ms
├── [db.get_user_files] ─ 200ms
│ └── attributes: {status: "ok", user_id_hash: "a3f2...", file_count: 5}
├── [search_engine.search] ────── 1800ms
│ └── attributes: {status: "ok", query: "python", results: 42}
└── [integration.share_code] ── 400ms
└── attributes: {status: "ok", platform: "github"}
[web.request] ────────────────────────── 1200ms
├── attributes: {component: "flask.webapp", http.status_code: 200}
└── [search] ─────── 900ms
└── attributes: {cache_hit: true, status: "ok"}
הטמעת דקורטור ושימוש בפונקציות עזר
from observability_instrumentation import traced, start_span, set_current_span_attributes
# שימוש בדקורטור
@traced("search_engine.search")
async def search(query: str):
set_current_span_attributes({
"query": query,
"status": "in_progress"
})
try:
results = await perform_search(query)
set_current_span_attributes({
"results_count": len(results),
"status": "ok"
})
return results
except Exception as e:
set_current_span_attributes({
"status": "error",
"error_message": str(e)
})
raise
# Span ידני
async def complex_operation():
with start_span("custom_operation") as span:
set_current_span_attributes({"step": "processing"})
# ... לוגיקה ...
Best Practices
מתי להוסיף Span: פעולות I/O, לוגיקה עסקית >100ms, נקודות כשל.
מאפיינים חשובים: תמיד status, user_id_hash; לפי הקשר: cache_hit, retry_count, error_message.
טיפ: השתמשו ב‑
set_current_span_attributes()לעדכון מאפיינים לאורך הפעולה.אל תעשה: אל תעטפו פונקציות קטנות (<10ms), אל תכללו מידע רגיש, אל תיצרו spans בתוך לולאות טייטות.