Skip to content

Commit

Permalink
a few un=important class moves (open-telemetry#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeyKanzhelev authored Jun 4, 2019
1 parent bca11b8 commit cee7906
Show file tree
Hide file tree
Showing 15 changed files with 42 additions and 54 deletions.
3 changes: 1 addition & 2 deletions src/OpenTelemetry.Abstractions/Trace/Config/TraceParams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ public sealed class TraceParams : ITraceParams
/// Default trace parameters.
/// </summary>
public static readonly ITraceParams Default =
new TraceParams(Samplers.GetProbabilitySampler(DefaultProbability), DefaultSpanMaxNumAttributes, DefaultSpanMaxNumEvents, DefaultSpanMaxNumMessageEvents, DefaultSpanMaxNumLinks);
new TraceParams(Samplers.AlwaysSample, DefaultSpanMaxNumAttributes, DefaultSpanMaxNumEvents, DefaultSpanMaxNumMessageEvents, DefaultSpanMaxNumLinks);

private const double DefaultProbability = 1e-4;
private const int DefaultSpanMaxNumAttributes = 32;
private const int DefaultSpanMaxNumEvents = 32;
private const int DefaultSpanMaxNumMessageEvents = 128;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
// limitations under the License.
// </copyright>

namespace OpenTelemetry.Trace.Sampler
namespace OpenTelemetry.Trace.Sampler.Internal
{
using System.Collections.Generic;

internal class AlwaysSampleSampler : ISampler
internal sealed class AlwaysSampleSampler : ISampler
{
internal AlwaysSampleSampler()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// limitations under the License.
// </copyright>

namespace OpenTelemetry.Trace.Sampler
namespace OpenTelemetry.Trace.Sampler.Internal
{
using System.Collections.Generic;

Expand Down
14 changes: 2 additions & 12 deletions src/OpenTelemetry.Abstractions/Trace/Sampler/Samplers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,11 @@ public sealed class Samplers
/// <summary>
/// Gets the sampler that always sample.
/// </summary>
public static ISampler AlwaysSample { get; } = new AlwaysSampleSampler();
public static ISampler AlwaysSample { get; } = new Internal.AlwaysSampleSampler();

/// <summary>
/// Gets the sampler than never samples.
/// </summary>
public static ISampler NeverSample { get; } = new NeverSampleSampler();

/// <summary>
/// Gets the probability sampler.
/// </summary>
/// <param name="probability">Probability to use.</param>
/// <returns>Sampler that samples with the given probability.</returns>
public static ISampler GetProbabilitySampler(double probability)
{
return ProbabilitySampler.Create(probability);
}
public static ISampler NeverSample { get; } = new Internal.NeverSampleSampler();
}
}
20 changes: 10 additions & 10 deletions src/OpenTelemetry.Abstractions/Trace/SpanExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static class SpanExtensions
/// <returns>Span with populated http method properties.</returns>
public static ISpan PutHttpMethodAttribute(this ISpan span, string method)
{
span.SetAttribute(SpanAttributeConstants.HttpMethodKey, AttributeValue.StringAttributeValue(method));
span.SetAttribute(SpanAttributeConstants.HttpMethodKey, method);
return span;
}

Expand All @@ -43,7 +43,7 @@ public static ISpan PutHttpMethodAttribute(this ISpan span, string method)
/// <returns>Span with populated status code properties.</returns>
public static ISpan PutHttpStatusCodeAttribute(this ISpan span, int statusCode)
{
span.SetAttribute(SpanAttributeConstants.HttpStatusCodeKey, AttributeValue.LongAttributeValue(statusCode));
span.SetAttribute(SpanAttributeConstants.HttpStatusCodeKey, statusCode);
return span;
}

Expand All @@ -58,7 +58,7 @@ public static ISpan PutHttpUserAgentAttribute(this ISpan span, string userAgent)
{
if (!string.IsNullOrWhiteSpace(userAgent))
{
span.SetAttribute(SpanAttributeConstants.HttpUserAgentKey, AttributeValue.StringAttributeValue(userAgent));
span.SetAttribute(SpanAttributeConstants.HttpUserAgentKey, userAgent);
}

return span;
Expand All @@ -76,11 +76,11 @@ public static ISpan PutHttpHostAttribute(this ISpan span, string hostName, int p
{
if (port == 80 || port == 443)
{
span.SetAttribute(SpanAttributeConstants.HttpHostKey, AttributeValue.StringAttributeValue(hostName));
span.SetAttribute(SpanAttributeConstants.HttpHostKey, hostName);
}
else
{
span.SetAttribute(SpanAttributeConstants.HttpHostKey, AttributeValue.StringAttributeValue(hostName + ":" + port));
span.SetAttribute(SpanAttributeConstants.HttpHostKey, hostName + ":" + port);
}

return span;
Expand All @@ -97,7 +97,7 @@ public static ISpan PutHttpRouteAttribute(this ISpan span, string route)
{
if (!string.IsNullOrEmpty(route))
{
span.SetAttribute(SpanAttributeConstants.HttpRouteKey, AttributeValue.StringAttributeValue(route));
span.SetAttribute(SpanAttributeConstants.HttpRouteKey, route);
}

return span;
Expand All @@ -114,7 +114,7 @@ public static ISpan PutHttpRawUrlAttribute(this ISpan span, string rawUrl)
{
if (!string.IsNullOrEmpty(rawUrl))
{
span.SetAttribute(SpanAttributeConstants.HttpUrlKey, AttributeValue.StringAttributeValue(rawUrl));
span.SetAttribute(SpanAttributeConstants.HttpUrlKey, rawUrl);
}

return span;
Expand All @@ -129,7 +129,7 @@ public static ISpan PutHttpRawUrlAttribute(this ISpan span, string rawUrl)
/// <returns>Span with populated path properties.</returns>
public static ISpan PutHttpPathAttribute(this ISpan span, string path)
{
span.SetAttribute(SpanAttributeConstants.HttpPathKey, AttributeValue.StringAttributeValue(path));
span.SetAttribute(SpanAttributeConstants.HttpPathKey, path);
return span;
}

Expand All @@ -142,7 +142,7 @@ public static ISpan PutHttpPathAttribute(this ISpan span, string path)
/// <returns>Span with populated response size properties.</returns>
public static ISpan PutHttpResponseSizeAttribute(this ISpan span, long size)
{
span.SetAttribute(SpanAttributeConstants.HttpResponseSizeKey, AttributeValue.LongAttributeValue(size));
span.SetAttribute(SpanAttributeConstants.HttpResponseSizeKey, size);
return span;
}

Expand All @@ -155,7 +155,7 @@ public static ISpan PutHttpResponseSizeAttribute(this ISpan span, long size)
/// <returns>Span with populated request size properties.</returns>
public static ISpan PutHttpRequestSizeAttribute(this ISpan span, long size)
{
span.SetAttribute(SpanAttributeConstants.HttpRequestSizeKey, AttributeValue.LongAttributeValue(size));
span.SetAttribute(SpanAttributeConstants.HttpRequestSizeKey, size);
return span;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public virtual void OnStopActivity(Activity activity, object payload)

foreach (var tag in activity.Tags)
{
span.SetAttribute(tag.Key, AttributeValue.StringAttributeValue(tag.Value));
span.SetAttribute(tag.Key, tag.Value);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public virtual void OnStopActivity(Activity activity, object payload)

foreach (var tag in activity.Tags)
{
span.SetAttribute(tag.Key, AttributeValue.StringAttributeValue(tag.Value));
span.SetAttribute(tag.Key, tag.Value);
}
}

Expand Down
File renamed without changes.
1 change: 0 additions & 1 deletion src/OpenTelemetry/OpenTelemetry.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
</PackageReference>
</ItemGroup>
<ItemGroup>
<Folder Include="Internal\" />
<Folder Include="Stats\" />
<Folder Include="Tags\" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ namespace OpenTelemetry.Trace.Sampler
using System.Collections.Generic;
using OpenTelemetry.Utils;

internal sealed class ProbabilitySampler : ISampler
public sealed class ProbabilitySampler : ISampler
{
internal ProbabilitySampler(double probability, long idUpperBound)
private ProbabilitySampler(double probability, long idUpperBound)
{
this.Probability = probability;
this.IdUpperBound = idUpperBound;
Expand Down Expand Up @@ -107,7 +107,7 @@ public override int GetHashCode()
return (int)h;
}

internal static ProbabilitySampler Create(double probability)
public static ProbabilitySampler Create(double probability)
{
if (probability < 0.0 || probability > 1.0)
{
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class TraceParamsTest
[Fact]
public void DefaultTraceParams()
{
Assert.Equal(Samplers.GetProbabilitySampler(1e-4), TraceParams.Default.Sampler);
Assert.Equal(Samplers.AlwaysSample, TraceParams.Default.Sampler);
Assert.Equal(32, TraceParams.Default.MaxNumberOfAttributes);
Assert.Equal(32, TraceParams.Default.MaxNumberOfEvents);
Assert.Equal(128, TraceParams.Default.MaxNumberOfMessageEvents);
Expand Down
40 changes: 20 additions & 20 deletions test/OpenTelemetry.Tests/Impl/Trace/Sampler/SamplersTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,80 +108,80 @@ public void NeverSampleSampler_ToString()
[Fact]
public void ProbabilitySampler_OutOfRangeHighProbability()
{
Assert.Throws<ArgumentOutOfRangeException>(() => Samplers.GetProbabilitySampler(1.01));
Assert.Throws<ArgumentOutOfRangeException>(() => ProbabilitySampler.Create(1.01));
}

[Fact]
public void ProbabilitySampler_OutOfRangeLowProbability()
{
Assert.Throws<ArgumentOutOfRangeException>(() => Samplers.GetProbabilitySampler(-0.00001));
Assert.Throws<ArgumentOutOfRangeException>(() => ProbabilitySampler.Create(-0.00001));
}


[Fact]
public void ProbabilitySampler_DifferentProbabilities_NotSampledParent()
{
ISampler neverSample = Samplers.GetProbabilitySampler(0.0);
ISampler neverSample = ProbabilitySampler.Create(0.0);
AssertSamplerSamplesWithProbability(
neverSample, notSampledSpanContext, new List<ISpan>(), 0.0);
ISampler alwaysSample = Samplers.GetProbabilitySampler(1.0);
ISampler alwaysSample = ProbabilitySampler.Create(1.0);
AssertSamplerSamplesWithProbability(
alwaysSample, notSampledSpanContext, new List<ISpan>(), 1.0);
ISampler fiftyPercentSample = Samplers.GetProbabilitySampler(0.5);
ISampler fiftyPercentSample = ProbabilitySampler.Create(0.5);
AssertSamplerSamplesWithProbability(
fiftyPercentSample, notSampledSpanContext, new List<ISpan>(), 0.5);
ISampler twentyPercentSample = Samplers.GetProbabilitySampler(0.2);
ISampler twentyPercentSample = ProbabilitySampler.Create(0.2);
AssertSamplerSamplesWithProbability(
twentyPercentSample, notSampledSpanContext, new List<ISpan>(), 0.2);
ISampler twoThirdsSample = Samplers.GetProbabilitySampler(2.0 / 3.0);
ISampler twoThirdsSample = ProbabilitySampler.Create(2.0 / 3.0);
AssertSamplerSamplesWithProbability(
twoThirdsSample, notSampledSpanContext, new List<ISpan>(), 2.0 / 3.0);
}

[Fact]
public void ProbabilitySampler_DifferentProbabilities_SampledParent()
{
ISampler neverSample = Samplers.GetProbabilitySampler(0.0);
ISampler neverSample = ProbabilitySampler.Create(0.0);
AssertSamplerSamplesWithProbability(
neverSample, sampledSpanContext, new List<ISpan>(), 1.0);
ISampler alwaysSample = Samplers.GetProbabilitySampler(1.0);
ISampler alwaysSample = ProbabilitySampler.Create(1.0);
AssertSamplerSamplesWithProbability(
alwaysSample, sampledSpanContext, new List<ISpan>(), 1.0);
ISampler fiftyPercentSample = Samplers.GetProbabilitySampler(0.5);
ISampler fiftyPercentSample = ProbabilitySampler.Create(0.5);
AssertSamplerSamplesWithProbability(
fiftyPercentSample, sampledSpanContext, new List<ISpan>(), 1.0);
ISampler twentyPercentSample = Samplers.GetProbabilitySampler(0.2);
ISampler twentyPercentSample = ProbabilitySampler.Create(0.2);
AssertSamplerSamplesWithProbability(
twentyPercentSample, sampledSpanContext, new List<ISpan>(), 1.0);
ISampler twoThirdsSample = Samplers.GetProbabilitySampler(2.0 / 3.0);
ISampler twoThirdsSample = ProbabilitySampler.Create(2.0 / 3.0);
AssertSamplerSamplesWithProbability(
twoThirdsSample, sampledSpanContext, new List<ISpan>(), 1.0);
}

[Fact]
public void ProbabilitySampler_DifferentProbabilities_SampledParentLink()
{
ISampler neverSample = Samplers.GetProbabilitySampler(0.0);
ISampler neverSample = ProbabilitySampler.Create(0.0);
AssertSamplerSamplesWithProbability(
neverSample, notSampledSpanContext, new List<ISpan>() { sampledSpan }, 1.0);
ISampler alwaysSample = Samplers.GetProbabilitySampler(1.0);
ISampler alwaysSample = ProbabilitySampler.Create(1.0);
AssertSamplerSamplesWithProbability(
alwaysSample, notSampledSpanContext, new List<ISpan>() { sampledSpan }, 1.0);
ISampler fiftyPercentSample = Samplers.GetProbabilitySampler(0.5);
ISampler fiftyPercentSample = ProbabilitySampler.Create(0.5);
AssertSamplerSamplesWithProbability(
fiftyPercentSample, notSampledSpanContext, new List<ISpan>() { sampledSpan }, 1.0);
ISampler twentyPercentSample = Samplers.GetProbabilitySampler(0.2);
ISampler twentyPercentSample = ProbabilitySampler.Create(0.2);
AssertSamplerSamplesWithProbability(
twentyPercentSample, notSampledSpanContext, new List<ISpan>() { sampledSpan }, 1.0);
ISampler twoThirdsSample = Samplers.GetProbabilitySampler(2.0 / 3.0);
ISampler twoThirdsSample = ProbabilitySampler.Create(2.0 / 3.0);
AssertSamplerSamplesWithProbability(
twoThirdsSample, notSampledSpanContext, new List<ISpan>() { sampledSpan }, 1.0);
}

[Fact]
public void ProbabilitySampler_SampleBasedOnTraceId()
{
ISampler defaultProbability = Samplers.GetProbabilitySampler(0.0001);
ISampler defaultProbability = ProbabilitySampler.Create(0.0001);
// This traceId will not be sampled by the ProbabilitySampler because the first 8 bytes as long
// is not less than probability * Long.MAX_VALUE;
ITraceId notSampledtraceId =
Expand Down Expand Up @@ -247,13 +247,13 @@ public void ProbabilitySampler_SampleBasedOnTraceId()
[Fact]
public void ProbabilitySampler_getDescription()
{
Assert.Equal(String.Format("ProbabilitySampler({0:F6})", 0.5), Samplers.GetProbabilitySampler(0.5).Description);
Assert.Equal(String.Format("ProbabilitySampler({0:F6})", 0.5), ProbabilitySampler.Create(0.5).Description);
}

[Fact]
public void ProbabilitySampler_ToString()
{
var result = Samplers.GetProbabilitySampler(0.5).ToString();
var result = ProbabilitySampler.Create(0.5).ToString();
Assert.Contains($"0{CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator}5", result);
}

Expand Down

0 comments on commit cee7906

Please sign in to comment.