Skip to content
This repository was archived by the owner on Dec 18, 2023. It is now read-only.

Commit 2ce8eed

Browse files
sandeshpSergeyKanzhelev
authored andcommitted
StartScopeSpan out param (#95)
* StartScopeSpan out param * Fact test added for out param case * Improved by declaring the out var inline
1 parent d087855 commit 2ce8eed

File tree

6 files changed

+49
-19
lines changed

6 files changed

+49
-19
lines changed

src/OpenCensus.Abstractions/Trace/ISpanBuilder.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,13 @@ public interface ISpanBuilder
5757
/// <returns>Scoped event to control the scope of span in the context.
5858
/// Dispose to stop the span and disassiciate it from the current context.</returns>
5959
IScope StartScopedSpan();
60+
61+
/// <summary>
62+
/// Starts the span and set it as a current on the current context, setting the out param to current span.
63+
/// </summary>
64+
/// <param name="currentSpan">Current span.</param>
65+
/// <returns>Scoped event to control the scope of span in the context.
66+
/// Dispose to stop the span and disassiciate it from the current context.</returns>
67+
IScope StartScopedSpan(out ISpan currentSpan);
6068
}
6169
}

src/OpenCensus.Collector.AspNetCore/Implementation/HttpInListener.cs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,23 +61,24 @@ public override void OnStartActivity(Activity activity, object payload)
6161

6262
string path = (request.PathBase.HasValue || request.Path.HasValue) ? (request.PathBase + request.Path).ToString() : "/";
6363

64-
this.Tracer.SpanBuilderWithRemoteParent(path, ctx).SetSampler(this.Sampler).StartScopedSpan();
65-
66-
var span = this.Tracer.CurrentSpan;
67-
68-
if (span != null)
64+
ISpan span = null;
65+
this.Tracer.SpanBuilderWithRemoteParent(path, ctx).SetSampler(this.Sampler).StartScopedSpan(out span);
66+
if (span == null)
6967
{
70-
// Note, route is missing at this stage. It will be available later
68+
// Debug.WriteLine("span is null");
69+
return;
70+
}
7171

72-
span.PutServerSpanKindAttribute();
73-
span.PutHttpHostAttribute(request.Host.Host, request.Host.Port ?? 80);
74-
span.PutHttpMethodAttribute(request.Method);
75-
span.PutHttpPathAttribute(path);
72+
// Note, route is missing at this stage. It will be available later
7673

77-
var userAgent = request.Headers["User-Agent"].FirstOrDefault();
78-
span.PutHttpUserAgentAttribute(userAgent);
79-
span.PutHttpRawUrlAttribute(GetUri(request));
80-
}
74+
span.PutServerSpanKindAttribute();
75+
span.PutHttpHostAttribute(request.Host.Host, request.Host.Port ?? 80);
76+
span.PutHttpMethodAttribute(request.Method);
77+
span.PutHttpPathAttribute(path);
78+
79+
var userAgent = request.Headers["User-Agent"].FirstOrDefault();
80+
span.PutHttpUserAgentAttribute(userAgent);
81+
span.PutHttpRawUrlAttribute(GetUri(request));
8182
}
8283

8384
public override void OnStopActivity(Activity activity, object payload)

src/OpenCensus.Collector.Dependencies/Implementation/HttpHandlerDiagnosticListener.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ public override void OnStartActivity(Activity activity, object payload)
5656
return;
5757
}
5858

59-
this.Tracer.SpanBuilder(request.RequestUri.AbsolutePath).SetSampler(this.Sampler).StartScopedSpan();
60-
var span = this.Tracer.CurrentSpan;
59+
this.Tracer.SpanBuilder(request.RequestUri.AbsolutePath).SetSampler(this.Sampler).StartScopedSpan(out ISpan span);
6160
span.PutClientSpanKindAttribute();
6261
span.PutHttpMethodAttribute(request.Method.ToString());
6362
span.PutHttpHostAttribute(request.RequestUri.Host, request.RequestUri.Port);

src/OpenCensus/Trace/SpanBuilderBase.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,11 @@ public IScope StartScopedSpan()
3333
{
3434
return CurrentSpanUtils.WithSpan(this.StartSpan(), true);
3535
}
36+
37+
public IScope StartScopedSpan(out ISpan currentSpan)
38+
{
39+
currentSpan = this.StartSpan();
40+
return CurrentSpanUtils.WithSpan(currentSpan, true);
41+
}
3642
}
3743
}

src/Samples/TestZipkin.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,9 @@ private static void DoWork(int i)
5858
// 7. Start another span. If another span was already started, it'll use that span as the parent span.
5959
// In this example, the main method already started a span, so that'll be the parent span, and this will be
6060
// a child span.
61-
using (OpenCensus.Common.IScope scope = tracer.SpanBuilder("DoWork").StartScopedSpan())
61+
using (OpenCensus.Common.IScope scope = tracer.SpanBuilder("DoWork").StartScopedSpan(out ISpan span))
6262
{
6363
// Simulate some work.
64-
ISpan span = tracer.CurrentSpan;
65-
6664
try
6765
{
6866
Console.WriteLine("Doing busy work");

test/OpenCensus.Tests/Impl/Trace/SpanBuilderBaseTest.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,23 @@ public void StartScopedSpan()
4949
span.Verify(s => s.End(EndSpanOptions.Default));
5050
Assert.Same(BlankSpan.Instance, tracer.CurrentSpan);
5151
}
52+
53+
[Fact]
54+
public void StartScopedSpan_WithParam()
55+
{
56+
Assert.Same(BlankSpan.Instance, tracer.CurrentSpan);
57+
58+
IScope scope = spanBuilder.Object.StartScopedSpan(out ISpan outSpan);
59+
try
60+
{
61+
Assert.Same(outSpan, tracer.CurrentSpan);
62+
}
63+
finally
64+
{
65+
scope.Dispose();
66+
}
67+
span.Verify(s => s.End(EndSpanOptions.Default));
68+
Assert.Same(BlankSpan.Instance, tracer.CurrentSpan);
69+
}
5270
}
5371
}

0 commit comments

Comments
 (0)