Skip to content

Commit 959cd79

Browse files
committed
Rename module and fcns
1 parent 4da92b2 commit 959cd79

File tree

5 files changed

+66
-49
lines changed

5 files changed

+66
-49
lines changed

docs/examples/patch_abc/langchain_example.py renamed to docs/examples/patch_leaf_subclasses/langchain_example.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from langchain_ollama import OllamaLLM
1010
from langchain_openai import ChatOpenAI
1111

12-
from opentelemetry.util._wrap import patch_abc
12+
from opentelemetry.util._patch import patch_leaf_subclasses
1313

1414

1515
def parse_args():
@@ -66,10 +66,10 @@ def wrapped_fcn(self, *args, **kwargs):
6666
return wrapped_fcn
6767

6868
# Patch traditional LLM models (Ollama, HuggingFace, etc.)
69-
patch_abc(BaseLLM, "_generate", my_wrapper)
69+
patch_leaf_subclasses(BaseLLM, "_generate", my_wrapper)
7070

7171
# Patch chat models (OpenAI, Anthropic, Google, etc.)
72-
patch_abc(BaseChatModel, "_generate", my_wrapper)
72+
patch_leaf_subclasses(BaseChatModel, "_generate", my_wrapper)
7373

7474

7575
def main():

docs/examples/patch_abc/simple_example.py renamed to docs/examples/patch_leaf_subclasses/simple_example.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from abc import ABC, abstractmethod
22

3-
from opentelemetry.util._wrap import patch_abc
3+
from opentelemetry.util._patch import patch_leaf_subclasses
44

55

66
class Greeter(ABC):
@@ -29,7 +29,7 @@ def wrapped_fcn(self, *args, **kwargs):
2929
return wrapped_fcn
3030

3131

32-
patch_abc(Greeter, "greet", my_wrapper)
32+
patch_leaf_subclasses(Greeter, "greet", my_wrapper)
3333

3434
EngishGreeter().greet()
3535
SpanishGreeter().greet()
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
def patch_leaf_subclasses(base_class, method_name, wrapper):
2+
"""
3+
Patches a method on leaf subclasses of a base class.
4+
5+
Args:
6+
base_class: The base class whose leaf subclasses will be patched
7+
method_name: Name of the method to patch
8+
wrapper: Function that wraps the original method
9+
10+
"""
11+
all_subclasses = _get_all_subclasses(base_class)
12+
leaf_subclasses = _get_leaf_subclasses(all_subclasses)
13+
14+
for subclass in leaf_subclasses:
15+
# Patch if the subclass has the method (either defined or inherited)
16+
# and it's actually callable
17+
if hasattr(subclass, method_name) and callable(getattr(subclass, method_name)):
18+
old_method = getattr(subclass, method_name)
19+
setattr(subclass, method_name, wrapper(old_method))
20+
21+
# This implementation does not work if the instrumented class is imported after the instrumentor runs.
22+
23+
24+
def _get_leaf_subclasses(all_subclasses):
25+
"""
26+
Returns only the leaf classes (classes with no subclasses) from a set of classes.
27+
28+
Args:
29+
all_subclasses: Set of classes to filter
30+
31+
Returns:
32+
set: Classes that have no subclasses within the provided set
33+
"""
34+
leaf_classes = set()
35+
for cls in all_subclasses:
36+
# A class is a leaf if no other class in the set is its subclass
37+
is_leaf = True
38+
for other_cls in all_subclasses:
39+
if other_cls != cls and issubclass(other_cls, cls):
40+
is_leaf = False
41+
break
42+
if is_leaf:
43+
leaf_classes.add(cls)
44+
return leaf_classes
45+
46+
47+
def _get_all_subclasses(cls):
48+
"""
49+
Gets all subclasses of a given class.
50+
51+
Args:
52+
cls: The base class to find subclasses for
53+
54+
Returns:
55+
set: All subclasses (direct and indirect) of the given class
56+
"""
57+
subclasses = set()
58+
for subclass in cls.__subclasses__():
59+
subclasses.add(subclass)
60+
subclasses.update(_get_all_subclasses(subclass))
61+
return subclasses

opentelemetry-api/src/opentelemetry/util/_wrap.py

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)