mirror of
https://git.mirrors.martin98.com/https://github.com/langgenius/dify.git
synced 2025-06-04 11:14:10 +08:00
[Observability] Add type check and try-except in otel (#20319)
This commit is contained in:
parent
4c46f04d77
commit
2b81b6673f
@ -22,6 +22,7 @@ def on_user_loaded(_sender, user: Union["Account", "EndUser"]):
|
|||||||
from opentelemetry.trace import get_current_span
|
from opentelemetry.trace import get_current_span
|
||||||
|
|
||||||
if user:
|
if user:
|
||||||
|
try:
|
||||||
current_span = get_current_span()
|
current_span = get_current_span()
|
||||||
if isinstance(user, Account) and user.current_tenant_id:
|
if isinstance(user, Account) and user.current_tenant_id:
|
||||||
tenant_id = user.current_tenant_id
|
tenant_id = user.current_tenant_id
|
||||||
@ -32,6 +33,9 @@ def on_user_loaded(_sender, user: Union["Account", "EndUser"]):
|
|||||||
if current_span:
|
if current_span:
|
||||||
current_span.set_attribute("service.tenant.id", tenant_id)
|
current_span.set_attribute("service.tenant.id", tenant_id)
|
||||||
current_span.set_attribute("service.user.id", user.id)
|
current_span.set_attribute("service.user.id", user.id)
|
||||||
|
except Exception:
|
||||||
|
logging.exception("Error setting tenant and user attributes")
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
def init_app(app: DifyApp):
|
def init_app(app: DifyApp):
|
||||||
@ -54,6 +58,7 @@ def init_app(app: DifyApp):
|
|||||||
|
|
||||||
def response_hook(span: Span, status: str, response_headers: list):
|
def response_hook(span: Span, status: str, response_headers: list):
|
||||||
if span and span.is_recording():
|
if span and span.is_recording():
|
||||||
|
try:
|
||||||
if status.startswith("2"):
|
if status.startswith("2"):
|
||||||
span.set_status(StatusCode.OK)
|
span.set_status(StatusCode.OK)
|
||||||
else:
|
else:
|
||||||
@ -69,6 +74,9 @@ def init_app(app: DifyApp):
|
|||||||
if request and request.method:
|
if request and request.method:
|
||||||
attributes[SpanAttributes.HTTP_METHOD] = str(request.method)
|
attributes[SpanAttributes.HTTP_METHOD] = str(request.method)
|
||||||
_http_response_counter.add(1, attributes)
|
_http_response_counter.add(1, attributes)
|
||||||
|
except Exception:
|
||||||
|
logging.exception("Error setting status and attributes")
|
||||||
|
pass
|
||||||
|
|
||||||
instrumentor = FlaskInstrumentor()
|
instrumentor = FlaskInstrumentor()
|
||||||
if dify_config.DEBUG:
|
if dify_config.DEBUG:
|
||||||
@ -99,7 +107,7 @@ def init_app(app: DifyApp):
|
|||||||
class ExceptionLoggingHandler(logging.Handler):
|
class ExceptionLoggingHandler(logging.Handler):
|
||||||
"""Custom logging handler that creates spans for logging.exception() calls"""
|
"""Custom logging handler that creates spans for logging.exception() calls"""
|
||||||
|
|
||||||
def emit(self, record):
|
def emit(self, record: logging.LogRecord):
|
||||||
try:
|
try:
|
||||||
if record.exc_info:
|
if record.exc_info:
|
||||||
tracer = get_tracer_provider().get_tracer("dify.exception.logging")
|
tracer = get_tracer_provider().get_tracer("dify.exception.logging")
|
||||||
@ -114,9 +122,12 @@ def init_app(app: DifyApp):
|
|||||||
},
|
},
|
||||||
) as span:
|
) as span:
|
||||||
span.set_status(StatusCode.ERROR)
|
span.set_status(StatusCode.ERROR)
|
||||||
|
if record.exc_info[1]:
|
||||||
span.record_exception(record.exc_info[1])
|
span.record_exception(record.exc_info[1])
|
||||||
span.set_attribute("exception.type", record.exc_info[0].__name__)
|
|
||||||
span.set_attribute("exception.message", str(record.exc_info[1]))
|
span.set_attribute("exception.message", str(record.exc_info[1]))
|
||||||
|
if record.exc_info[0]:
|
||||||
|
span.set_attribute("exception.type", record.exc_info[0].__name__)
|
||||||
|
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user