-
Notifications
You must be signed in to change notification settings - Fork 376
[Feature] Enable Tracing Mechanism (Phase 1) #1068
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Mustafa974
wants to merge
20
commits into
LazyAGI:main
Choose a base branch
from
Mustafa974:hlt/tracing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
fd708f6
init tracing base settings
Mustafa974 5af772c
delete redundant codes
Mustafa974 4b56154
fix lint
Mustafa974 931591b
add doc for hook class
Mustafa974 29b4d5a
fix doc issue
Mustafa974 9db373e
refine dependencies and config add
Mustafa974 9b83091
refine basic tracing fwk
Mustafa974 3e65cde
Merge branch 'main' into hlt/tracing
Mustafa974 8118405
fix lint
Mustafa974 cd39e66
add docs for tracing
Mustafa974 c429ab5
delete unrelated files
Mustafa974 97386d1
refine gemini's suggestion and add thread lock for config
Mustafa974 5a844e6
add guard for no hook._span_handle
Mustafa974 fe47cb3
fix comments of review agent
Mustafa974 c97e6b9
fix lint
Mustafa974 4b80a6f
fix
Mustafa974 c6bb6d4
fix __init__ issue
Mustafa974 0bab21b
refine trace hook
Mustafa974 7301f8c
add hook provider registry to decouple module/flow and tracing hook
Mustafa974 43beda5
rename report to finalize
Mustafa974 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,10 @@ | ||
| from abc import ABC, abstractmethod | ||
| import inspect | ||
| import ast | ||
| import copy | ||
| from .common import LOG | ||
| from .tracing.runtime import start_span, set_span_output, set_span_error, finish_span | ||
| from .tracing.configs import resolve_default_module_trace | ||
|
|
||
Mustafa974 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| class LazyLLMHook(ABC): | ||
|
|
||
|
|
@@ -16,7 +20,10 @@ def pre_hook(self, *args, **kwargs): | |
| def post_hook(self, output): | ||
| pass | ||
|
|
||
| def report(): # This is not an abstract method, but it is required to be implemented in subclasses. | ||
| def on_error(self, exc): | ||
| return None | ||
|
|
||
| def report(self): # This is not an abstract method, but it is required to be implemented in subclasses. | ||
| raise NotImplementedError | ||
|
|
||
|
|
||
|
|
@@ -58,3 +65,61 @@ def post_hook(self, output): | |
| try: | ||
| self._generator.send(output) if self._left_count == 1 else next(self._generator) | ||
| except StopIteration: pass | ||
|
|
||
|
|
||
| class LazyTracingHook(LazyLLMHook): | ||
|
||
| def __init__(self, obj): | ||
| self._obj = obj | ||
Mustafa974 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| self._span_handle = None | ||
|
|
||
| @property | ||
| def _span_kind(self): | ||
| return 'flow' if hasattr(self._obj, '_flow_id') else 'module' | ||
|
|
||
| def _enabled(self) -> bool: | ||
| if self._span_kind != 'module': | ||
| return True | ||
| return resolve_default_module_trace( | ||
| module_name=getattr(self._obj, 'name', None) or getattr(self._obj, '_module_name', None), | ||
| module_class=self._obj.__class__, | ||
| ) | ||
|
|
||
| def pre_hook(self, *args, **kwargs): | ||
| if not self._enabled(): | ||
| self._span_handle = None | ||
| return | ||
| self._span_handle = start_span(span_kind=self._span_kind, target=self._obj, args=args, kwargs=kwargs) | ||
|
|
||
Mustafa974 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| def post_hook(self, output): | ||
| set_span_output(self._span_handle, output) | ||
|
|
||
| def on_error(self, exc): | ||
| set_span_error(self._span_handle, exc) | ||
|
|
||
| def report(self): | ||
| finish_span(self._span_handle) | ||
| self._span_handle = None | ||
|
|
||
Mustafa974 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| def _materialize_hook(hook_type, obj): | ||
| if isinstance(hook_type, LazyLLMHook): | ||
| return copy.deepcopy(hook_type) | ||
| assert isinstance(hook_type, type) and issubclass(hook_type, LazyLLMHook), ( | ||
| f'{hook_type} is not a subclass of LazyLLMHook') | ||
| return hook_type(obj) | ||
|
|
||
|
|
||
| def run_pre_hooks(obj, hook_types, hook_objs, *args, raise_on_error: bool, **kwargs): | ||
Mustafa974 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| for hook_type in hook_types: | ||
| hook_obj = _materialize_hook(hook_type, obj) | ||
| try: | ||
| hook_obj.pre_hook(*args, **kwargs) | ||
| except Exception as e: | ||
| hook_obj.report() | ||
| if raise_on_error: | ||
| for active_hook in hook_objs: | ||
| active_hook.report() | ||
| raise | ||
| LOG.warning(f'Hook `{type(hook_obj).__name__}` pre_hook failed and will be skipped: {e}') | ||
| continue | ||
| hook_objs.append(hook_obj) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| from .runtime import ( | ||
| TracingSetupError, | ||
| start_span, | ||
| set_span_output, | ||
| set_span_error, | ||
| finish_span, | ||
| get_trace_context, | ||
| set_trace_context, | ||
| tracing_available, | ||
| ) | ||
| from .configs import ( | ||
| DEFAULT_MODULE_TRACE_CONFIG, | ||
| get_default_module_trace_config, | ||
| set_default_module_trace_config, | ||
| resolve_default_module_trace, | ||
| ) | ||
|
|
||
| __all__ = [ | ||
| 'TracingSetupError', | ||
| 'start_span', | ||
| 'set_span_output', | ||
| 'set_span_error', | ||
| 'finish_span', | ||
| 'get_trace_context', | ||
| 'set_trace_context', | ||
| 'tracing_available', | ||
| 'DEFAULT_MODULE_TRACE_CONFIG', | ||
| 'get_default_module_trace_config', | ||
| 'set_default_module_trace_config', | ||
| 'resolve_default_module_trace', | ||
| ] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.