Skip to content

Commit

Permalink
rename FromCurrentActivity to SetCreateChild
Browse files Browse the repository at this point in the history
  • Loading branch information
lmolkova committed Jul 12, 2019
1 parent f2f2edc commit ed7762f
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 41 deletions.
9 changes: 6 additions & 3 deletions src/OpenTelemetry.Abstractions/Trace/ISpanBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,15 @@ public interface ISpanBuilder
ISpanBuilder SetNoParent();

/// <summary>
/// Instructs builder to use Activity created by library to create a span.
/// The span will represent this Activity and will not be a child of this Activity.
/// Sets flag indicating that new span should become a child of implicit context (Activity.Current)
/// or continue run in this context and inherit it
/// Use this method with value 'false' in auto-collectors when library is instrumented with Activities.
/// Any parent that was set previously will be discarded.
/// </summary>
/// <param name="createChild">If true, a new span will become a child of the existing implicit context.
/// If false, new span will continue this context.</param>
/// <returns>This span builder for chaining.</returns>
ISpanBuilder SetAutoInstrumented();
ISpanBuilder SetCreateChild(bool createChild);

/// <summary>
/// Set <see cref="SpanKind"/> on the span.
Expand Down
34 changes: 19 additions & 15 deletions src/OpenTelemetry.Abstractions/Trace/NoopSpanBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,23 +96,27 @@ public ISpanBuilder SetNoParent()
}

/// <inheritdoc/>
public ISpanBuilder FromCurrentActivity()
public ISpanBuilder SetCreateChild(bool createChild)
{
var currentActivity = Activity.Current;

if (currentActivity == null)
{
throw new ArgumentException("Current Activity cannot be null");
}

if (currentActivity.IdFormat != ActivityIdFormat.W3C)
{
throw new ArgumentException("Current Activity is not in W3C format");
}

if (currentActivity.StartTimeUtc == default || currentActivity.Duration != default)
if (!createChild)
{
throw new ArgumentException("Current Activity is not running: it has not been started or has been stopped");
var currentActivity = Activity.Current;

if (currentActivity == null)
{
throw new ArgumentException("Current Activity cannot be null");
}

if (currentActivity.IdFormat != ActivityIdFormat.W3C)
{
throw new ArgumentException("Current Activity is not in W3C format");
}

if (currentActivity.StartTimeUtc == default || currentActivity.Duration != default)
{
throw new ArgumentException(
"Current Activity is not running: it has not been started or has been stopped");
}
}

return this;
Expand Down
37 changes: 23 additions & 14 deletions src/OpenTelemetry/Trace/SpanBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,27 +113,36 @@ public ISpanBuilder SetNoParent()
}

/// <inheritdoc />
public ISpanBuilder SetAutoInstrumented()
public ISpanBuilder SetCreateChild(bool createChild)
{
var currentActivity = Activity.Current;

if (currentActivity == null)
if (!createChild)
{
throw new ArgumentException("Current Activity cannot be null");
}
var currentActivity = Activity.Current;

if (currentActivity.IdFormat != ActivityIdFormat.W3C)
{
throw new ArgumentException("Current Activity is not in W3C format");
}
if (currentActivity == null)
{
throw new ArgumentException("Current Activity cannot be null");
}

if (currentActivity.IdFormat != ActivityIdFormat.W3C)
{
throw new ArgumentException("Current Activity is not in W3C format");
}

if (currentActivity.StartTimeUtc == default || currentActivity.Duration != default)
if (currentActivity.StartTimeUtc == default || currentActivity.Duration != default)
{
throw new ArgumentException(
"Current Activity is not running: it has not been started or has been stopped");
}

this.fromActivity = currentActivity;
this.contextSource = ContextSource.Activity;
}
else
{
throw new ArgumentException("Current Activity is not running: it has not been started or has been stopped");
this.contextSource = ContextSource.CurrentActivityParent;
}

