mirror of
https://github.com/langgenius/dify.git
synced 2026-02-15 05:04:02 +00:00
Some checks failed
autofix.ci / autofix (push) Has been cancelled
Build and Push API & Web / build (api, DIFY_API_IMAGE_NAME, linux/amd64, build-api-amd64) (push) Has been cancelled
Build and Push API & Web / build (api, DIFY_API_IMAGE_NAME, linux/arm64, build-api-arm64) (push) Has been cancelled
Build and Push API & Web / build (web, DIFY_WEB_IMAGE_NAME, linux/amd64, build-web-amd64) (push) Has been cancelled
Build and Push API & Web / build (web, DIFY_WEB_IMAGE_NAME, linux/arm64, build-web-arm64) (push) Has been cancelled
Build and Push API & Web / create-manifest (api, DIFY_API_IMAGE_NAME, merge-api-images) (push) Has been cancelled
Build and Push API & Web / create-manifest (web, DIFY_WEB_IMAGE_NAME, merge-web-images) (push) Has been cancelled
Main CI Pipeline / Check Changed Files (push) Has been cancelled
Main CI Pipeline / API Tests (push) Has been cancelled
Main CI Pipeline / Web Tests (push) Has been cancelled
Main CI Pipeline / Style Check (push) Has been cancelled
Main CI Pipeline / VDB Tests (push) Has been cancelled
Main CI Pipeline / DB Migration Test (push) Has been cancelled
36 lines
1.0 KiB
Python
36 lines
1.0 KiB
Python
"""Request context for logging - framework agnostic.
|
|
|
|
This module provides request-scoped context variables for logging,
|
|
using Python's contextvars for thread-safe and async-safe storage.
|
|
"""
|
|
|
|
import uuid
|
|
from contextvars import ContextVar
|
|
|
|
_request_id: ContextVar[str] = ContextVar("log_request_id", default="")
|
|
_trace_id: ContextVar[str] = ContextVar("log_trace_id", default="")
|
|
|
|
|
|
def get_request_id() -> str:
|
|
"""Get current request ID (10 hex chars)."""
|
|
return _request_id.get()
|
|
|
|
|
|
def get_trace_id() -> str:
|
|
"""Get fallback trace ID when OTEL is unavailable (32 hex chars)."""
|
|
return _trace_id.get()
|
|
|
|
|
|
def init_request_context() -> None:
|
|
"""Initialize request context. Call at start of each request."""
|
|
req_id = uuid.uuid4().hex[:10]
|
|
trace_id = uuid.uuid5(uuid.NAMESPACE_DNS, req_id).hex
|
|
_request_id.set(req_id)
|
|
_trace_id.set(trace_id)
|
|
|
|
|
|
def clear_request_context() -> None:
|
|
"""Clear request context. Call at end of request (optional)."""
|
|
_request_id.set("")
|
|
_trace_id.set("")
|