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
Add span_id
attribute to Events (instrumentation)
#12417
Conversation
@@ -365,7 +365,8 @@ def _run_step( | |||
**kwargs: Any, | |||
) -> TaskStepOutput: | |||
"""Execute step.""" | |||
dispatcher.event(AgentRunStepStartEvent()) | |||
span_id = dispatcher.current_span_id | |||
dispatcher.event(AgentRunStepStartEvent(span_id=span_id)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rather than getting the span-id and passing it in, could event()
attach this automatically?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(would make development easier and less error-prone)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is actually why the callback manager used a context manager
with callback_manager.event() as event:
So if the dispatcher needs a similar pattern, we could use that again, or if possible, it just gets handled automatically
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good point. We could maybe try to adopt a context manager approach:
with dispatcher.span_session() as dispatch_event:
dispatch_event()
# perform other logic
dispatch_event()
So, here I think we add span_id
arg to event()
i.e., dispatcher.event(span_id, event)
. Thus, span_id
will added to the event within .event()
. And then make it so that the context manager yields a partial of dispatcher.event(span_id, event)
i.e., dispatcher_event = partial(dispatcher.event, span_id=dispatcher.current_span_id)
# dispatcher.py
def event(event: BaseEvent, span_id: str = ""):
# modify span_id field of event
event.span_id = span_id
...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we already decorated the function, I wonder if we can do something like this
https://stackoverflow.com/questions/12435765/can-a-decorated-function-access-variables-of-the-decorator
It would completely hide this from the implementation (and maintains a cleaner look to the code for readability)
Thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This way, when .event() is called, it can automatically pull the span ID from globals? Maybe? (if the injected global is a function/lambda?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If that doesn't work, then context manager it is (even though its slightly less neat)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For visibility (lol we're discussing via slack on this too -- my b):
I think this will work! Attach the attr to the wrapper which should still be coroutine safe I think. Will give it a shot.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
alright, wrapt
complicated things for me a bit. Resorted to using context manager approach as described previously in this thread.
e07e42c
to
f090642
Compare
91c7efc
to
92144e7
Compare
Description
Event
with theSpan
enclosure where it takes placespan_id
field is added toBaseEvent
current_span_id
todispatcher
which we useasyncio.Lock
to update when working with async funcsdispatcher.current_span_id
and store it into a variable that can be re-used within the scope of the funcSpanDropEvent
that fires if a Span is being dropped due to an encounteredException
.Fixes # (issue)
New Package?
Did I fill in the
tool.llamahub
section in thepyproject.toml
and provide a detailed README.md for my new integration or package?Version Bump?
Did I bump the version in the
pyproject.toml
file of the package I am updating? (Except for thellama-index-core
package)Type of Change
Please delete options that are not relevant.
How Has This Been Tested?
Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration