feat(core): Support opentelemetry exporter (#1690)

This commit is contained in:
Fangyin Cheng
2024-07-05 15:20:21 +08:00
committed by GitHub
parent 84fc1fc7fe
commit bf978d2bf9
39 changed files with 1176 additions and 218 deletions

View File

@@ -1,4 +1,4 @@
import uuid
import logging
from contextvars import ContextVar
from starlette.middleware.base import BaseHTTPMiddleware
@@ -7,7 +7,11 @@ from starlette.types import ASGIApp
from dbgpt.util.tracer import Tracer, TracerContext
_DEFAULT_EXCLUDE_PATHS = ["/api/controller/heartbeat"]
from .base import _parse_span_id
_DEFAULT_EXCLUDE_PATHS = ["/api/controller/heartbeat", "/api/health"]
logger = logging.getLogger(__name__)
class TraceIDMiddleware(BaseHTTPMiddleware):
@@ -33,11 +37,12 @@ class TraceIDMiddleware(BaseHTTPMiddleware):
):
return await call_next(request)
span_id = request.headers.get("DBGPT_TRACER_SPAN_ID")
# if not span_id:
# span_id = str(uuid.uuid4())
# self.trace_context_var.set(TracerContext(span_id=span_id))
# Read trace_id from request headers
span_id = _parse_span_id(request)
logger.debug(
f"TraceIDMiddleware: span_id={span_id}, path={request.url.path}, "
f"headers={request.headers}"
)
with self.tracer.start_span(
self.root_operation_name, span_id, metadata={"path": request.url.path}
):