Skip to content
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

Merged

Conversation

KodrAus
Copy link
Contributor

@KodrAus KodrAus commented Sep 19, 2024

Fixes #2106

Changes

This is a breaking change.

This PR moves the opentelemetry_sdk::logs::TraceContext into opentelemetry::logs::TraceContext so we can add a LogRecord::set_trace_context(&mut self, trace_context: TraceContext) method.

To make this work, I needed to make the TraceContext type depend on trace APIs, which are only conditionally available. I followed the pattern set by SpanContext by hiding the fields and exposing them via methods. These methods and the TraceContext constructor are only available when the trace feature is enabled. I thought this was better than making the set_trace_context method conditional, because conditional trait methods are a bit of a hazard for consumers to deal with.

Merge requirement checklist

  • CONTRIBUTING guidelines followed
  • Unit tests added/updated (if applicable)
  • Appropriate CHANGELOG.md files updated for non-trivial, user-facing changes
  • Changes in public API reviewed (if applicable)

@KodrAus KodrAus requested a review from a team as a code owner September 19, 2024 08:20
Copy link

codecov bot commented Sep 19, 2024

Codecov Report

Attention: Patch coverage is 89.24731% with 10 lines in your changes missing coverage. Please review.

Project coverage is 79.0%. Comparing base (9cbf693) to head (ab1de99).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
opentelemetry/src/logs/record.rs 0.0% 10 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

@@ -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);
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member

@lalitb lalitb Sep 20, 2024

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?

Copy link
Contributor Author

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.

Copy link
Member

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.

@lalitb lalitb self-assigned this Sep 26, 2024
@KodrAus
Copy link
Contributor Author

KodrAus commented Sep 27, 2024

It’s been a full week but I should be able to pick this up again. Are we happy to:

  1. Make this method conditional on the trace feature with an empty default body.
  2. Make it take TraceId, SpanId, and TraceFlags instead of TraceContext so it doesn’t have to be pulled into the core crate.

If so, I’ll make that change.

@lalitb
Copy link
Member

lalitb commented Sep 30, 2024

It’s been a full week but I should be able to pick this up again. Are we happy to:

  1. Make this method conditional on the trace feature with an empty default body.
  2. Make it take TraceId, SpanId, and TraceFlags instead of TraceContext so it doesn’t have to be pulled into the core crate.

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 trace feature.

@KodrAus
Copy link
Contributor Author

KodrAus commented Oct 1, 2024

Just to clear this would also mean - Make TraceId, SpanId, TraceFlags (and possibly few other) not conditional on the trace feature.

Ah, I thought making the method conditional on trace would allow us not to make TraceId et al. non-conditional. If you'd prefer, I can go down that route and make those types non-conditional, and make the method non-conditional too, but still with a default empty body.

@lalitb
Copy link
Member

lalitb commented Oct 3, 2024

Ah, I thought making the method conditional on trace would allow us not to make TraceId et al. non-conditional. If you'd prefer, I can go down that route and make those types non-conditional, and make the method non-conditional too, but still with a default empty body.

Sorry for the confusion. I think this will do as you suggested earlier:

  • Make this method conditional on the trace feature with an empty default body.
  • Make it take TraceId, SpanId, and TraceFlags instead of TraceContext so it doesn’t have to be pulled into the core crate.

@KodrAus KodrAus force-pushed the feature/log-record-trace-context branch 2 times, most recently from 80e0efd to 2814f04 Compare October 5, 2024 12:48
@KodrAus
Copy link
Contributor Author

KodrAus commented Oct 5, 2024

Ok, I've updated the PR. It's now a much smaller change than it was before.

Copy link
Member

@cijothomas cijothomas left a 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.

Copy link
Member

@lalitb lalitb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks

@lalitb lalitb merged commit 6aad3db into open-telemetry:main Oct 6, 2024
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature]: Allow setting TraceContext on opentelemetry::logs::LogRecord
3 participants