Beispiel #1
0
        private static void SetupJaeger()
        {
            var jaegerTracerConfig = new JaegerTracerConfig();

            jaegerTracerConfig.DefaultTracerId = "Default-Tracer";
            jaegerTracerConfig.TraceEndPoint   = "http://localhost:14268/api/traces";
            var tracerFactory = new JaegerTracerFactory(jaegerTracerConfig);

            //replace null
            var tracerContext = new TracerContext(tracerFactory);

            TracerContext.Resolve = () => tracerContext;
        }
        private static void AddTraceClients(IServiceCollection services, IConfiguration configuration)
        {
            services.AddSingleton <IClientTracerApi, ClientTracerApi>();
            services.AddSingleton <CommandQueue>();
            services.AddSingleton <CommandQueueTask>();

            //SimpleTrace.dll
            var assemblyToScan = Assembly.GetAssembly(typeof(ICommandLogistic));

            //All IClientSpanProcess
            services.AddSingletonFromAssembly(assemblyToScan, typeof(IClientSpanProcess));
            //All ICommandLogistic and KnownCommands
            services.AddSingletonFromAssembly(assemblyToScan, typeof(ICommandLogistic));
            services.AddSingleton(sp =>
            {
                var knownCommands = KnownCommands.Instance;

                var commandLogistics = sp.GetServices <ICommandLogistic>().ToList();
                foreach (var commandLogistic in commandLogistics)
                {
                    knownCommands.Register(commandLogistic);
                }

                return(knownCommands);
            });

            //TraceConfig
            var traceConfig = TryLoadTraceConfig(configuration);

            services.AddSingleton(traceConfig);

            //CommandQueueTaskLoop
            services.AddSingleton(sp =>
            {
                var commandQueueTaskLoop = new CommandQueueTaskLoop();

                var queueTask        = sp.GetService <CommandQueueTask>();
                var knownCommands    = sp.GetService <KnownCommands>();
                var commandLogistics = sp.GetServices <ICommandLogistic>().ToList();
                foreach (var commandLogistic in commandLogistics)
                {
                    knownCommands.Register(commandLogistic);
                }

                commandQueueTaskLoop.Init(
                    TimeSpan.FromSeconds(traceConfig.FlushIntervalSecond),
                    queueTask,
                    sp.GetService <CommandQueue>(),
                    sp.GetServices <ICommandLogistic>(),
                    sp.GetServices <IClientSpanProcess>(),
                    DateHelper.Instance.GetDateNow);

                return(commandQueueTaskLoop);
            });
            services.AddSingleton(CommandQueueProcessLogs.Instance);


            //IClientSpanRepository
            if (traceConfig.TraceSaveProcessEnabled)
            {
                services.AddSingleton <IClientSpanRepository, ClientSpanRepository>();
            }
            else
            {
                services.AddSingleton <IClientSpanRepository, NullClientSpanRepository>();
            }

            //TracerContext
            var tracerContext = TracerContext.Resolve();

            services.AddSingleton <TracerContext>(tracerContext);

            if (traceConfig.TraceSendProcessEnabled)
            {
                //JaegerTracerConfig
                var jaegerTracerConfig = new JaegerTracerConfig();
                jaegerTracerConfig.DefaultTracerId = traceConfig.DefaultTracerId;
                jaegerTracerConfig.TraceEndPoint   = traceConfig.TraceEndPoint;
                services.AddSingleton(jaegerTracerConfig);

                //ITracerFactory and TracerContext
                var tracerFactory = new JaegerTracerFactory(jaegerTracerConfig);
                services.AddSingleton <ITracerFactory>(tracerFactory);
                tracerContext.Factory = tracerFactory;

                //ITraceSender
                services.AddSingleton <ITraceSender, JaegerTraceSender>();

                //log TraceConfig
                var tracer = tracerContext.Current();
                using (var scope = tracer.BuildSpan("LogObject").StartActive(true))
                {
                    scope.Span.SetTag("Name", "TraceConfig");
                    var dictionary = MyModelHelper.GetKeyValueDictionary(traceConfig);
                    foreach (var kv in dictionary)
                    {
                        scope.Span.Log(kv.Key + " : " + kv.Value);
                    }
                }
            }
            else
            {
                services.AddSingleton <ITracerFactory, NullTracerFactory>();
                services.AddSingleton <ITraceSender, NullTraceSender>();
            }
        }