Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

[Observability] feat: add metrics of http response (#18499)

AichiB7Ade750a67ec65
diff --git a/api/extensions/ext_otel.py b/api/extensions/ext_otel.py
index 59ec0d0686a..a2edd832ecd 100644
--- a/api/extensions/ext_otel.py
+++ b/api/extensions/ext_otel.py
@@ -14,7 +14,7 @@ from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExport
from opentelemetry.instrumentation.celery import CeleryInstrumentor
from opentelemetry.instrumentation.flask import FlaskInstrumentor
from opentelemetry.instrumentation.sqlalchemy import SQLAlchemyInstrumentor
-from opentelemetry.metrics import get_meter_provider, set_meter_provider
+from opentelemetry.metrics import get_meter, get_meter_provider, set_meter_provider
from opentelemetry.propagate import set_global_textmap
from opentelemetry.propagators.b3 import B3Format
from opentelemetry.propagators.composite import CompositePropagator
@@ -112,6 +112,11 @@ def is_celery_worker():
def init_flask_instrumentor(app: DifyApp):
+ meter = get_meter("http_metrics", version=dify_config.CURRENT_VERSION)
+ _http_response_counter = meter.create_counter(
+ "http.server.response.count", description="Total number of HTTP responses by status code", unit="{response}"
+ )
+
def response_hook(span: Span, status: str, response_headers: list):
if span and span.is_recording():
if status.startswith("2"):
@@ -119,6 +124,11 @@ def init_flask_instrumentor(app: DifyApp):
else:
span.set_status(StatusCode.ERROR, status)
+ status = status.split(" ")[0]
+ status_code = int(status)
+ status_class = f"{status_code // 100}xx"
+ _http_response_counter.add(1, {"status_code": status_code, "status_class": status_class})
+
instrumentor = FlaskInstrumentor()
if dify_config.DEBUG:
logging.info("Initializing Flask instrumentor")