internal ITracer Build(string serviceName)
        {
            var loggerFactory = new LoggerFactory().AddConsole();

            //Very basic configuration
            var samplerConfiguration = new Jaeger.Configuration.SamplerConfiguration(loggerFactory)
                                       .WithType(ConstSampler.Type)
                                       .WithParam(1);

            //NOTE: not reporter for now
            var reporterConfiguration = new Jaeger.Configuration.ReporterConfiguration(loggerFactory)
                                        .WithLogSpans(true);

            //NOTE: only support jaeger for now
            var config = Jaeger.Configuration.FromEnv(loggerFactory);

            return(config.GetTracer());

            //return new Jaeger.Configuration(serviceName, loggerFactory)
            //                 .WithSampler(samplerConfiguration)
            //                 .WithReporter(reporterConfiguration)
            //                 .GetTracer();
        }
Beispiel #2
0
        /// <summary>
        /// 添加Jaeger链路追踪,实例对象ITracer
        /// </summary>
        /// <param name="services"></param>
        /// <param name="openTracingBuilder"></param>
        /// <returns></returns>
        public static IServiceCollection AddJaeger(this IServiceCollection services, Action <IOpenTracingBuilder> openTracingBuilder = null)
        {
            if (openTracingBuilder == null)
            {
                openTracingBuilder = builder =>
                {
                    builder.AddCoreFx();
                    builder.AddAspNetCore();
                    builder.AddEntityFrameworkCore();
                    builder.AddLoggerProvider();
                    builder.ConfigureGenericDiagnostics(options =>
                    {
                    });
                    builder.ConfigureAspNetCore(options =>
                    {
                        options.Hosting.OperationNameResolver = (context) =>
                        {
                            return(context.Request.Path.ToUriComponent());
                        };
                        options.Hosting.IgnorePatterns.Add(a =>
                        {
                            return(false);
                        });
                    });
                };
            }

            services.AddOpenTracing(openTracingBuilder);
            services.AddSingleton <ITracer>(serviceProvider =>
            {
                var config              = serviceProvider.GetService <TracingConfiguration>();
                var serviceName         = config.SerivceName ?? serviceProvider.GetRequiredService <IHostingEnvironment>().ApplicationName;
                var loggerFactory       = serviceProvider.GetRequiredService <ILoggerFactory>();
                var endPoint            = config.EndPoint;
                var senderConfiguration = new Jaeger.Configuration.SenderConfiguration(loggerFactory);

                if (!string.IsNullOrEmpty(config.AgentHost))
                {
                    senderConfiguration
                    .WithAgentHost(config.AgentHost)
                    .WithAgentPort(config.AgentPort);
                }
                else
                {
                    senderConfiguration.WithEndpoint(endPoint);
                }


                var samplerConfiguration = new Jaeger.Configuration.SamplerConfiguration(loggerFactory)
                                           .WithType(config.SamplerType);

                var reporterConfiguration = new Jaeger.Configuration.ReporterConfiguration(loggerFactory)
                                            .WithFlushInterval(TimeSpan.FromSeconds(config.FlushIntervalSeconds))
                                            .WithLogSpans(config.LogSpans)
                                            .WithSender(senderConfiguration);


                ITracer tracer = null;
                if (config.Open)
                {
                    tracer = new Jaeger.Configuration(serviceName, loggerFactory)
                             .WithSampler(samplerConfiguration)
                             .WithReporter(reporterConfiguration)
                             .GetTracer();
                }
                else
                {
                    tracer = new Jaeger.Tracer.Builder(serviceName)
                             .WithSampler(new Jaeger.Samplers.RateLimitingSampler(0))
                             .WithReporter(new Jaeger.Reporters.NoopReporter()).Build();
                }


                if (!GlobalTracer.IsRegistered())
                {
                    GlobalTracer.Register(tracer);
                }

                return(tracer);
            });
            return(services);
        }