Commit b4484e5d authored by Liuhaoyang's avatar Liuhaoyang

Refactor ContextManager

parent 5af0ec2d
...@@ -16,35 +16,10 @@ ...@@ -16,35 +16,10 @@
* *
*/ */
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using SkyWalking.Context.Trace;
namespace SkyWalking.Context namespace SkyWalking.Context
{ {
public static class ListenerManager public interface IIgnoreTracerContextListener
{
private static readonly IList<ITracingContextListener> _listeners = new List<ITracingContextListener>();
[MethodImpl(MethodImplOptions.Synchronized)]
public static void Add(ITracingContextListener listener)
{
_listeners.Add(listener);
}
[MethodImpl(MethodImplOptions.Synchronized)]
public static void Remove(ITracingContextListener listener)
{ {
_listeners.Remove(listener); void AfterFinish(ITracerContext tracerContext);
}
public static void NotifyFinish(ITraceSegment traceSegment)
{
foreach (var listener in _listeners)
{
listener.AfterFinished(traceSegment);
}
}
} }
} }
\ No newline at end of file
...@@ -31,13 +31,14 @@ namespace SkyWalking.Context ...@@ -31,13 +31,14 @@ namespace SkyWalking.Context
/// We also provide the CONTEXT propagation based on ThreadLocal mechanism. /// We also provide the CONTEXT propagation based on ThreadLocal mechanism.
/// Meaning, each segment also related to singe thread. /// Meaning, each segment also related to singe thread.
/// </summary> /// </summary>
public class ContextManager :ITracingContextListener, IBootService public class ContextManager : ITracingContextListener, IBootService, IIgnoreTracerContextListener
{ {
private static readonly ThreadLocal<ITracerContext> _context = new ThreadLocal<ITracerContext>(); private static readonly ThreadLocal<ITracerContext> _context = new ThreadLocal<ITracerContext>();
private static ITracerContext GetOrCreateContext(String operationName, bool forceSampling) private static ITracerContext GetOrCreateContext(String operationName, bool forceSampling)
{ {
if (!_context.IsValueCreated) var context = _context.Value;
if (context == null)
{ {
if (string.IsNullOrEmpty(operationName)) if (string.IsNullOrEmpty(operationName))
{ {
...@@ -84,7 +85,7 @@ namespace SkyWalking.Context ...@@ -84,7 +85,7 @@ namespace SkyWalking.Context
{ {
get get
{ {
if (_context.IsValueCreated) if (_context.Value != null)
{ {
return _context.Value.GetReadableGlobalTraceId(); return _context.Value.GetReadableGlobalTraceId();
} }
...@@ -98,11 +99,13 @@ namespace SkyWalking.Context ...@@ -98,11 +99,13 @@ namespace SkyWalking.Context
public static ISpan CreateEntrySpan(string operationName, IContextCarrier carrier) public static ISpan CreateEntrySpan(string operationName, IContextCarrier carrier)
{ {
//todo samplingService //todo samplingService
return null; return null;
} }
public void AfterFinished(ITraceSegment traceSegment) public void AfterFinished(ITraceSegment traceSegment)
{ {
_context.Value = null;
} }
public void Dispose() public void Dispose()
...@@ -111,7 +114,13 @@ namespace SkyWalking.Context ...@@ -111,7 +114,13 @@ namespace SkyWalking.Context
public void Init() public void Init()
{ {
throw new NotImplementedException(); TracingContext.ListenerManager.Add(this);
IgnoredTracerContext.ListenerManager.Add(this);
}
public void AfterFinish(ITracerContext tracerContext)
{
_context.Value = null;
} }
} }
} }
...@@ -17,6 +17,8 @@ ...@@ -17,6 +17,8 @@
*/ */
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using SkyWalking.Context.Trace; using SkyWalking.Context.Trace;
namespace SkyWalking.Context namespace SkyWalking.Context
...@@ -71,5 +73,30 @@ namespace SkyWalking.Context ...@@ -71,5 +73,30 @@ namespace SkyWalking.Context
{ {
_stackDepth--; _stackDepth--;
} }
public static class ListenerManager
{
private static readonly List<IIgnoreTracerContextListener> _listeners = new List<IIgnoreTracerContextListener>();
[MethodImpl(MethodImplOptions.Synchronized)]
public static void Add(IIgnoreTracerContextListener listener)
{
_listeners.Add(listener);
}
public static void NotifyFinish(ITracerContext tracerContext)
{
foreach (var listener in _listeners)
{
listener.AfterFinish(tracerContext);
}
}
[MethodImpl(MethodImplOptions.Synchronized)]
public static void Remove(IIgnoreTracerContextListener listener)
{
_listeners.Remove(listener);
}
}
} }
} }
\ No newline at end of file
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Runtime.CompilerServices;
using SkyWalking.Boot; using SkyWalking.Boot;
using SkyWalking.Context.Trace; using SkyWalking.Context.Trace;
using SkyWalking.Dictionarys; using SkyWalking.Dictionarys;
...@@ -380,5 +381,32 @@ namespace SkyWalking.Context ...@@ -380,5 +381,32 @@ namespace SkyWalking.Context
return true; return true;
} }
public static class ListenerManager
{
private static readonly IList<ITracingContextListener> _listeners = new List<ITracingContextListener>();
[MethodImpl(MethodImplOptions.Synchronized)]
public static void Add(ITracingContextListener listener)
{
_listeners.Add(listener);
}
[MethodImpl(MethodImplOptions.Synchronized)]
public static void Remove(ITracingContextListener listener)
{
_listeners.Remove(listener);
}
public static void NotifyFinish(ITraceSegment traceSegment)
{
foreach (var listener in _listeners)
{
listener.AfterFinished(traceSegment);
}
}
}
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment