|
1 | | -import inspect |
2 | | -from abc import ABC |
3 | | -from typing import Any, List |
4 | | - |
5 | | -from llama_index.core.instrumentation.dispatcher import ( |
6 | | - Dispatcher, |
7 | | - Manager, |
8 | | - DISPATCHER_SPAN_DECORATED_ATTR, |
| 1 | +from llama_index_instrumentation import ( |
| 2 | + DispatcherSpanMixin, # noqa |
| 3 | + get_dispatcher, # noqa |
| 4 | + root_dispatcher, # noqa |
| 5 | + root_manager, # noqa |
9 | 6 | ) |
10 | | -from llama_index.core.instrumentation.event_handlers import NullEventHandler |
11 | | -from llama_index.core.instrumentation.span_handlers import NullSpanHandler |
12 | | - |
13 | | -root_dispatcher: Dispatcher = Dispatcher( |
14 | | - name="root", |
15 | | - event_handlers=[NullEventHandler()], |
16 | | - span_handlers=[NullSpanHandler()], |
17 | | - propagate=False, |
| 7 | +from llama_index_instrumentation.dispatcher import ( |
| 8 | + DISPATCHER_SPAN_DECORATED_ATTR, # noqa |
| 9 | + Dispatcher, # noqa |
| 10 | + Manager, # noqa |
18 | 11 | ) |
19 | | - |
20 | | -root_manager: Manager = Manager(root_dispatcher) |
21 | | - |
22 | | - |
23 | | -def get_dispatcher(name: str = "root") -> Dispatcher: |
24 | | - """Module method that should be used for creating a new Dispatcher.""" |
25 | | - if name in root_manager.dispatchers: |
26 | | - return root_manager.dispatchers[name] |
27 | | - |
28 | | - candidate_parent_name = ".".join(name.split(".")[:-1]) |
29 | | - if candidate_parent_name in root_manager.dispatchers: |
30 | | - parent_name = candidate_parent_name |
31 | | - else: |
32 | | - parent_name = "root" |
33 | | - |
34 | | - new_dispatcher = Dispatcher( |
35 | | - name=name, |
36 | | - root_name=root_dispatcher.name, |
37 | | - parent_name=parent_name, |
38 | | - manager=root_manager, |
39 | | - ) |
40 | | - root_manager.add_dispatcher(new_dispatcher) |
41 | | - return new_dispatcher |
42 | | - |
43 | | - |
44 | | -class DispatcherSpanMixin(ABC): |
45 | | - """ |
46 | | - Apply the `dispatcher.span` decorator to implementations of abstract methods, as well |
47 | | - as any methods previously decorated (in any base class) that are being overridden by |
48 | | - a subclass. For example, if class `A` has abstract method `f`, and class `B` inherits |
49 | | - from `A` and provides an implementation of `f`, then `B.f` will be decorated by the mixin. |
50 | | - Furthermore, if `B` has a non-abstract method `g` that is decorated by `dispatcher.span` |
51 | | - and new class `C` inherits from `B` and overrides `g`, then `C.g` will also be decorated |
52 | | - by the mixin. Note that users can still manually apply `dispatcher.span` to the methods |
53 | | - in their custom subclasses without creating duplicate spans because the `dispatcher.span` |
54 | | - decorator should be idempotent. |
55 | | - """ |
56 | | - |
57 | | - def __init_subclass__(cls, **kwargs: Any) -> None: |
58 | | - super().__init_subclass__(**kwargs) |
59 | | - abstract_methods: List[str] = [] |
60 | | - decorated_methods: List[str] = [] |
61 | | - for base_cls in inspect.getmro(cls): |
62 | | - if base_cls is cls: |
63 | | - continue |
64 | | - for attr, method in base_cls.__dict__.items(): |
65 | | - if not callable(method): |
66 | | - continue |
67 | | - if ( |
68 | | - hasattr(method, "__isabstractmethod__") |
69 | | - and method.__isabstractmethod__ |
70 | | - ): |
71 | | - abstract_methods.append(attr) |
72 | | - elif hasattr(method, DISPATCHER_SPAN_DECORATED_ATTR): |
73 | | - decorated_methods.append(attr) |
74 | | - dispatcher = get_dispatcher(cls.__module__) |
75 | | - for attr, method in cls.__dict__.items(): |
76 | | - if ( |
77 | | - not callable(method) |
78 | | - or hasattr(method, "__isabstractmethod__") |
79 | | - and method.__isabstractmethod__ |
80 | | - ): |
81 | | - continue |
82 | | - if attr in abstract_methods or attr in decorated_methods: |
83 | | - setattr(cls, attr, dispatcher.span(method)) |
| 12 | +from llama_index_instrumentation.event_handlers import NullEventHandler # noqa |
| 13 | +from llama_index_instrumentation.span_handlers import NullSpanHandler # noqa |
0 commit comments