Skip to content

Commit

Permalink
Refactor current implementations of IScopeManager (#608)
Browse files Browse the repository at this point in the history
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
zacharycmontoya authored Jan 21, 2020
1 parent ed473a4 commit d969429
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 127 deletions.
79 changes: 9 additions & 70 deletions src/Datadog.Trace.AspNet/AspNetScopeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,12 @@

namespace Datadog.Trace.AspNet
{
internal class AspNetScopeManager : IScopeManager
internal class AspNetScopeManager : ScopeManagerBase
{
private static readonly Vendors.Serilog.ILogger Log = DatadogLogging.For<AspNetScopeManager>();

private readonly string _name = "__Datadog_Scope_Current__" + Guid.NewGuid();
private readonly AsyncLocalCompat<Scope> _activeScopeFallback = new AsyncLocalCompat<Scope>();

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 Scope Active
public override Scope Active
{
get
{
Expand All @@ -33,66 +21,17 @@ public Scope Active

return _activeScopeFallback.Get();
}
}

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);

SetScope(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
return;
}

// if the scope that was just closed was the active scope,
// set its parent as the new active scope
SetScope(current.Parent);
SpanDeactivated?.Invoke(this, new SpanEventArgs(current.Span));

if (!isRootSpan)
protected set
{
SpanActivated?.Invoke(this, new SpanEventArgs(current.Parent.Span));
}

SpanClosed?.Invoke(this, new SpanEventArgs(scope.Span));

if (isRootSpan)
{
TraceEnded?.Invoke(this, new SpanEventArgs(scope.Span));
}
}
var httpContext = HttpContext.Current;
if (httpContext != null)
{
httpContext.Items[_name] = value;
}

private void SetScope(Scope scope)
{
var httpContext = HttpContext.Current;
if (httpContext != null)
{
httpContext.Items[_name] = scope;
_activeScopeFallback.Set(value);
}

_activeScopeFallback.Set(scope);
}
}
}
63 changes: 6 additions & 57 deletions src/Datadog.Trace/AsyncLocalScopeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,71 +3,20 @@

namespace Datadog.Trace
{
internal class AsyncLocalScopeManager : IScopeManager
internal class AsyncLocalScopeManager : ScopeManagerBase
{
private static readonly Vendors.Serilog.ILogger Log = DatadogLogging.GetLogger(typeof(AsyncLocalScopeManager));

private readonly AsyncLocalCompat<Scope> _activeScope = new AsyncLocalCompat<Scope>();

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 Scope Active => _activeScope.Get();

public Scope Activate(Span span, bool finishOnClose)
public override Scope Active
{
var newParent = Active;
var scope = new Scope(newParent, span, this, finishOnClose);
var scopeOpenedArgs = new SpanEventArgs(span);

SpanOpened?.Invoke(this, scopeOpenedArgs);

_activeScope.Set(scope);

if (newParent != null)
get
{
SpanDeactivated?.Invoke(this, new SpanEventArgs(newParent.Span));
return _activeScope.Get();
}

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
_activeScope.Set(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)
protected set
{
TraceEnded?.Invoke(this, new SpanEventArgs(scope.Span));
_activeScope.Set(value);
}
}
}
Expand Down
72 changes: 72 additions & 0 deletions src/Datadog.Trace/ScopeManagerBase.cs
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));
}
}
}
}

0 comments on commit d969429

Please sign in to comment.