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

Update to DiagnosticSource preview 7 and leverage Activity.SetIdFormat() #160

Merged
merged 3 commits into from
Jul 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public class BinaryFormat : IBinaryFormat
private const int TraceIdFieldIdOffset = VersionIdOffset + IdSize;
private const int TraceIdOffset = TraceIdFieldIdOffset + IdSize;
private const byte SpanIdFieldId = 1;
private const int SpaneIdFieldIdOffset = TraceIdOffset + TraceIdSize;
private const int SpanIdOffset = SpaneIdFieldIdOffset + IdSize;
private const int SpanIdFieldIdOffset = TraceIdOffset + TraceIdSize;
private const int SpanIdOffset = SpanIdFieldIdOffset + IdSize;
private const byte TraceOptionsFieldId = 2;
private const int TraceOptionFieldIdOffset = SpanIdOffset + SpanIdSize;
private const int TraceOptionOffset = TraceOptionFieldIdOffset + IdSize;
Expand Down Expand Up @@ -96,11 +96,11 @@ public byte[] ToByteArray(SpanContext spanContext)
Span<byte> spanBytes = stackalloc byte[FormatLength];
spanBytes[VersionIdOffset] = VersionId;
spanBytes[TraceIdFieldIdOffset] = TraceIdFieldId;
spanBytes[SpaneIdFieldIdOffset] = SpanIdFieldId;
spanBytes[SpanIdFieldIdOffset] = SpanIdFieldId;
spanBytes[TraceOptionFieldIdOffset] = TraceOptionsFieldId;
spanBytes[TraceOptionOffset] = (byte)spanContext.TraceOptions;
spanContext.TraceId.CopyTo(spanBytes.Slice(TraceIdOffset));
spanContext.SpanId.CopyTo(spanBytes.Slice(SpanIdOffset));
spanContext.TraceId.CopyTo(spanBytes.Slice(TraceIdOffset, TraceIdSize));
spanContext.SpanId.CopyTo(spanBytes.Slice(SpanIdOffset, SpanIdSize));

return spanBytes.ToArray();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="4.6.0-preview6.19303.8" />
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="4.6.0-preview7.19362.9" />
</ItemGroup>

