ray-llm
61 строка · 1.9 Кб
1import os2import secrets3import socket4
5from opentelemetry import (6trace,7)
8from opentelemetry.instrumentation.aiohttp_client import AioHttpClientInstrumentor9from opentelemetry.instrumentation.botocore import BotocoreInstrumentor10from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor11from opentelemetry.instrumentation.redis import RedisInstrumentor12from opentelemetry.sdk.resources import Resource13from opentelemetry.sdk.trace import TracerProvider14from opentelemetry.util._once import Once15
16from rayllm.backend.observability.tracing.baggage_span_processor import (17BaggageSpanProcessor,18)
19from rayllm.backend.observability.tracing.fastapi import FastAPIInstrumentor20from rayllm.backend.observability.tracing.threading_propagator import (21ThreadPoolExecutorInstrumentor,22)
23
24has_setup_tracing = Once()25
26
27def _setup_tracing():28tracer_provider = TracerProvider(29resource=Resource(30attributes={31"service.name": "aviary_endpoints",32"meta.local_hostname": socket.gethostname(),33"meta.process_id": os.getpid(),34"meta.process_unique_id": secrets.token_urlsafe(),35}36),37)38
39tracer_provider.add_span_processor( # type: ignore40# Ensure we set baggage entries as attributes on all spans41BaggageSpanProcessor()42)43
44trace.set_tracer_provider(tracer_provider)45
46# Not really effective right now, still need to `instrument_app` each app47FastAPIInstrumentor().instrument()48
49AioHttpClientInstrumentor().instrument()50BotocoreInstrumentor().instrument()51HTTPXClientInstrumentor().instrument()52ThreadPoolExecutorInstrumentor().instrument()53RedisInstrumentor().instrument()54
55tracer = trace.get_tracer(__name__)56with tracer.start_as_current_span("startup"):57pass58
59
60def setup_tracing() -> None:61has_setup_tracing.do_once(_setup_tracing)62