Repository navigation
Add new_trace parameter to logfire.instrument - #1499
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
| tagged = logfire.with_tags('test_instrument') | ||
|
|
||
| def context_factory(): | ||
| return {'new_contex': 123} |
There was a problem hiding this comment.
We didn't discuss this context factory API, I'm not sure if I like it or understand the point. It's certainly not being tested here, new_contex doesn't do anything or appear in the exported spans.
There was a problem hiding this comment.
I didn't mention it in the original issue because it wasn't something I'd considered. But when I realized I'd just be using attach_context behind the scenes, it seemed preferable to allow users the option to provide input for attach_context.
If you're against it, it's not that useful for me and I don't mind cutting it. But if it's worth keeping I'll add better testing for it.
alexmojaki
left a comment
There was a problem hiding this comment.
this feature isn't really compelling enough without the span link
I'll look at adding it, just trying to determine the best approach. |
|
If you'd still like me to review this, please ensure that all checks are passing. |
Merge with main branch
|
Sorry, I didn't have time to finish the required tests. I have changed the API to be what we previously discussed, with new_context being simply True / False. I agree with you that this makes the most sense. |
new_trace parameter to logfire.instrument
There was a problem hiding this comment.
Pull request overview
This PR adds a new_trace parameter to the instrument decorator, enabling instrumented functions to start a new trace instead of being a child of the current span. When new_trace=True, the function creates a new trace with a span link to the parent span, effectively breaking the parent-child relationship while maintaining traceability.
Key Changes
- Added
new_traceboolean parameter to@instrument()decorator - New traces include a span link to the previous span context for traceability
- Implementation uses OpenTelemetry's context manipulation to detach from parent spans
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| tests/test_logfire.py | Added comprehensive test cases covering new_trace functionality with parent spans, no parent spans, without argument extraction, and with selective argument extraction |
| logfire/_internal/main.py | Updated _fast_span, _instrument_span_with_args, and instrument method signatures to accept and propagate new_trace parameter via **kwargs |
| logfire/_internal/instrument.py | Implemented core new_trace logic using OpenTelemetry trace context management, creating span links and detaching from parent context when enabled |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@njz-cvm does this look good to you? |
|
Looks great to me, this is a much cleaner implementation. |
As per #1493, it's current a bit awkward to remove context from instrumentation. This PR adds the "new_context" keyword to ensure designated function calls do not inherit from parent spans.
Here is an example from the new test:
Alternatively, new_context can be set to
True, which is the same asnew_context=dict.