Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ext_otel.py 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. import atexit
  2. import logging
  3. import os
  4. import platform
  5. import socket
  6. import sys
  7. from typing import Union
  8. import flask
  9. from celery.signals import worker_init # type: ignore
  10. from flask_login import user_loaded_from_request, user_logged_in # type: ignore
  11. from configs import dify_config
  12. from dify_app import DifyApp
  13. from models import Account, EndUser
  14. @user_logged_in.connect
  15. @user_loaded_from_request.connect
  16. def on_user_loaded(_sender, user: Union["Account", "EndUser"]):
  17. if dify_config.ENABLE_OTEL:
  18. from opentelemetry.trace import get_current_span
  19. if user:
  20. try:
  21. current_span = get_current_span()
  22. if isinstance(user, Account) and user.current_tenant_id:
  23. tenant_id = user.current_tenant_id
  24. elif isinstance(user, EndUser):
  25. tenant_id = user.tenant_id
  26. else:
  27. return
  28. if current_span:
  29. current_span.set_attribute("service.tenant.id", tenant_id)
  30. current_span.set_attribute("service.user.id", user.id)
  31. except Exception:
  32. logging.exception("Error setting tenant and user attributes")
  33. pass
  34. def init_app(app: DifyApp):
  35. from opentelemetry.semconv.trace import SpanAttributes
  36. def is_celery_worker():
  37. return "celery" in sys.argv[0].lower()
  38. def instrument_exception_logging():
  39. exception_handler = ExceptionLoggingHandler()
  40. logging.getLogger().addHandler(exception_handler)
  41. def init_flask_instrumentor(app: DifyApp):
  42. meter = get_meter("http_metrics", version=dify_config.CURRENT_VERSION)
  43. _http_response_counter = meter.create_counter(
  44. "http.server.response.count",
  45. description="Total number of HTTP responses by status code, method and target",
  46. unit="{response}",
  47. )
  48. def response_hook(span: Span, status: str, response_headers: list):
  49. if span and span.is_recording():
  50. try:
  51. if status.startswith("2"):
  52. span.set_status(StatusCode.OK)
  53. else:
  54. span.set_status(StatusCode.ERROR, status)
  55. status = status.split(" ")[0]
  56. status_code = int(status)
  57. status_class = f"{status_code // 100}xx"
  58. attributes: dict[str, str | int] = {"status_code": status_code, "status_class": status_class}
  59. request = flask.request
  60. if request and request.url_rule:
  61. attributes[SpanAttributes.HTTP_TARGET] = str(request.url_rule.rule)
  62. if request and request.method:
  63. attributes[SpanAttributes.HTTP_METHOD] = str(request.method)
  64. _http_response_counter.add(1, attributes)
  65. except Exception:
  66. logging.exception("Error setting status and attributes")
  67. pass
  68. instrumentor = FlaskInstrumentor()
  69. if dify_config.DEBUG:
  70. logging.info("Initializing Flask instrumentor")
  71. instrumentor.instrument_app(app, response_hook=response_hook)
  72. def init_sqlalchemy_instrumentor(app: DifyApp):
  73. with app.app_context():
  74. engines = list(app.extensions["sqlalchemy"].engines.values())
  75. SQLAlchemyInstrumentor().instrument(enable_commenter=True, engines=engines)
  76. def setup_context_propagation():
  77. # Configure propagators
  78. set_global_textmap(
  79. CompositePropagator(
  80. [
  81. TraceContextTextMapPropagator(), # W3C trace context
  82. B3Format(), # B3 propagation (used by many systems)
  83. ]
  84. )
  85. )
  86. def shutdown_tracer():
  87. provider = trace.get_tracer_provider()
  88. if hasattr(provider, "force_flush"):
  89. provider.force_flush()
  90. class ExceptionLoggingHandler(logging.Handler):
  91. """Custom logging handler that creates spans for logging.exception() calls"""
  92. def emit(self, record: logging.LogRecord):
  93. try:
  94. if record.exc_info:
  95. tracer = get_tracer_provider().get_tracer("dify.exception.logging")
  96. with tracer.start_as_current_span(
  97. "log.exception",
  98. attributes={
  99. "log.level": record.levelname,
  100. "log.message": record.getMessage(),
  101. "log.logger": record.name,
  102. "log.file.path": record.pathname,
  103. "log.file.line": record.lineno,
  104. },
  105. ) as span:
  106. span.set_status(StatusCode.ERROR)
  107. if record.exc_info[1]:
  108. span.record_exception(record.exc_info[1])
  109. span.set_attribute("exception.message", str(record.exc_info[1]))
  110. if record.exc_info[0]:
  111. span.set_attribute("exception.type", record.exc_info[0].__name__)
  112. except Exception:
  113. pass
  114. from opentelemetry import trace
  115. from opentelemetry.exporter.otlp.proto.grpc.metric_exporter import OTLPMetricExporter as GRPCMetricExporter
  116. from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter as GRPCSpanExporter
  117. from opentelemetry.exporter.otlp.proto.http.metric_exporter import OTLPMetricExporter as HTTPMetricExporter
  118. from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter as HTTPSpanExporter
  119. from opentelemetry.instrumentation.celery import CeleryInstrumentor
  120. from opentelemetry.instrumentation.flask import FlaskInstrumentor
  121. from opentelemetry.instrumentation.sqlalchemy import SQLAlchemyInstrumentor
  122. from opentelemetry.metrics import get_meter, get_meter_provider, set_meter_provider
  123. from opentelemetry.propagate import set_global_textmap
  124. from opentelemetry.propagators.b3 import B3Format
  125. from opentelemetry.propagators.composite import CompositePropagator
  126. from opentelemetry.sdk.metrics import MeterProvider
  127. from opentelemetry.sdk.metrics.export import ConsoleMetricExporter, PeriodicExportingMetricReader
  128. from opentelemetry.sdk.resources import Resource
  129. from opentelemetry.sdk.trace import TracerProvider
  130. from opentelemetry.sdk.trace.export import (
  131. BatchSpanProcessor,
  132. ConsoleSpanExporter,
  133. )
  134. from opentelemetry.sdk.trace.sampling import ParentBasedTraceIdRatio
  135. from opentelemetry.semconv.resource import ResourceAttributes
  136. from opentelemetry.trace import Span, get_tracer_provider, set_tracer_provider
  137. from opentelemetry.trace.propagation.tracecontext import TraceContextTextMapPropagator
  138. from opentelemetry.trace.status import StatusCode
  139. setup_context_propagation()
  140. # Initialize OpenTelemetry
  141. # Follow Semantic Convertions 1.32.0 to define resource attributes
  142. resource = Resource(
  143. attributes={
  144. ResourceAttributes.SERVICE_NAME: dify_config.APPLICATION_NAME,
  145. ResourceAttributes.SERVICE_VERSION: f"dify-{dify_config.CURRENT_VERSION}-{dify_config.COMMIT_SHA}",
  146. ResourceAttributes.PROCESS_PID: os.getpid(),
  147. ResourceAttributes.DEPLOYMENT_ENVIRONMENT: f"{dify_config.DEPLOY_ENV}-{dify_config.EDITION}",
  148. ResourceAttributes.HOST_NAME: socket.gethostname(),
  149. ResourceAttributes.HOST_ARCH: platform.machine(),
  150. "custom.deployment.git_commit": dify_config.COMMIT_SHA,
  151. ResourceAttributes.HOST_ID: platform.node(),
  152. ResourceAttributes.OS_TYPE: platform.system().lower(),
  153. ResourceAttributes.OS_DESCRIPTION: platform.platform(),
  154. ResourceAttributes.OS_VERSION: platform.version(),
  155. }
  156. )
  157. sampler = ParentBasedTraceIdRatio(dify_config.OTEL_SAMPLING_RATE)
  158. provider = TracerProvider(resource=resource, sampler=sampler)
  159. set_tracer_provider(provider)
  160. exporter: Union[GRPCSpanExporter, HTTPSpanExporter, ConsoleSpanExporter]
  161. metric_exporter: Union[GRPCMetricExporter, HTTPMetricExporter, ConsoleMetricExporter]
  162. protocol = (dify_config.OTEL_EXPORTER_OTLP_PROTOCOL or "").lower()
  163. if dify_config.OTEL_EXPORTER_TYPE == "otlp":
  164. if protocol == "grpc":
  165. exporter = GRPCSpanExporter(
  166. endpoint=dify_config.OTLP_BASE_ENDPOINT,
  167. # Header field names must consist of lowercase letters, check RFC7540
  168. headers=(("authorization", f"Bearer {dify_config.OTLP_API_KEY}"),),
  169. insecure=True,
  170. )
  171. metric_exporter = GRPCMetricExporter(
  172. endpoint=dify_config.OTLP_BASE_ENDPOINT,
  173. headers=(("authorization", f"Bearer {dify_config.OTLP_API_KEY}"),),
  174. insecure=True,
  175. )
  176. else:
  177. exporter = HTTPSpanExporter(
  178. endpoint=dify_config.OTLP_BASE_ENDPOINT + "/v1/traces",
  179. headers={"Authorization": f"Bearer {dify_config.OTLP_API_KEY}"},
  180. )
  181. metric_exporter = HTTPMetricExporter(
  182. endpoint=dify_config.OTLP_BASE_ENDPOINT + "/v1/metrics",
  183. headers={"Authorization": f"Bearer {dify_config.OTLP_API_KEY}"},
  184. )
  185. else:
  186. exporter = ConsoleSpanExporter()
  187. metric_exporter = ConsoleMetricExporter()
  188. provider.add_span_processor(
  189. BatchSpanProcessor(
  190. exporter,
  191. max_queue_size=dify_config.OTEL_MAX_QUEUE_SIZE,
  192. schedule_delay_millis=dify_config.OTEL_BATCH_EXPORT_SCHEDULE_DELAY,
  193. max_export_batch_size=dify_config.OTEL_MAX_EXPORT_BATCH_SIZE,
  194. export_timeout_millis=dify_config.OTEL_BATCH_EXPORT_TIMEOUT,
  195. )
  196. )
  197. reader = PeriodicExportingMetricReader(
  198. metric_exporter,
  199. export_interval_millis=dify_config.OTEL_METRIC_EXPORT_INTERVAL,
  200. export_timeout_millis=dify_config.OTEL_METRIC_EXPORT_TIMEOUT,
  201. )
  202. set_meter_provider(MeterProvider(resource=resource, metric_readers=[reader]))
  203. if not is_celery_worker():
  204. init_flask_instrumentor(app)
  205. CeleryInstrumentor(tracer_provider=get_tracer_provider(), meter_provider=get_meter_provider()).instrument()
  206. instrument_exception_logging()
  207. init_sqlalchemy_instrumentor(app)
  208. atexit.register(shutdown_tracer)
  209. def is_enabled():
  210. return dify_config.ENABLE_OTEL
  211. @worker_init.connect(weak=False)
  212. def init_celery_worker(*args, **kwargs):
  213. if dify_config.ENABLE_OTEL:
  214. from opentelemetry.instrumentation.celery import CeleryInstrumentor
  215. from opentelemetry.metrics import get_meter_provider
  216. from opentelemetry.trace import get_tracer_provider
  217. tracer_provider = get_tracer_provider()
  218. metric_provider = get_meter_provider()
  219. if dify_config.DEBUG:
  220. logging.info("Initializing OpenTelemetry for Celery worker")
  221. CeleryInstrumentor(tracer_provider=tracer_provider, meter_provider=metric_provider).instrument()