mirror of
https://git.mirrors.martin98.com/https://github.com/langgenius/dify.git
synced 2025-08-19 18:29:09 +08:00
[Observability] feat: add metrics of http response (#18499)
This commit is contained in:
parent
8e6ea4d117
commit
de750a67ec
@ -14,7 +14,7 @@ from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExport
|
|||||||
from opentelemetry.instrumentation.celery import CeleryInstrumentor
|
from opentelemetry.instrumentation.celery import CeleryInstrumentor
|
||||||
from opentelemetry.instrumentation.flask import FlaskInstrumentor
|
from opentelemetry.instrumentation.flask import FlaskInstrumentor
|
||||||
from opentelemetry.instrumentation.sqlalchemy import SQLAlchemyInstrumentor
|
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.propagate import set_global_textmap
|
||||||
from opentelemetry.propagators.b3 import B3Format
|
from opentelemetry.propagators.b3 import B3Format
|
||||||
from opentelemetry.propagators.composite import CompositePropagator
|
from opentelemetry.propagators.composite import CompositePropagator
|
||||||
@ -112,6 +112,11 @@ def is_celery_worker():
|
|||||||
|
|
||||||
|
|
||||||
def init_flask_instrumentor(app: DifyApp):
|
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):
|
def response_hook(span: Span, status: str, response_headers: list):
|
||||||
if span and span.is_recording():
|
if span and span.is_recording():
|
||||||
if status.startswith("2"):
|
if status.startswith("2"):
|
||||||
@ -119,6 +124,11 @@ def init_flask_instrumentor(app: DifyApp):
|
|||||||
else:
|
else:
|
||||||
span.set_status(StatusCode.ERROR, status)
|
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()
|
instrumentor = FlaskInstrumentor()
|
||||||
if dify_config.DEBUG:
|
if dify_config.DEBUG:
|
||||||
logging.info("Initializing Flask instrumentor")
|
logging.info("Initializing Flask instrumentor")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user