feat(agent): Add trace for agent (#1407)

This commit is contained in:
Fangyin Cheng
2024-04-11 19:07:06 +08:00
committed by GitHub
parent 7d6dfd9ea8
commit aea575e0b4
19 changed files with 1126 additions and 89 deletions

View File

@@ -5,8 +5,10 @@ Manages the lifecycle and registration of components.
from __future__ import annotations
import asyncio
import atexit
import logging
import sys
import threading
from abc import ABC, abstractmethod
from enum import Enum
from typing import TYPE_CHECKING, Dict, Optional, Type, TypeVar, Union
@@ -162,6 +164,8 @@ class SystemApp(LifeCycle):
] = {} # Dictionary to store registered components.
self._asgi_app = asgi_app
self._app_config = app_config or AppConfig()
self._stop_event = threading.Event()
self._stop_event.clear()
self._build()
@property
@@ -273,11 +277,14 @@ class SystemApp(LifeCycle):
def before_stop(self):
"""Invoke the before_stop hooks for all registered components."""
if self._stop_event.is_set():
return
for _, v in self.components.items():
try:
v.before_stop()
except Exception as e:
pass
self._stop_event.set()
async def async_before_stop(self):
"""Asynchronously invoke the before_stop hooks for all registered components."""
@@ -287,6 +294,7 @@ class SystemApp(LifeCycle):
def _build(self):
"""Integrate lifecycle events with the internal ASGI app if available."""
if not self.app:
self._register_exit_handler()
return
@self.app.on_event("startup")
@@ -308,3 +316,7 @@ class SystemApp(LifeCycle):
"""ASGI app shutdown event handler."""
await self.async_before_stop()
self.before_stop()
def _register_exit_handler(self):
"""Register an exit handler to stop the system app."""
atexit.register(self.before_stop)