Skip to content

Commit

Permalink
Align with spec on ISpanBuilder and ITracer (#133)
Browse files Browse the repository at this point in the history
* Align with spec on ISpanBuilder and ITraces

* add bad args tests on SpanBuilder
  • Loading branch information
Liudmila Molkova authored and SergeyKanzhelev committed Jul 9, 2019
1 parent 843fc3e commit 023e861
Show file tree
Hide file tree
Showing 51 changed files with 1,463 additions and 1,394 deletions.
4 changes: 2 additions & 2 deletions samples/Exporters/TestApplicationInsights.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ internal static object Run()

Stats.ViewManager.RegisterView(VideoSizeView);

using (var scopedTags = tagContextBuilder.BuildScoped())
using (tagContextBuilder.BuildScoped())
{
using (var scopedSpan = spanBuilder.StartScopedSpan())
using (Tracer.WithSpan(spanBuilder.StartSpan()))
{
Tracer.CurrentSpan.AddEvent("Start processing video.");
Thread.Sleep(TimeSpan.FromMilliseconds(10));
Expand Down
40 changes: 20 additions & 20 deletions samples/Exporters/TestHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,29 @@ internal static object Run()
{
Console.WriteLine("Hello World!");

var collector = new DependenciesCollector(new DependenciesCollectorOptions(), Tracer, Samplers.AlwaysSample);

var exporter = new ZipkinTraceExporter(
new ZipkinTraceExporterOptions()
using (new DependenciesCollector(new DependenciesCollectorOptions(), Tracer, Samplers.AlwaysSample))
{
var exporter = new ZipkinTraceExporter(
new ZipkinTraceExporterOptions()
{
Endpoint = new Uri("https://zipkin.azurewebsites.net/api/v2/spans"),
ServiceName = typeof(Program).Assembly.GetName().Name,
},
Tracing.ExportComponent);
exporter.Start();

using (Tracer.WithSpan(Tracer.SpanBuilder("incoming request").SetSampler(Samplers.AlwaysSample).StartSpan()))
{
Endpoint = new Uri("https://zipkin.azurewebsites.net/api/v2/spans"),
ServiceName = typeof(Program).Assembly.GetName().Name,
},
Tracing.ExportComponent);
exporter.Start();

var scope = Tracer.SpanBuilder("incoming request").SetSampler(Samplers.AlwaysSample).StartScopedSpan();

var client = new HttpClient();
var t = client.GetStringAsync("http://bing.com");

t.Wait();

scope.Dispose();
using (var client = new HttpClient())
{
client.GetStringAsync("http://bing.com").GetAwaiter().GetResult();
}
}

Console.ReadLine();
Console.ReadLine();

return null;
return null;
}
}
}
}
4 changes: 2 additions & 2 deletions samples/Exporters/TestRedis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ internal static object Run(string zipkinUri)
var db = connection.GetDatabase();

// 4. Create a scoped span. It will end automatically when using statement ends
using (var scope = tracer.SpanBuilder("Main").StartScopedSpan())
using (tracer.WithSpan(tracer.SpanBuilder("Main").StartSpan()))
{
Console.WriteLine("About to do a busy work");
for (var i = 0; i < 10; i++)
Expand All @@ -84,7 +84,7 @@ private static void DoWork(IDatabase db)
// 7. Start another span. If another span was already started, it'll use that span as the parent span.
// In this example, the main method already started a span, so that'll be the parent span, and this will be
// a child span.
using (var scope = tracer.SpanBuilder("DoWork").StartScopedSpan())
using (tracer.WithSpan(tracer.SpanBuilder("DoWork").StartSpan()))
{
// Simulate some work.
var span = tracer.CurrentSpan;
Expand Down
4 changes: 2 additions & 2 deletions samples/Exporters/TestStackdriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ internal static object Run(string projectId)

Stats.ViewManager.RegisterView(VideoSizeView);

using (var scopedTags = tagContextBuilder.BuildScoped())
using (tagContextBuilder.BuildScoped())
{
using (var scopedSpan = spanBuilder.StartScopedSpan())
using (Tracer.WithSpan(spanBuilder.StartSpan()))
{
Tracer.CurrentSpan.AddEvent("Processing video.");
Thread.Sleep(TimeSpan.FromMilliseconds(10));
Expand Down
4 changes: 2 additions & 2 deletions samples/Exporters/TestZipkin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ internal static object Run(string zipkinUri)
var tracer = Tracing.Tracer;

// 4. Create a scoped span. It will end automatically when using statement ends
using (var scope = tracer.SpanBuilder("Main").StartScopedSpan())
using (tracer.WithSpan(tracer.SpanBuilder("Main").StartSpan()))
{
Console.WriteLine("About to do a busy work");
for (var i = 0; i < 10; i++)
Expand All @@ -73,7 +73,7 @@ private static void DoWork(int i)
// 7. Start another span. If another span was already started, it'll use that span as the parent span.
// In this example, the main method already started a span, so that'll be the parent span, and this will be
// a child span.
using (var scope = tracer.SpanBuilder("DoWork").StartScopedSpan())
using (tracer.WithSpan(tracer.SpanBuilder("DoWork").StartSpan()))
{
// Simulate some work.
var span = tracer.CurrentSpan;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ public SpanContext Extract<T>(T carrier, Func<T, string, IEnumerable<string>> ge
if (traceparentCollection.Count() > 1)
{
// multiple traceparent are not allowed
return null;
return SpanContext.Blank;
}

var traceparent = traceparentCollection?.FirstOrDefault();
var traceparentParsed = this.TryExtractTraceparent(traceparent, out var traceId, out var spanId, out var traceoptions);

if (!traceparentParsed)
{
return null;
return SpanContext.Blank;
}

var tracestateResult = Tracestate.Empty;
Expand Down Expand Up @@ -159,7 +159,7 @@ public SpanContext Extract<T>(T carrier, Func<T, string, IEnumerable<string>> ge
}

// in case of exception indicate to upstream that there is no parseable context from the top
return null;
return SpanContext.Blank;
}

/// <inheritdoc/>
Expand Down
169 changes: 169 additions & 0 deletions src/OpenTelemetry.Abstractions/Internal/BlankSpan.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
// <copyright file="BlankSpan.cs" company="OpenTelemetry Authors">
// Copyright 2018, 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>

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

/// <summary>
/// Blank span.
/// </summary>
public sealed class BlankSpan : ISpan
{
/// <summary>
/// Blank span instance.
/// </summary>
public static readonly BlankSpan Instance = new BlankSpan();

private BlankSpan()
{
}

/// <inheritdoc />
public SpanContext Context => SpanContext.Blank;

/// <inheritdoc />
public bool IsRecordingEvents => false;

/// <inheritdoc />
public Status Status
{
get => Status.Ok;

set
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
}
}

/// <inheritdoc />
public bool HasEnded { get; }

/// <inheritdoc />
public void UpdateName(string name)
{
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
}

/// <inheritdoc />
public void SetAttribute(string key, IAttributeValue value)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}

if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
}

/// <inheritdoc />
public void SetAttribute(string key, string value)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}

if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
}

/// <inheritdoc />
public void SetAttribute(string key, long value)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
}

/// <inheritdoc />
public void SetAttribute(string key, double value)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
}