this.fromActivity = currentActivity;
this.contextSource = ContextSource.Activity;
this.parentSpanContext = null;
this.parentSpanContext = null;
this.parentActivity = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ public void NoopSpanBuilder_BadArguments()
Assert.Throws<ArgumentNullException>(() => spanBuilder.SetParent((Activity)null));

// no Activity.Current
Assert.Throws<ArgumentException>(() => spanBuilder.FromCurrentActivity());
Assert.Throws<ArgumentException>(() => spanBuilder.SetCreateChild(false));

// Activity.Current wrong format
Activity.DefaultIdFormat = ActivityIdFormat.Hierarchical;
Activity.ForceDefaultIdFormat = true;
var a = new Activity("foo").Start(); // TODO SetIdFormat
Assert.Throws<ArgumentException>(() => spanBuilder.FromCurrentActivity());
Assert.Throws<ArgumentException>(() => spanBuilder.SetCreateChild(false));
a.Stop();

Assert.Throws<ArgumentNullException>(() => spanBuilder.SetSampler(null));
Expand Down
14 changes: 7 additions & 7 deletions test/OpenTelemetry.Tests/Impl/Trace/SpanBuilderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public SpanBuilderTest()
configMock.Setup((c) => c.ActiveTraceParams).Returns(alwaysSampleTraceParams);

startEndHandler = Mock.Of<IStartEndHandler>();
tracer = new Tracer(startEndHandler, traceConfig, null);
tracer = new Tracer(startEndHandler, traceConfig);
}

[Fact]
Expand Down Expand Up @@ -191,7 +191,7 @@ public void StartSpanLastParentWins6()

var childSpan = (Span)new SpanBuilder(SpanName, spanBuilderOptions)
.SetParent(spanContext)
.SetAutoInstrumented()
.SetCreateChild(false)
.StartSpan();

Assert.True(childSpan.Context.IsValid);
Expand All @@ -210,7 +210,7 @@ public void StartSpanLastParentWins7()
var activity = new Activity("foo").Start();

var childSpan = (Span)new SpanBuilder(SpanName, spanBuilderOptions)
.SetAutoInstrumented()
.SetCreateChild(false)
.SetParent(spanContext)
.StartSpan();

Expand Down Expand Up @@ -383,7 +383,7 @@ public void StartSpanFromCurrentActivity()

var span = new SpanBuilder(SpanName, spanBuilderOptions)
.SetSpanKind(SpanKind.Internal)
.SetAutoInstrumented()
.SetCreateChild(false)
.StartSpan();

Assert.True(span.Context.IsValid);
Expand All @@ -404,7 +404,7 @@ public void StartSpanFromCurrentRecordedActivity()

var span = new SpanBuilder(SpanName, spanBuilderOptions)
.SetSpanKind(SpanKind.Internal)
.SetAutoInstrumented()
.SetCreateChild(false)
.StartSpan();

Assert.True(span.Context.IsValid);
Expand Down Expand Up @@ -876,13 +876,13 @@ public void SpanBuilder_BadArguments()
Assert.Throws<ArgumentNullException>(() => spanBuilder.SetParent((Activity)null));

// no Activity.Current
Assert.Throws<ArgumentException>(() => spanBuilder.SetAutoInstrumented());
Assert.Throws<ArgumentException>(() => spanBuilder.SetCreateChild(false));

// Activity.Current wrong format
Activity.DefaultIdFormat = ActivityIdFormat.Hierarchical;
Activity.ForceDefaultIdFormat = true;
var a = new Activity("foo").Start(); // TODO SetIdFormat
Assert.Throws<ArgumentException>(() => spanBuilder.SetAutoInstrumented());
Assert.Throws<ArgumentException>(() => spanBuilder.SetCreateChild(false));
a.Stop();

Assert.Throws<ArgumentNullException>(() => spanBuilder.SetSampler(null));
Expand Down

0 comments on commit ed7762f

Please sign in to comment.