Skip to content

Commit ec05d45

Browse files
authored
chore: move instrumentation code out to its own package (#19062)
1 parent edbba3d commit ec05d45

File tree

19 files changed

+160
-1981
lines changed

19 files changed

+160
-1981
lines changed
Lines changed: 11 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,13 @@
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
96
)
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
1811
)
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
Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1 @@
1-
from abc import ABC, abstractmethod
2-
3-
4-
class BaseInstrumentationHandler(ABC):
5-
@classmethod
6-
@abstractmethod
7-
def init(cls) -> None:
8-
"""Initialize the instrumentation handler."""
1+
from llama_index_instrumentation.base import BaseInstrumentationHandler # noqa

0 commit comments

Comments
 (0)