/// <inheritdoc />
public void SetAttribute(string key, bool value)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
}

/// <inheritdoc />
public void AddEvent(string name)
{
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
}

/// <inheritdoc />
public void AddEvent(string name, IDictionary<string, IAttributeValue> attributes)
{
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}

if (attributes == null)
{
throw new ArgumentNullException(nameof(attributes));
}
}

/// <inheritdoc />
public void AddEvent(IEvent newEvent)
{
if (newEvent == null)
{
throw new ArgumentNullException(nameof(newEvent));
}
}

/// <inheritdoc />
public void AddLink(ILink link)
{
if (link == null)
{
throw new ArgumentNullException(nameof(link));
}
}

/// <inheritdoc />
public void End()
{
}
}
}
6 changes: 3 additions & 3 deletions src/OpenTelemetry.Abstractions/Trace/ISampler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ public interface ISampler
/// <param name="name"> Name of a span to be created. Note, that the name of the span is settable.
/// So this name can be changed later and <see cref="ISampler"/> implementation should assume that.
/// Typical example of a name change is when <see cref="ISpan"/> representing incoming http request
/// has a name of url path and then being updated with route name when rouing complete.
/// has a name of url path and then being updated with route name when routing complete.
/// </param>
/// <param name="parentLinks">Links associated with the parent span.</param>
/// <param name="links">Links associated with the span.</param>
/// <returns>True of span needs to be created. False otherwise.</returns>
bool ShouldSample(SpanContext parentContext, TraceId traceId, SpanId spanId, string name, IEnumerable<ISpan> parentLinks);
bool ShouldSample(SpanContext parentContext, TraceId traceId, SpanId spanId, string name, IEnumerable<ILink> links);
}
}
4 changes: 2 additions & 2 deletions src/OpenTelemetry.Abstractions/Trace/ISpan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace OpenTelemetry.Trace

/// <summary>
/// <para>Span represents the execution of the certain span of code or span of time between two events which is part of
/// a distributed trace and has result of execution, context of executuion and other properties.</para>
/// a distributed trace and has result of execution, context of execution and other properties.</para>
///
/// <para>This class is mostly write only. Span should not be used to exchange information. Only to add properties
/// to it for monitoring purposes. It will be converted to SpanData that is readable.</para>
Expand Down Expand Up @@ -103,7 +103,7 @@ public interface ISpan
/// Adds a single <see cref="IEvent"/> with the <see cref="IDictionary{String, IAttributeValue}"/> attributes to the <see cref="ISpan"/>.
/// </summary>
/// <param name="name">Event name.</param>
/// <param name="attributes"><see cref="IDictionary{String, IAttributeValue}"/> of attributes name/value pairs associted with the <see cref="IEvent"/>.</param>
/// <param name="attributes"><see cref="IDictionary{String, IAttributeValue}"/> of attributes name/value pairs associated with the <see cref="IEvent"/>.</param>
void AddEvent(string name, IDictionary<string, IAttributeValue> attributes);

/// <summary>
Expand Down
Loading

0 comments on commit 023e861

Please sign in to comment.