-
Notifications
You must be signed in to change notification settings - Fork 437
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 a set_trace_context method to LogRecord trait #2129
Add a set_trace_context method to LogRecord trait #2129
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2129 +/- ##
=====================================
Coverage 79.0% 79.0%
=====================================
Files 121 121
Lines 20856 20945 +89
=====================================
+ Hits 16482 16561 +79
- Misses 4374 4384 +10 ☔ View full report in Codecov by Sentry. |
opentelemetry/src/logs/record.rs
Outdated
@@ -29,6 +32,9 @@ pub trait LogRecord { | |||
/// Sets the message body of the log. | |||
fn set_body(&mut self, body: AnyValue); | |||
|
|||
/// Sets the trace context of the log. | |||
fn set_trace_context(&mut self, trace_context: TraceContext); |
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.
Will providing a method (say):
fn set_trace_context(&mut self, trace_id: TraceId, span_id: SpanId, trace_flags: Option<TraceFlags>);
prevent moving the TraceContext
to the API, and keep API surface minimal?
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.
We wouldn’t be able to do this without making that method conditional on #[feature = “trace”]
, since that’s where TraceId
etc are defined.
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.
Do you foresee any concerns with making it conditional, as the method serves no purpose without the trace feature enabled? Alternatively, should we pass Context
as a parameter, allowing the user to embed TraceContext
within 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.
Conditional trait methods without a default body are a hazard, but ones with a default body are generally ok.
The issue with non-default conditional trait methods is that the crate you implement it in is not the only crate that can enable features, so you can end up with impossible builds where an unrelated dependency causes you to need to implement a method you didn't have.
Default conditional trait methods are ok, because you only opt-in to implementing them when you have something meaningful to implement. Otherwise you don't have to worry about them. The drawback of this approach compared to non-conditional trait methods with arguments that may be conditionally constructible is that the method itself becomes less reliable. If, for example, you're writing an implementation that forwards to another one there may be some methods that don't forward, because they're using the default that throws the argument away.
If you'd prefer, I can make the method itself conditional and give it an empty default body, then move TraceContext
back into the SDK. That sets out a bit of a precedent for evolving the trait too once it's stable and anything we add to it needs to have a default body.
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.
Thanks for explanation. I agree conditional trait is not good to have.
If you'd prefer, I can make the method itself conditional and give it an empty default body, then move TraceContext back into the SDK. That sets out a bit of a precedent for evolving the trait too once it's stable and anything we add to it needs to have a default body.
Yes, this would be the good approach.
It’s been a full week but I should be able to pick this up again. Are we happy to:
If so, I’ll make that change. |
Thanks @KodrAus . That looks good to me. Just to clear this would also mean - Make TraceId, SpanId, TraceFlags (and possibly few other) not conditional on the |
Ah, I thought making the method conditional on |
Sorry for the confusion. I think this will do as you suggested earlier:
|
80e0efd
to
2814f04
Compare
Ok, I've updated the PR. It's now a much smaller change than it was before. |
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.
LGTM, with a changelog entry.
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.
LGTM. Thanks
Fixes #2106
Changes
This is a breaking change.
This PR moves the
opentelemetry_sdk::logs::TraceContext
intoopentelemetry::logs::TraceContext
so we can add aLogRecord::set_trace_context(&mut self, trace_context: TraceContext)
method.To make this work, I needed to make the
TraceContext
type depend ontrace
APIs, which are only conditionally available. I followed the pattern set bySpanContext
by hiding the fields and exposing them via methods. These methods and theTraceContext
constructor are only available when thetrace
feature is enabled. I thought this was better than making theset_trace_context
method conditional, because conditional trait methods are a bit of a hazard for consumers to deal with.Merge requirement checklist
CHANGELOG.md
files updated for non-trivial, user-facing changes