<ItemGroup>
Expand Down
10 changes: 10 additions & 0 deletions src/OpenTelemetry.Abstractions/Trace/Link.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ private Link(SpanContext context, IDictionary<string, object> attributes)
/// <returns>New <see cref="ILink"/> instance.</returns>
public static ILink FromActivity(Activity activity)
{
if (activity == null)
{
throw new ArgumentNullException(nameof(activity));
}

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

var tracestate = Tracestate.Empty;
var tracestateBuilder = Tracestate.Builder;
if (TracestateUtils.TryExtractTracestate(activity.TraceStateString, tracestateBuilder))
Expand Down
19 changes: 10 additions & 9 deletions src/OpenTelemetry/Trace/SpanBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ namespace OpenTelemetry.Trace
using System.Collections.Generic;
using System.Diagnostics;
using OpenTelemetry.Context.Propagation;
using OpenTelemetry.Internal;
using OpenTelemetry.Trace.Config;

/// <inheritdoc/>
Expand All @@ -41,10 +40,6 @@ public class SpanBuilder : ISpanBuilder

internal SpanBuilder(string name, SpanBuilderOptions options)
{
// TODO: remove with next DiagnosticSource preview, switch to Activity setidformat
Activity.DefaultIdFormat = ActivityIdFormat.W3C;
Activity.ForceDefaultIdFormat = true;

this.name = name ?? throw new ArgumentNullException(nameof(name));
this.options = options ?? throw new ArgumentNullException(nameof(options));
}
Expand Down Expand Up @@ -343,8 +338,10 @@ private Activity CreateActivityForSpan(ContextSource contextSource, ISpan explic
case ContextSource.CurrentActivityParent:
{
// Activity will figure out its parent
spanActivity = new Activity(this.name).Start();

spanActivity = new Activity(this.name)
.SetIdFormat(ActivityIdFormat.W3C)
.Start();

// chances are, Activity.Current has span attached
if (CurrentSpanUtils.CurrentSpan is Span currentSpan)
{
Expand Down Expand Up @@ -372,8 +369,10 @@ private Activity CreateActivityForSpan(ContextSource contextSource, ISpan explic

case ContextSource.NoParent:
{
// TODO fix after next DiagnosticSource preview comes out - this is a hack to force activity to become orphan
spanActivity = new Activity(this.name).SetParentId(" ").Start();
spanActivity = new Activity(this.name)
.SetIdFormat(ActivityIdFormat.W3C)
.SetParentId(" ")
.Start();
this.parentSpanContext = null;
break;
}
Expand All @@ -396,6 +395,7 @@ private Activity CreateActivityForSpan(ContextSource contextSource, ISpan explic
this.parentSpanContext.TraceOptions);
}

spanActivity.SetIdFormat(ActivityIdFormat.W3C);
spanActivity.TraceStateString = this.parentSpanContext.Tracestate.ToString();
spanActivity.Start();

Expand All @@ -412,6 +412,7 @@ private Activity CreateActivityForSpan(ContextSource contextSource, ISpan explic
this.parentSpan.Context.TraceOptions);
}

spanActivity.SetIdFormat(ActivityIdFormat.W3C);
spanActivity.TraceStateString = this.parentSpan.Context.Tracestate.ToString();
spanActivity.Start();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public class CurrentSpanUtilsTest: IDisposable

public CurrentSpanUtilsTest()
{
// TODO: remove with next DiagnosticSource preview, switch to Activity setidformat
Activity.DefaultIdFormat = ActivityIdFormat.W3C;
Activity.ForceDefaultIdFormat = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ public class SpanExporterTest : IDisposable

public SpanExporterTest()
{
// TODO
Activity.DefaultIdFormat = ActivityIdFormat.W3C;
Activity.ForceDefaultIdFormat = true;

startEndHandler = new StartEndHandler(spanExporter, new SimpleEventQueue());
spanExporter.RegisterHandler("test.service", serviceHandler);
}
Expand All @@ -53,6 +49,7 @@ private Span CreateSampledEndedSpan(string spanName)
{
var sampledActivity = new Activity(spanName);
sampledActivity.ActivityTraceFlags |= ActivityTraceFlags.Recorded;
sampledActivity.SetIdFormat(ActivityIdFormat.W3C);
sampledActivity.Start();
var span =
Span.StartSpan(
Expand All @@ -68,6 +65,7 @@ private Span CreateSampledEndedSpan(string spanName)
private Span CreateNotSampledEndedSpan(string spanName)
{
var notSampledActivity = new Activity(spanName);
notSampledActivity.SetIdFormat(ActivityIdFormat.W3C);
notSampledActivity.Start();
var span =
Span.StartSpan(
Expand Down
35 changes: 26 additions & 9 deletions test/OpenTelemetry.Tests/Impl/Trace/LinkTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,22 @@
// limitations under the License.
// </copyright>

using System.Linq;

namespace OpenTelemetry.Trace.Test
{
using System;
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics;
using OpenTelemetry.Utils;
using Xunit;

public class LinkTest
public class LinkTest : IDisposable
{
private readonly IDictionary<string, object> attributesMap = new Dictionary<string, object>();
private readonly SpanContext spanContext;


public LinkTest()
{
// TODO: remove with next DiagnosticSource preview, switch to Activity setidformat
Activity.DefaultIdFormat = ActivityIdFormat.W3C;
Activity.ForceDefaultIdFormat = true;

spanContext = SpanContext.Create(ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom(), ActivityTraceFlags.None, Tracestate.Empty); ;

attributesMap.Add("MyAttributeKey0", "MyStringAttribute");
Expand Down Expand Up @@ -93,7 +88,9 @@ public void Link_ToString()
[Fact]
public void FromSpanContext_FromActivity()
{
var activity = new Activity("foo").Start();
var activity = new Activity("foo")
.SetIdFormat(ActivityIdFormat.W3C)
.Start();
activity.TraceStateString = "k1=v1, k2=v2";

var link = Link.FromActivity(activity);
Expand All @@ -107,5 +104,25 @@ public void FromSpanContext_FromActivity()
Assert.Equal("k2", entries[1].Key);
Assert.Equal("v2", entries[1].Value);
}

[Fact]
public void FromSpanContext_FromNullActivity()
{
Assert.Throws<ArgumentNullException>( () => Link.FromActivity(null));
}

[Fact]
public void FromSpanContext_FromHierarchicalActivity()
{
var activity = new Activity("foo")
.SetIdFormat(ActivityIdFormat.Hierarchical)
.Start();
Assert.Throws<ArgumentException>(() => Link.FromActivity(activity));
}

public void Dispose()
{
Activity.Current = null;
}
}
}
6 changes: 3 additions & 3 deletions test/OpenTelemetry.Tests/Impl/Trace/NoopSpanBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ public void NoopSpanBuilder_BadArguments()
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
var a = new Activity("foo")
.SetIdFormat(ActivityIdFormat.Hierarchical)
.Start();
Assert.Throws<ArgumentException>(() => spanBuilder.SetCreateChild(false));
a.Stop();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ private void TestSpanContextConversion(SpanContext spanContext)
[Fact]
public void Propagate_SpanContextTracingEnabled()
{
TestSpanContextConversion(
SpanContext.Create(TraceId, SpanId, ActivityTraceFlags.Recorded, Tracestate.Empty));
TestSpanContextConversion(SpanContext.Create(TraceId, SpanId, ActivityTraceFlags.Recorded, Tracestate.Empty));
}

[Fact]
Expand Down
53 changes: 35 additions & 18 deletions test/OpenTelemetry.Tests/Impl/Trace/SpanBuilderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ namespace OpenTelemetry.Trace.Test
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Moq;
using OpenTelemetry.Abstractions.Utils;
Expand All @@ -42,10 +41,6 @@ public class SpanBuilderTest : IDisposable
private readonly ITracer tracer;
public SpanBuilderTest()
{
// TODO: remove with next DiagnosticSource preview, switch to Activity setidformat
Activity.DefaultIdFormat = ActivityIdFormat.W3C;
Activity.ForceDefaultIdFormat = true;

// MockitoAnnotations.initMocks(this);
spanBuilderOptions =
new SpanBuilderOptions(startEndHandler, traceConfig);
Expand Down Expand Up @@ -165,7 +160,9 @@ public void StartSpanLastParentWins5()
ActivityTraceId.CreateRandom(),
ActivitySpanId.CreateRandom(),
ActivityTraceFlags.None, Tracestate.Empty);
var activity = new Activity("foo").Start();
var activity = new Activity("foo")
.SetIdFormat(ActivityIdFormat.W3C)
.Start();

var childSpan = (Span)new SpanBuilder(SpanName, spanBuilderOptions)
.SetParent(spanContext)
Expand All @@ -185,7 +182,9 @@ public void StartSpanLastParentWins6()
ActivityTraceId.CreateRandom(),
ActivitySpanId.CreateRandom(),
ActivityTraceFlags.None, Tracestate.Empty);
var activity = new Activity("foo").Start();
var activity = new Activity("foo")
.SetIdFormat(ActivityIdFormat.W3C)
.Start();

var childSpan = (Span)new SpanBuilder(SpanName, spanBuilderOptions)
.SetParent(spanContext)
Expand All @@ -205,7 +204,9 @@ public void StartSpanLastParentWins7()
ActivityTraceId.CreateRandom(),
ActivitySpanId.CreateRandom(),
ActivityTraceFlags.None, Tracestate.Empty);
var activity = new Activity("foo").Start();
var activity = new Activity("foo")
.SetIdFormat(ActivityIdFormat.W3C)
.Start();

var childSpan = (Span)new SpanBuilder(SpanName, spanBuilderOptions)
.SetCreateChild(false)
Expand Down Expand Up @@ -271,7 +272,9 @@ public void StartChildSpan()
[Fact]
public void StartSpanInScopeOfCurrentActivity()
{
var parentActivity = new Activity(SpanName).Start();
var parentActivity = new Activity(SpanName)
.SetIdFormat(ActivityIdFormat.W3C)
.Start();
parentActivity.TraceStateString = "k1=v1,k2=v2";

var childSpan = new SpanBuilder(SpanName, spanBuilderOptions)
Expand All @@ -292,7 +295,9 @@ public void StartSpanInScopeOfCurrentActivity()
[Fact]
public void StartSpanInScopeOfCurrentActivityRecorded()
{
var parentActivity = new Activity(SpanName).Start();
var parentActivity = new Activity(SpanName)
.SetIdFormat(ActivityIdFormat.W3C)
.Start();
parentActivity.ActivityTraceFlags |= ActivityTraceFlags.Recorded;

var childSpan = new SpanBuilder(SpanName, spanBuilderOptions)
Expand Down Expand Up @@ -332,7 +337,9 @@ public void StartSpanInScopeOfCurrentActivityNoParent()
[Fact]
public void StartSpanFromExplicitActivity()
{
var parentActivity = new Activity(SpanName).Start();
var parentActivity = new Activity(SpanName)
.SetIdFormat(ActivityIdFormat.W3C)
.Start();
parentActivity.TraceStateString = "k1=v1,k2=v2";
parentActivity.Stop();

Expand All @@ -357,7 +364,9 @@ public void StartSpanFromExplicitActivity()
[Fact]
public void StartSpanFromExplicitRecordedActivity()
{
var parentActivity = new Activity(SpanName).Start();
var parentActivity = new Activity(SpanName)
.SetIdFormat(ActivityIdFormat.W3C)
.Start();
parentActivity.ActivityTraceFlags |= ActivityTraceFlags.Recorded;
parentActivity.Stop();

Expand All @@ -375,7 +384,9 @@ public void StartSpanFromExplicitRecordedActivity()
[Fact]
public void StartSpanFromCurrentActivity()
{
var activity = new Activity(SpanName).Start();
var activity = new Activity(SpanName)
.SetIdFormat(ActivityIdFormat.W3C)
.Start();
activity.TraceStateString = "k1=v1,k2=v2";

var span = new SpanBuilder(SpanName, spanBuilderOptions)
Expand All @@ -396,7 +407,9 @@ public void StartSpanFromCurrentActivity()
[Fact]
public void StartSpanFromCurrentRecordedActivity()
{
var activity = new Activity(SpanName).Start();
var activity = new Activity(SpanName)
.SetIdFormat(ActivityIdFormat.W3C)
.Start();
activity.ActivityTraceFlags |= ActivityTraceFlags.Recorded;

var span = new SpanBuilder(SpanName, spanBuilderOptions)
Expand Down Expand Up @@ -586,7 +599,9 @@ public void StartSpan_WithLink()
[Fact]
public void StartSpan_WithLinkFromActivity()
{
var activityLink = new Activity("foo").Start();
var activityLink = new Activity("foo")
.SetIdFormat(ActivityIdFormat.W3C)
.Start();
activityLink.Stop();

var span = new SpanBuilder(SpanName, spanBuilderOptions)
Expand All @@ -599,6 +614,8 @@ public void StartSpan_WithLinkFromActivity()

Assert.Single(links);

Assert.NotEqual(default, activityLink.TraceId);
Assert.NotEqual(default, activityLink.SpanId);
Assert.Equal(activityLink.TraceId, links[0].Context.TraceId);
Assert.Equal(activityLink.SpanId, links[0].Context.SpanId);
Assert.Equal(activityLink.ActivityTraceFlags, links[0].Context.TraceOptions);
Expand Down Expand Up @@ -869,9 +886,9 @@ public void SpanBuilder_BadArguments()
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
var a = new Activity("foo")
.SetIdFormat(ActivityIdFormat.Hierarchical)
.Start();
Assert.Throws<ArgumentException>(() => spanBuilder.SetCreateChild(false));
a.Stop();

Expand Down
Loading