Ejemplo n.º 1
0
        public Task Send(IList <IClientSpan> entities)
        {
            //1 group and convert trace span trees
            //2 send spans by relations
            var sameTraceSpanGroups = entities.OrderBy(x => x.StartUtc)
                                      .GroupBy(x => new { x.TracerId, x.TraceId });

            var tracerContext = TracerContext.Resolve();

            foreach (var sameTraceSpanGroup in sameTraceSpanGroups)
            {
                var logger = SimpleLogSingleton <JaegerTracerFactory> .Instance.Logger;
                logger.LogInfo(string.Format("Send ClientSpans => {0}: {1}", sameTraceSpanGroup.Key, sameTraceSpanGroup.Count()));

                //var groupKey = sameTraceSpanGroup.Key;
                var sameTraceSpans = sameTraceSpanGroup.ToList();
                var myTreeConvert  = MyTreeConvert.Instance;
                var myTrees        = myTreeConvert
                                     .MakeTrees(sameTraceSpans, span => span.ToLocateCurrentKey(), span => span.ToLocateParentKey())
                                     .OrderBy(x => x.Value.StartUtc).ToList();

                foreach (var myTree in myTrees)
                {
                    SendSpanTree(tracerContext, myTree);
                }
            }

            return(Task.FromResult(0));
        }
Ejemplo n.º 2
0
        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>();
            }
        }