-
Notifications
You must be signed in to change notification settings - Fork 87
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
Improve Span Links API #726
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
There are no files selected for viewing
This file contains 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 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 | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -11,9 +11,10 @@ | |||||||||
""" # noqa: D205 | ||||||||||
|
||||||||||
from contextlib import contextmanager | ||||||||||
from typing import Any, Iterator, Mapping | ||||||||||
from typing import Any, Iterator, Mapping, cast | ||||||||||
|
||||||||||
from opentelemetry import context, propagate | ||||||||||
from opentelemetry.trace import Link, Span | ||||||||||
|
||||||||||
# anything that can be used to carry context, e.g. Headers or a dict | ||||||||||
ContextCarrier = Mapping[str, Any] | ||||||||||
|
@@ -70,3 +71,26 @@ | |||||||||
yield | ||||||||||
finally: | ||||||||||
context.attach(old_context) | ||||||||||
|
||||||||||
|
||||||||||
def build_span_link(carrier: ContextCarrier) -> Link: | ||||||||||
"""Build a span link from a context carrier. | ||||||||||
|
||||||||||
This is useful when you want to link a span in a different service to the current span. | ||||||||||
|
||||||||||
```py | ||||||||||
from logfire.propagate import build_span_link, get_context | ||||||||||
|
||||||||||
logfire_context = get_context() | ||||||||||
|
||||||||||
... | ||||||||||
|
||||||||||
# later on in another thread, process or service | ||||||||||
link = build_span_link(logfire_context) | ||||||||||
with logfire.span('process_data', _links=[link]): | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
... | ||||||||||
``` | ||||||||||
""" | ||||||||||
context = propagate.extract(carrier=carrier) | ||||||||||
span = cast(Span, next(iter(context.values()))) | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The context can contain multiple values, e.g. baggage. |
||||||||||
return Link(span.get_span_context()) | ||||||||||
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.
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.
Why wouldn't the user use
with attach_context
here?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 it's attached, then it doesn't make sense to add a span link, does it?
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.
Right, I'm questioning adding (and recommending)
build_span_link
. In this situation it seems more sensible to use a parent-child relationship instead of a span link.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.
I mean, if I change the comment to "later on a task queue", would that be better? Or what you suggest instead?
By the way, I'm not sure
build_span_link
is the best API for this, if webuild_span_context
also works. I don't care which if thespan(..., _links=[...])
accepts one of them.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.
No because regular distributed tracing still seems better.
I honestly don't know when span links are useful in general. I suppose in your bigger example there's already a parent
process_message
. If we assume that for whatever reason we really want to keep that as the parent, then we have to use a link. But then the docs should clarify that so that users only use links when it makes sense.I like that idea. Maybe name it
get_span_context
.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.
You disagree with open-telemetry/opentelemetry-python-contrib#3002 ?
Besides Airflow, I'm not sure who uses them... But I feel they are supposed to be useful when you "trigger" something e.g. you have an endpoint, and you trigger a veryyyy long task somewhere else - you want to be able to close the span from the endpoint, and have a link to this very long task.
👍
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.
Overall, yes. What made you post that?
The endpoint span will still be closed. It'll just have children that starts after it ends, which feels weird but should be harmless.
The endpoint span won't have a link to the task span, only the other way around. Someone looking at the endpoint span has no easy way to find the task span.