-
Notifications
You must be signed in to change notification settings - Fork 757
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
StartSpan should not generate active span by default #994
Changes from 13 commits
a07830c
1cdce5c
0ef867d
0481030
a06bdb4
8ab15cf
53d0374
6f1a3e4
95d2722
2e2aea3
cbe3d66
8b62c3a
774ac0a
a79dded
3b77f8c
a26ba0a
407c5ac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,62 +54,54 @@ public TelemetrySpan CurrentSpan | |
} | ||
|
||
/// <summary> | ||
/// Starts span. | ||
/// Starts root span. | ||
/// </summary> | ||
/// <param name="name">Span name.</param> | ||
/// <param name="kind">Kind.</param> | ||
/// <param name="attributes">Initial attributes for the span.</param> | ||
/// <param name="links"> <see cref="Link"/> for the span.</param> | ||
/// <param name="startTime"> Start time for the span.</param> | ||
/// <returns>Span instance.</returns> | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public TelemetrySpan StartSpan(string name, SpanKind kind = SpanKind.Internal) | ||
public TelemetrySpan StartRootSpan(string name, SpanKind kind = SpanKind.Internal, IEnumerable<KeyValuePair<string, object>> attributes = null, IEnumerable<Link> links = null, DateTimeOffset startTime = default) | ||
{ | ||
// TODO: Open Question - should we have both StartSpan and StartActiveSpan? | ||
// Or should we call this method StartActiveSpan? | ||
// This method StartSpan is starting a Span and making it Active. | ||
// OTel spec calls for StartSpan, and StartActiveSpan being separate. | ||
// Need to see if it makes sense for .NET to strictly adhere to it. | ||
// Some discussions in Spec: https://github.com/open-telemetry/opentelemetry-specification/pull/485 | ||
if (!this.ActivitySource.HasListeners()) | ||
{ | ||
return TelemetrySpan.NoopInstance; | ||
} | ||
|
||
var activityKind = this.ConvertToActivityKind(kind); | ||
var activity = this.ActivitySource.StartActivity(name, activityKind); | ||
if (activity == null) | ||
{ | ||
return TelemetrySpan.NoopInstance; | ||
} | ||
|
||
return new TelemetrySpan(activity); | ||
return this.StartSpanHelper(false, name, kind, default, attributes, links, startTime); | ||
} | ||
|
||
/// <summary> | ||
/// Starts span. | ||
/// Starts a span and does not make it as current span. | ||
/// </summary> | ||
/// <param name="name">Span name.</param> | ||
/// <param name="kind">Kind.</param> | ||
/// <param name="parent">Parent for new span.</param> | ||
/// <param name="parentSpan">Parent for new span.</param> | ||
/// <param name="attributes">Initial attributes for the span.</param> | ||
/// <param name="links"> <see cref="Link"/> for the span.</param> | ||
/// <param name="startTime"> Start time for the span.</param> | ||
/// <returns>Span instance.</returns> | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
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. Lets modify the summary to say this will not set the started span as the active one. 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. As per the spec and other languages implementation, starting a span does not make it active, should we call it out explicitly here? 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. Yes. This is important for .NET, as the .NET Activity API's StartActivity is making it active (this is violation of spec). The equivalent SHIM api should be StartActiveSpan. 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. Fixed. |
||
public TelemetrySpan StartSpan(string name, SpanKind kind, in SpanContext parent) | ||
public TelemetrySpan StartSpan(string name, SpanKind kind, in TelemetrySpan parentSpan, IEnumerable<KeyValuePair<string, object>> attributes = null, IEnumerable<Link> links = null, DateTimeOffset startTime = default) | ||
{ | ||
if (!this.ActivitySource.HasListeners()) | ||
{ | ||
return TelemetrySpan.NoopInstance; | ||
} | ||
|
||
var activityKind = this.ConvertToActivityKind(kind); | ||
var activity = this.ActivitySource.StartActivity(name, activityKind, parent.ActivityContext); | ||
if (activity == null) | ||
{ | ||
return TelemetrySpan.NoopInstance; | ||
} | ||
return this.StartSpan(name, kind, parentSpan?.Context ?? default, attributes, links, startTime); | ||
} | ||
|
||
return new TelemetrySpan(activity); | ||
/// <summary> | ||
/// Starts a span and does not make it as current span. | ||
/// </summary> | ||
/// <param name="name">Span name.</param> | ||
/// <param name="kind">Kind.</param> | ||
/// <param name="parentContext">Parent Context for new span.</param> | ||
/// <param name="attributes">Initial attributes for the span.</param> | ||
/// <param name="links"> <see cref="Link"/> for the span.</param> | ||
/// <param name="startTime"> Start time for the span.</param> | ||
/// <returns>Span instance.</returns> | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public TelemetrySpan StartSpan(string name, SpanKind kind = SpanKind.Internal, in SpanContext parentContext = default, IEnumerable<KeyValuePair<string, object>> attributes = null, IEnumerable<Link> links = null, DateTimeOffset startTime = default) | ||
{ | ||
return this.StartSpanHelper(false, name, kind, parentContext, attributes, links, startTime); | ||
} | ||
|
||
/// <summary> | ||
/// Starts span. | ||
/// Starts a span and make it the current active span. | ||
/// </summary> | ||
/// <param name="name">Span name.</param> | ||
/// <param name="kind">Kind.</param> | ||
|
@@ -119,13 +111,13 @@ public TelemetrySpan StartSpan(string name, SpanKind kind, in SpanContext parent | |
/// <param name="startTime"> Start time for the span.</param> | ||
/// <returns>Span instance.</returns> | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public TelemetrySpan StartSpan(string name, SpanKind kind, in TelemetrySpan parentSpan, IEnumerable<KeyValuePair<string, object>> attributes = null, IEnumerable<Link> links = null, DateTimeOffset startTime = default) | ||
public TelemetrySpan StartActiveSpan(string name, SpanKind kind, in TelemetrySpan parentSpan, IEnumerable<KeyValuePair<string, object>> attributes = null, IEnumerable<Link> links = null, DateTimeOffset startTime = default) | ||
{ | ||
return this.StartSpan(name, kind, parentSpan.Context, attributes, links, startTime); | ||
return this.StartActiveSpan(name, kind, parentSpan?.Context ?? default, attributes, links, startTime); | ||
} | ||
|
||
/// <summary> | ||
/// Starts span. | ||
/// Starts a span and make it the current active span. | ||
/// </summary> | ||
/// <param name="name">Span name.</param> | ||
/// <param name="kind">Kind.</param> | ||
|
@@ -135,23 +127,38 @@ public TelemetrySpan StartSpan(string name, SpanKind kind, in TelemetrySpan pare | |
/// <param name="startTime"> Start time for the span.</param> | ||
/// <returns>Span instance.</returns> | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public TelemetrySpan StartSpan(string name, SpanKind kind, in SpanContext parentContext, IEnumerable<KeyValuePair<string, object>> attributes = null, IEnumerable<Link> links = null, DateTimeOffset startTime = default) | ||
public TelemetrySpan StartActiveSpan(string name, SpanKind kind = SpanKind.Internal, in SpanContext parentContext = default, IEnumerable<KeyValuePair<string, object>> attributes = null, IEnumerable<Link> links = null, DateTimeOffset startTime = default) | ||
{ | ||
return this.StartSpanHelper(true, name, kind, parentContext, attributes, links, startTime); | ||
} | ||
|
||
/// <summary> | ||
/// Makes the given span as the current one. | ||
/// </summary> | ||
/// <param name="span">The span to be made current.</param> | ||
/// <returns>The current span.</returns> | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public TelemetrySpan WithSpan(TelemetrySpan span) | ||
{ | ||
span.Activate(); | ||
return span; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
private TelemetrySpan StartSpanHelper(bool isActiveSpan, string name, SpanKind kind, in SpanContext parentContext = default, IEnumerable<KeyValuePair<string, object>> attributes = null, IEnumerable<Link> links = null, DateTimeOffset startTime = default) | ||
{ | ||
if (!this.ActivitySource.HasListeners()) | ||
{ | ||
return TelemetrySpan.NoopInstance; | ||
} | ||
|
||
var activityKind = this.ConvertToActivityKind(kind); | ||
var activityLinks = links?.Select(l => l.ActivityLink); | ||
|
||
IList<ActivityLink> activityLinks = null; | ||
if (links != null && links.Count() > 0) | ||
Activity previousActivity = null; | ||
if (!isActiveSpan) | ||
{ | ||
activityLinks = new List<ActivityLink>(); | ||
foreach (var link in links) | ||
{ | ||
activityLinks.Add(link.ActivityLink); | ||
} | ||
previousActivity = Activity.Current; | ||
} | ||
|
||
// TODO: | ||
|
@@ -164,39 +171,26 @@ public TelemetrySpan StartSpan(string name, SpanKind kind, in SpanContext parent | |
return TelemetrySpan.NoopInstance; | ||
} | ||
|
||
return new TelemetrySpan(activity); | ||
} | ||
if (!isActiveSpan) | ||
{ | ||
Activity.Current = previousActivity; | ||
} | ||
|
||
/// <summary> | ||
/// Makes the given span as the current one. | ||
/// </summary> | ||
/// <param name="span">The span to be made current.</param> | ||
/// <returns>The current span.</returns> | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public TelemetrySpan WithSpan(TelemetrySpan span) | ||
{ | ||
span.Activate(); | ||
return span; | ||
return new TelemetrySpan(activity); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
private ActivityKind ConvertToActivityKind(SpanKind kind) | ||
{ | ||
switch (kind) | ||
return kind switch | ||
{ | ||
case SpanKind.Client: | ||
return ActivityKind.Client; | ||
case SpanKind.Consumer: | ||
return ActivityKind.Consumer; | ||
case SpanKind.Internal: | ||
return ActivityKind.Internal; | ||
case SpanKind.Producer: | ||
return ActivityKind.Producer; | ||
case SpanKind.Server: | ||
return ActivityKind.Server; | ||
default: | ||
return ActivityKind.Internal; | ||
} | ||
SpanKind.Client => ActivityKind.Client, | ||
SpanKind.Consumer => ActivityKind.Consumer, | ||
SpanKind.Internal => ActivityKind.Internal, | ||
SpanKind.Producer => ActivityKind.Producer, | ||
SpanKind.Server => ActivityKind.Server, | ||
_ => ActivityKind.Internal, | ||
}; | ||
} | ||
} | ||
} |
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.
Can you add Tracer unit tests to validate all the scenarios? RootSpan, Start, StartActive? We had tests before, but I got them removed when shifting to Activity. We can now add some tests.