-
Notifications
You must be signed in to change notification settings - Fork 772
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
Leverage ActivityListener.AutoGenerateRootContextTraceId #1007
Merged
cijothomas
merged 8 commits into
open-telemetry:master
from
cijothomas:cijothomas/samplingtraceidtobecreatedfix
Aug 5, 2020
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4ef16a9
Leverage ActivityListener.AutoGenerateRootContextTraceId
cijothomas a78dbf0
test
cijothomas 65ffaed
More test
cijothomas bd2b67c
changelog
cijothomas 75c3e2a
Merge branch 'master' into cijothomas/samplingtraceidtobecreatedfix
cijothomas b486437
remove duplicated test logic
cijothomas 3319a83
markdown fix
cijothomas 8234059
space
cijothomas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 0 additions & 110 deletions
110
test/OpenTelemetry.Tests/Implementation/Trace/ActivityListenerSdkTest.cs
This file was deleted.
Oops, something went wrong.
157 changes: 157 additions & 0 deletions
157
test/OpenTelemetry.Tests/Implementation/Trace/TraceSdkTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
// <copyright file="TraceSdkTest.cs" company="OpenTelemetry Authors"> | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// </copyright> | ||
|
||
using System.Diagnostics; | ||
using OpenTelemetry; | ||
using OpenTelemetry.Trace; | ||
using Xunit; | ||
|
||
namespace OpenTelemetry.Tests.Implementation.Trace | ||
{ | ||
public class TraceSdkTest | ||
{ | ||
private const string ActivitySourceName = "TraceSdkTest"; | ||
|
||
[Fact] | ||
public void TracerSdkInvokesSamplingWithCorrectParameters() | ||
{ | ||
var testSampler = new TestSampler(); | ||
using var activitySource = new ActivitySource(ActivitySourceName); | ||
using var sdk = Sdk.CreateTracerProvider( | ||
(tpbuilder) => | ||
{ | ||
tpbuilder.AddActivitySource(ActivitySourceName); | ||
tpbuilder.SetSampler(testSampler); | ||
}); | ||
|
||
// OpenTelemetry Sdk is expected to set default to W3C. | ||
Assert.True(Activity.DefaultIdFormat == ActivityIdFormat.W3C); | ||
|
||
using (var rootActivity = activitySource.StartActivity("root")) | ||
{ | ||
Assert.NotNull(rootActivity); | ||
|
||
// TODO: Follow up with .NET on why ParentSpanId is != default here. | ||
// Assert.True(rootActivity.ParentSpanId == default); | ||
|
||
// Validate that the TraceId seen by Sampler is same as the | ||
// Activity when it got created. | ||
Assert.Equal(rootActivity.TraceId, testSampler.LatestSamplingParameters.TraceId); | ||
} | ||
|
||
using (var parent = activitySource.StartActivity("parent", ActivityKind.Client)) | ||
{ | ||
Assert.Equal(parent.TraceId, testSampler.LatestSamplingParameters.TraceId); | ||
using (var child = activitySource.StartActivity("child")) | ||
{ | ||
Assert.Equal(child.TraceId, testSampler.LatestSamplingParameters.TraceId); | ||
Assert.Equal(parent.TraceId, child.TraceId); | ||
Assert.Equal(parent.SpanId, child.ParentSpanId); | ||
} | ||
} | ||
|
||
var customContext = new ActivityContext( | ||
ActivityTraceId.CreateRandom(), | ||
ActivitySpanId.CreateRandom(), | ||
ActivityTraceFlags.None); | ||
|
||
using (var fromCustomContext = | ||
activitySource.StartActivity("customContext", ActivityKind.Client, customContext)) | ||
{ | ||
Assert.Equal(fromCustomContext.TraceId, testSampler.LatestSamplingParameters.TraceId); | ||
Assert.Equal(customContext.TraceId, fromCustomContext.TraceId); | ||
Assert.Equal(customContext.SpanId, fromCustomContext.ParentSpanId); | ||
Assert.NotEqual(customContext.SpanId, fromCustomContext.SpanId); | ||
} | ||
|
||
// Validate that when StartActivity is called using Parent as string, | ||
// Sampling is called correctly. | ||
var act = new Activity("anything").Start(); | ||
act.Stop(); | ||
var customContextAsString = act.Id; | ||
var expectedTraceId = act.TraceId; | ||
var expectedParentSpanId = act.SpanId; | ||
|
||
using (var fromCustomContextAsString = | ||
activitySource.StartActivity("customContext", ActivityKind.Client, customContextAsString)) | ||
{ | ||
Assert.Equal(fromCustomContextAsString.TraceId, testSampler.LatestSamplingParameters.TraceId); | ||
Assert.Equal(expectedTraceId, fromCustomContextAsString.TraceId); | ||
Assert.Equal(expectedParentSpanId, fromCustomContextAsString.ParentSpanId); | ||
} | ||
|
||
using (var fromInvalidW3CIdParent = | ||
activitySource.StartActivity("customContext", ActivityKind.Client, "InvalidW3CIdParent")) | ||
{ | ||
// OpenTelemetry ActivityContext does not support | ||
// non W3C Ids. Starting activity with non W3C Ids | ||
// will result in no activity being created. | ||
Assert.Null(fromInvalidW3CIdParent); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void TracerSdkSetsActivityDataRequestBasedOnSamplingDecision() | ||
{ | ||
var testSampler = new TestSampler(); | ||
using var activitySource = new ActivitySource(ActivitySourceName); | ||
using var sdk = Sdk.CreateTracerProvider( | ||
(tpbuilder) => | ||
{ | ||
tpbuilder.AddActivitySource(ActivitySourceName); | ||
tpbuilder.SetSampler(testSampler); | ||
}); | ||
|
||
testSampler.DesiredSamplingResult = new SamplingResult(true); | ||
using (var activity = activitySource.StartActivity("root")) | ||
{ | ||
Assert.NotNull(activity); | ||
Assert.True(activity.IsAllDataRequested); | ||
Assert.True(activity.Recorded); | ||
} | ||
|
||
testSampler.DesiredSamplingResult = new SamplingResult(false); | ||
using (var activity = activitySource.StartActivity("root")) | ||
{ | ||
// Even if sampling returns false, for root activities, | ||
// activity is still created with PropagationOnly. | ||
Assert.NotNull(activity); | ||
Assert.False(activity.IsAllDataRequested); | ||
Assert.False(activity.Recorded); | ||
|
||
using (var innerActivity = activitySource.StartActivity("inner")) | ||
{ | ||
// This is not a root activity. | ||
// If sampling returns false, no activity is created at all. | ||
Assert.Null(innerActivity); | ||
} | ||
} | ||
} | ||
|
||
private class TestSampler : Sampler | ||
{ | ||
public SamplingResult DesiredSamplingResult { get; set; } = new SamplingResult(true); | ||
|
||
public SamplingParameters LatestSamplingParameters { get; private set; } | ||
|
||
public override SamplingResult ShouldSample(in SamplingParameters samplingParameters) | ||
{ | ||
this.LatestSamplingParameters = samplingParameters; | ||
return this.DesiredSamplingResult; | ||
} | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I guess we need an empty line here.
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.
Your favourite tool caught it as well!
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.
🚨