Commit 5af0ec2d authored by Liuhaoyang's avatar Liuhaoyang

Init ServiceManager

parent 54b7c0b9
...@@ -16,14 +16,12 @@ ...@@ -16,14 +16,12 @@
* *
*/ */
using System.Threading.Tasks; using System;
namespace SkyWalking.Boot namespace SkyWalking.Boot
{ {
public interface IBootService public interface IBootService: IDisposable
{ {
Task Executing(); void Init();
Task Executed();
} }
} }
\ No newline at end of file
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
namespace SkyWalking.Boot
{
public class ServiceManager : IDisposable
{
private static readonly ServiceManager _instance = new ServiceManager();
public static ServiceManager Instance => _instance;
private readonly Dictionary<Type, object> _services = new Dictionary<Type, object>();
private ServiceManager()
{
}
private Type[] FindServiceTypes()
{
return typeof(ServiceManager).Assembly.GetTypes().Where(x => typeof(IBootService).IsAssignableFrom(x))
.ToArray();
}
public object GetService(Type serviceType)
{
_services.TryGetValue(serviceType, out var instance);
return instance;
}
public T GetService<T>()
{
return (T) GetService(typeof(T));
}
[MethodImpl(MethodImplOptions.Synchronized)]
public void Init()
{
var types = FindServiceTypes();
foreach (var type in types)
{
if (Activator.CreateInstance(type) is IBootService service)
{
service.Init();
_services.Add(type, service);
}
}
}
[MethodImpl(MethodImplOptions.Synchronized)]
public void Dispose()
{
foreach (var item in _services.Values)
{
var service = item as IBootService;
service?.Dispose();
}
}
}
}
\ No newline at end of file
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*
*/
using System;
using System.Threading;
using SkyWalking.Boot;
using SkyWalking.Config;
using SkyWalking.Context.Trace;
using SkyWalking.Dictionarys;
using SkyWalking.Sampling;
namespace SkyWalking.Context
{
/// <summary>
/// Context manager controls the whole context of tracing. Since .NET server application runs as same as Java,
/// We also provide the CONTEXT propagation based on ThreadLocal mechanism.
/// Meaning, each segment also related to singe thread.
/// </summary>
public class ContextManager :ITracingContextListener, IBootService
{
private static readonly ThreadLocal<ITracerContext> _context = new ThreadLocal<ITracerContext>();
private static ITracerContext GetOrCreateContext(String operationName, bool forceSampling)
{
if (!_context.IsValueCreated)
{
if (string.IsNullOrEmpty(operationName))
{
// logger.debug("No operation name, ignore this trace.");
_context.Value = new IgnoredTracerContext();
}
else
{
if (!DictionaryUtil.IsNull(RemoteDownstreamConfig.Agent.ApplicationId) &&
DictionaryUtil.IsNull(RemoteDownstreamConfig.Agent.ApplicationInstanceId))
{
var suffixIdx = operationName.LastIndexOf('.');
if (suffixIdx > -1 && AgentConfig.IgnoreSuffix.Contains(operationName.Substring(suffixIdx)))
{
_context.Value = new IgnoredTracerContext();
}
else
{
var sampler = ServiceManager.Instance.GetService<SamplingService>();
if (forceSampling || sampler.TrySampling())
{
_context.Value = new TracingContext();
}
else
{
_context.Value = new IgnoredTracerContext();
}
}
}
else
{
_context.Value = new IgnoredTracerContext();
}
}
}
return _context.Value;
}
private static ITracerContext Context => _context.Value;
public static string GlobalTraceId
{
get
{
if (_context.IsValueCreated)
{
return _context.Value.GetReadableGlobalTraceId();
}
return "N/A";
}
}
public static IContextSnapshot Capture => _context.Value?.Capture;
public static ISpan CreateEntrySpan(string operationName, IContextCarrier carrier)
{
//todo samplingService
return null;
}
public void AfterFinished(ITraceSegment traceSegment)
{
}
public void Dispose()
{
}
public void Init()
{
throw new NotImplementedException();
}
}
}
...@@ -16,36 +16,60 @@ ...@@ -16,36 +16,60 @@
* *
*/ */
using System;
using System.Threading; using SkyWalking.Context.Trace;
namespace SkyWalking.Context namespace SkyWalking.Context
{ {
/// <summary> public class IgnoredTracerContext : ITracerContext
/// Context manager controls the whole context of tracing. Since .NET server application runs as same as Java,
/// We also provide the CONTEXT propagation based on ThreadLocal mechanism.
/// Meaning, each segment also related to singe thread.
/// </summary>
public static class ContextManager
{ {
private static readonly ThreadLocal<ITracerContext> CONTEXT = new ThreadLocal<ITracerContext>(); private static readonly NoopSpan noopSpan = new NoopSpan();
private int _stackDepth;
public void Inject(IContextCarrier carrier)
{
}
public void Extract(IContextCarrier carrier)
{
}
public IContextSnapshot Capture { get; }
public ISpan ActiveSpan { get; }
public void Continued(IContextSnapshot snapshot)
{
}
private static ITracerContext GetOrCreate(String operationName, bool forceSampling) public string GetReadableGlobalTraceId()
{ {
if (!CONTEXT.IsValueCreated) return string.Empty;
{ }
return null;
} public ISpan CreateEntrySpan(string operationName)
else {
{ _stackDepth++;
return null; return noopSpan;
} }
public ISpan CreateLocalSpan(string operationName)
{
_stackDepth++;
return noopSpan;
}
public ISpan CreateExitSpan(string operationName, string remotePeer)
{
_stackDepth++;
return noopSpan;
} }
public static IContextSnapshot Capture() public void StopSpan(ISpan span)
{ {
return null; _stackDepth--;
} }
} }
} }
\ No newline at end of file
...@@ -17,13 +17,10 @@ ...@@ -17,13 +17,10 @@
*/ */
using System; using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Linq.Expressions; using SkyWalking.Boot;
using System.Runtime.InteropServices;
using SkyWalking.Context.Trace; using SkyWalking.Context.Trace;
using SkyWalking.Dictionary;
using SkyWalking.Dictionarys; using SkyWalking.Dictionarys;
using SkyWalking.Sampling; using SkyWalking.Sampling;
using SkyWalking.Utils; using SkyWalking.Utils;
...@@ -40,7 +37,7 @@ namespace SkyWalking.Context ...@@ -40,7 +37,7 @@ namespace SkyWalking.Context
public TracingContext() public TracingContext()
{ {
_sampler = new SamplingService(); _sampler = ServiceManager.Instance.GetService<SamplingService>();
_segment = new TraceSegment(); _segment = new TraceSegment();
_activeSpanStacks = new Stack<ISpan>(); _activeSpanStacks = new Stack<ISpan>();
} }
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
* *
*/ */
namespace SkyWalking.Dictionary namespace SkyWalking.Dictionarys
{ {
public class DictionaryManager public class DictionaryManager
{ {
......
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
using SkyWalking.Dictionarys; using SkyWalking.Dictionarys;
namespace SkyWalking.Dictionary namespace SkyWalking.Dictionarys
{ {
public class Found : PossibleFound public class Found : PossibleFound
{ {
......
...@@ -21,7 +21,7 @@ using SkyWalking.Config; ...@@ -21,7 +21,7 @@ using SkyWalking.Config;
using SkyWalking.Dictionarys; using SkyWalking.Dictionarys;
using SkyWalking.NetworkProtocol; using SkyWalking.NetworkProtocol;
namespace SkyWalking.Dictionary namespace SkyWalking.Dictionarys
{ {
public class NetworkAddressDictionary public class NetworkAddressDictionary
{ {
......
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
using SkyWalking.Dictionarys; using SkyWalking.Dictionarys;
namespace SkyWalking.Dictionary namespace SkyWalking.Dictionarys
{ {
public class NotFound : PossibleFound public class NotFound : PossibleFound
{ {
......
...@@ -25,7 +25,7 @@ using SkyWalking.Config; ...@@ -25,7 +25,7 @@ using SkyWalking.Config;
using SkyWalking.Dictionarys; using SkyWalking.Dictionarys;
using SkyWalking.NetworkProtocol; using SkyWalking.NetworkProtocol;
namespace SkyWalking.Dictionary namespace SkyWalking.Dictionarys
{ {
public class OperationNameDictionary public class OperationNameDictionary
{ {
......
...@@ -16,29 +16,28 @@ ...@@ -16,29 +16,28 @@
* *
*/ */
using System.Threading.Tasks;
namespace SkyWalking.Sampling namespace SkyWalking.Sampling
{ {
public class SamplingService : ISampler public class SamplingService : ISampler
{ {
public Task Executing()
public bool TrySampling()
{ {
return Task.CompletedTask; return true;
} }
public Task Executed() public void ForceSampled()
{ {
return Task.CompletedTask;
} }
public bool TrySampling() public void Dispose()
{ {
return true; throw new System.NotImplementedException();
} }
public void ForceSampled() public void Init()
{ {
throw new System.NotImplementedException();
} }
} }
} }
\ 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