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 functionality to explicitely specify a span parent #349

Merged
merged 7 commits into from
Oct 9, 2020

Conversation

pyohannes
Copy link
Contributor

@pyohannes pyohannes commented Oct 2, 2020

This PR extends the StartSpanOptions struct by a parent SpanContext. If this is given, it will be used as a parent for a given span.

While I was at it, I cleaned up some things in the SpanContext implementation.

Closes #318.

@pyohannes pyohannes requested a review from a team October 2, 2020 00:34
@@ -33,10 +33,6 @@ namespace trace_api = opentelemetry::trace;
class SpanContext final
{
public:
// An invalid SpanContext.
SpanContext() noexcept
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let's use SpanContext::GetInvalid() as the default way to create an invalid SpanContext. Having both SpanContext() and SpanContext::GetInvalid() returning an invalid SpanContext is somewhat confusing.

Copy link
Contributor

Choose a reason for hiding this comment

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

Explicit delete as SpanContext() = delete;?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call, I made it explicit.

trace_api::TraceId trace_id_;
trace_api::SpanId span_id_;
trace_api::TraceFlags trace_flags_;
bool remote_parent_ = false;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removing the const enables us to use default (and hopefully optimized) copy constructors and assignment operators.

Copy link
Member

Choose a reason for hiding this comment

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

nit: We can bring back const-ness if we delete assignment operator ? const varaibles have its own level of optimization

@codecov
Copy link

codecov bot commented Oct 2, 2020

Codecov Report

Merging #349 into master will increase coverage by 0.01%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #349      +/-   ##
==========================================
+ Coverage   94.71%   94.73%   +0.01%     
==========================================
  Files         154      154              
  Lines        6847     6871      +24     
==========================================
+ Hits         6485     6509      +24     
  Misses        362      362              
Impacted Files Coverage Δ
api/include/opentelemetry/trace/default_span.h 21.42% <ø> (ø)
api/include/opentelemetry/trace/span.h 100.00% <ø> (ø)
api/include/opentelemetry/trace/span_context.h 100.00% <ø> (ø)
api/include/opentelemetry/trace/noop.h 86.20% <100.00%> (+1.02%) ⬆️
api/test/trace/span_context_test.cc 100.00% <100.00%> (ø)
sdk/src/trace/tracer.cc 73.52% <100.00%> (+2.56%) ⬆️
sdk/test/trace/tracer_test.cc 100.00% <100.00%> (ø)
sdk/test/trace/span_data_test.cc 100.00% <0.00%> (ø)

@pyohannes pyohannes changed the title Add functionality to explicitely specify a span parent [WIP] Add functionality to explicitely specify a span parent Oct 2, 2020
//
// This defaults to an invalid span context. In this case, the Span is
// automatically parented to the currently active span.
SpanContext parent = SpanContext::GetInvalid();
Copy link
Contributor

@ThomsonTan ThomsonTan Oct 2, 2020

Choose a reason for hiding this comment

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

Defaults to invalid value may look a little weird, would GetEmpty() be a better name?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was thinking about that, but then decided against introducing an empty span context. In the current terminology, a span context can either be valid or invalid (it has an IsValid method). So SpanContext::GetInvalid()::IsValid() == true I think is more idiomatic than SpanContext::GetEmpty()::IsValid() == true. And here, an invalid span context is used like a null pointer (indicating that no parent span context is set).

But I don't have a very strong opinion on that, I'd be willing to go with GetEmpty() if folks feel that's clearer.

Copy link
Member

Choose a reason for hiding this comment

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

I too don't have strong opinion on this, although having two different methods GetEmpty() and GetInvalid() returning invalid span context is more confusing to me.

@pyohannes pyohannes changed the title [WIP] Add functionality to explicitely specify a span parent Add functionality to explicitely specify a span parent Oct 2, 2020
// this = spn_ctx;
// return *this;
// };
SpanContext &operator=(const SpanContext &ctx) = default;
Copy link
Member

Choose a reason for hiding this comment

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

Span context should be immutable as per specs. Just wondering if we need to have assignment operator ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Having an assignment operator makes it easier and more efficient to specify a parent context in the StartSpanOptions struct:

trace_api::StartSpanOptions options;
options.parent   = span_first->GetContext();

Without an assignment operator, we'd need to resort to a pointer (a heap allocated SpanContext object), which is more inefficient (as it requires a heap allocation/deallocation).

The SpanContext is immutable in the sense, that it has no setters (span id, trace id and flags cannot be changed independently of each other), and the SpanContext assigned to a Span cannot change either.

trace_api::TraceId trace_id_;
trace_api::SpanId span_id_;
trace_api::TraceFlags trace_flags_;
bool remote_parent_ = false;
Copy link
Member

Choose a reason for hiding this comment

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

nit: We can bring back const-ness if we delete assignment operator ? const varaibles have its own level of optimization

//
// This defaults to an invalid span context. In this case, the Span is
// automatically parented to the currently active span.
SpanContext parent = SpanContext::GetInvalid();
Copy link
Member

Choose a reason for hiding this comment

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

I too don't have strong opinion on this, although having two different methods GetEmpty() and GetInvalid() returning invalid span context is more confusing to me.

//
// This defaults to an invalid span context. In this case, the Span is
// automatically parented to the currently active span.
SpanContext parent = SpanContext::GetInvalid();
Copy link
Member

@lalitb lalitb Oct 2, 2020

Choose a reason for hiding this comment

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

Just wondering if it would be more optimized to have SpanContext::GetInvalid() method returning a static instance of invalid context ( using c++11 local atomic static ). Although, Can be part of separate PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That would be more efficient if we'd return a pointer, but as we return by value here this involves a copy anyway, and we wouldn't gain much by having a static singleton.

@pyohannes pyohannes added the pr:please-merge This PR is ready to be merged by a Maintainer (rebased, CI passed, has enough valid approvals, etc.) label Oct 8, 2020
@reyang reyang merged commit 4a1b793 into open-telemetry:master Oct 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr:please-merge This PR is ready to be merged by a Maintainer (rebased, CI passed, has enough valid approvals, etc.)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add functionality to explicitly parent a span from a parent span or parent context
4 participants