-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor current implementations of IScopeManager (#608)
Refactor current implementations of IScopeManager to inherit from ScopeManagerBase so all the implementation is identical except for how the Active scope is stored and retrieved.
- Loading branch information
1 parent
ed473a4
commit d969429
Showing
3 changed files
with
87 additions
and
127 deletions.
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
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,72 @@ | ||
using System; | ||
using Datadog.Trace.Logging; | ||
|
||
namespace Datadog.Trace | ||
{ | ||
internal abstract class ScopeManagerBase : IScopeManager | ||
{ | ||
private static readonly Vendors.Serilog.ILogger Log = DatadogLogging.GetLogger(typeof(ScopeManagerBase)); | ||
|
||
public event EventHandler<SpanEventArgs> SpanOpened; | ||
|
||
public event EventHandler<SpanEventArgs> SpanActivated; | ||
|
||
public event EventHandler<SpanEventArgs> SpanDeactivated; | ||
|
||
public event EventHandler<SpanEventArgs> SpanClosed; | ||
|
||
public event EventHandler<SpanEventArgs> TraceEnded; | ||
|
||
public abstract Scope Active { get; protected set; } | ||
|
||
public Scope Activate(Span span, bool finishOnClose) | ||
{ | ||
var newParent = Active; | ||
var scope = new Scope(newParent, span, this, finishOnClose); | ||
var scopeOpenedArgs = new SpanEventArgs(span); | ||
|
||
SpanOpened?.Invoke(this, scopeOpenedArgs); | ||
|
||
Active = scope; | ||
|
||
if (newParent != null) | ||
{ | ||
SpanDeactivated?.Invoke(this, new SpanEventArgs(newParent.Span)); | ||
} | ||
|
||
SpanActivated?.Invoke(this, scopeOpenedArgs); | ||
|
||
return scope; | ||
} | ||
|
||
public void Close(Scope scope) | ||
{ | ||
var current = Active; | ||
var isRootSpan = scope.Parent == null; | ||
|
||
if (current == null || current != scope) | ||
{ | ||
// This is not the current scope for this context, bail out | ||
SpanClosed?.Invoke(this, new SpanEventArgs(scope.Span)); | ||
return; | ||
} | ||
|
||
// if the scope that was just closed was the active scope, | ||
// set its parent as the new active scope | ||
Active = scope.Parent; | ||
SpanDeactivated?.Invoke(this, new SpanEventArgs(scope.Span)); | ||
|
||
if (!isRootSpan) | ||
{ | ||
SpanActivated?.Invoke(this, new SpanEventArgs(scope.Parent.Span)); | ||
} | ||
|
||
SpanClosed?.Invoke(this, new SpanEventArgs(scope.Span)); | ||
|
||
if (isRootSpan) | ||
{ | ||
TraceEnded?.Invoke(this, new SpanEventArgs(scope.Span)); | ||
} | ||
} | ||
} | ||
} |