/// <summary>
        /// 添加Jaeger
        /// </summary>
        /// <param name="services"></param>
        /// <param name="conf"></param>
        /// <returns></returns>
        public static IServiceCollection AddClientJaeger(this IServiceCollection services, IConfiguration conf)
        {
            //读取Jaeger配制
            var key           = conf.GetSection("GrpcClient").Exists() ? "GrpcClient" : "GrpcServer";
            var jaegerOptions = conf.GetSection($"{key}:Jaeger").Get <JaegerOptions>();

            if (jaegerOptions == null || jaegerOptions.Enable == false)
            {
                return(services);
            }

            //jaeger
            if (!services.Any(p => p.ServiceType == typeof(ITracer)))
            {
                services.AddSingleton <ITracer>(sp =>
                {
                    var options = sp.GetService <IOptions <GrpcClientOptions> >().Value;
                    var tracer  = new Jaeger.Tracer.Builder(options.Jaeger.ServiceName)
                                  .WithLoggerFactory(sp.GetService <ILoggerFactory>())
                                  .WithSampler(new Jaeger.Samplers.ConstSampler(true))
                                  .WithReporter(new Jaeger.Reporters.RemoteReporter.Builder()
                                                .WithFlushInterval(TimeSpan.FromSeconds(5))
                                                .WithMaxQueueSize(5)
                                                .WithSender(new UdpSender(jaegerOptions.AgentIp, jaegerOptions.AgentPort, 1024 * 5)).Build())
                                  .Build();
                    return(tracer);
                });
            }
            //添加jaeger中间件
            services.AddClientInterceptor <ClientJaegerTracingInterceptor>();

            return(services);
        }
Example #2
0
        /// <summary>
        /// 添加Jaeger
        /// </summary>
        /// <param name="services"></param>
        /// <param name="conf"></param>
        /// <returns></returns>
        private static IServiceCollection AddJaeger(this IServiceCollection services, IConfiguration conf)
        {
            var key           = "GrpcServer:Jaeger";
            var jaegerOptions = conf.GetSection(key).Get <JaegerOptions>();

            if (jaegerOptions == null || jaegerOptions.Enable == false)
            {
                return(services);
            }

            //jaeger
            services.AddSingleton <ITracer>(sp => {
                var options     = sp.GetService <IOptions <GrpcServerOptions> >().Value.Jaeger;
                var serviceName = options.ServiceName;
                var tracer      = new Jaeger.Tracer.Builder(serviceName)
                                  .WithLoggerFactory(sp.GetService <ILoggerFactory>())
                                  .WithSampler(new Jaeger.Samplers.ConstSampler(true))
                                  .WithReporter(new Jaeger.Reporters.RemoteReporter.Builder()
                                                .WithFlushInterval(TimeSpan.FromSeconds(5))
                                                .WithMaxQueueSize(5)
                                                .WithSender(new UdpSender(jaegerOptions.AgentIp, jaegerOptions.AgentPort, 1024 * 5)).Build())
                                  .Build();
                return(tracer);
            });

            return(services);
        }
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            string JAEGER_SERVICE_NAME = Environment.GetEnvironmentVariable("JAEGER_SERVICE_NAME");
            string JAEGER_AGENT_PORT   = Environment.GetEnvironmentVariable("JAEGER_AGENT_PORT");
            string JAEGER_AGENT_HOST   = Environment.GetEnvironmentVariable("JAEGER_AGENT_HOST");

            services.AddOpenTracing();
            var sender = new Jaeger.Senders.Thrift.UdpSender(
                JAEGER_AGENT_HOST,
                int.Parse(JAEGER_AGENT_PORT),
                0
                );
            var remoteReporter = new RemoteReporter.Builder()
                                 .WithSender(sender)
                                 .Build();
            var sampler = new ConstSampler(true);
            var tracer  = new Jaeger.Tracer.Builder(JAEGER_SERVICE_NAME)
                          .WithReporter(remoteReporter)
                          .WithSampler(sampler)
                          .Build();

            GlobalTracer.Register(tracer);
            services.AddHttpClient();
            services.AddControllers();
        }
Example #4
0
        public static IServiceCollection AddTracerService(this IServiceCollection services)
        {
            if (_initialized)
            {
                return(services);
            }

            _initialized = true;
            var options = GetJaegerOptions(services);

            if (!options.Enabled)
            {
                var defaultTracer = DefaultTracer.Create();
                services.AddSingleton(defaultTracer);
                return(services);
            }

            services.AddSingleton <ITracer>(sp =>
            {
                var loggerFactory = sp.GetRequiredService <ILoggerFactory>();

                var reporter = new RemoteReporter
                               .Builder()
                               .WithSender(new UdpSender(options.UdpHost, options.UdpPort, options.MaxPacketSize))
                               .WithLoggerFactory(loggerFactory)
                               .Build();

                var sampler = GetSampler(options);

                var tracer = new Jaeger.Tracer
                             .Builder(options.ServiceName)
                             .WithReporter(reporter)
                             .WithSampler(sampler)
                             .Build();

                GlobalTracer.Register(tracer);

                return(tracer);
            });
            return(services);
        }
        /// <summary>
        /// 添加Jaeger和Interceptor
        /// </summary>
        /// <param name="services"></param>
        /// <param name="conf"></param>
        /// <returns></returns>
        public static IServiceCollection AddJaeger(this IServiceCollection services, IConfiguration conf)
        {
            var key           = "GrpcServer:Jaeger";
            var jaegerOptions = conf.GetSection(key).Get <JaegerOptions>();

            if (jaegerOptions == null)
            {
                throw new ArgumentException($"{key} Value cannot be null");
            }

            if (string.IsNullOrWhiteSpace(jaegerOptions.AgentIp))
            {
                throw new ArgumentException($"{key}:AgentIp Value cannot be null");
            }

            if (jaegerOptions.AgentPort == 0)
            {
                throw new ArgumentNullException($"{key}:AgentPort Value cannot be null");
            }

            //jaeger
            services.AddSingleton <ITracer>(sp => {
                var serviceName = jaegerOptions.ServiceName ?? GrpcServerOptions.Instance.DiscoveryServiceName;
                var tracer      = new Jaeger.Tracer.Builder(serviceName)
                                  .WithLoggerFactory(sp.GetService <ILoggerFactory>())
                                  .WithSampler(new Jaeger.Samplers.ConstSampler(true))
                                  .WithReporter(new Jaeger.Reporters.RemoteReporter.Builder()
                                                .WithFlushInterval(TimeSpan.FromSeconds(5))
                                                .WithMaxQueueSize(5)
                                                .WithSender(new Jaeger.Senders.UdpSender(jaegerOptions.AgentIp, jaegerOptions.AgentPort, 1024 * 5)).Build())
                                  .Build();
                return(tracer);
            });

            //添加jaeger中间件
            services.AddServerInterceptor <JaegerTracingInterceptor>();
            services.AddClientJaeger();

            return(services);
        }
Example #6
0
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddOcelot();
            services.AddSingleton <OpenTracing.ITracer>(serviceProvider =>
            {
                string serviceName = Assembly.GetEntryAssembly().GetName().Name;

                ILoggerFactory loggerFactory = serviceProvider.GetRequiredService <ILoggerFactory>();

                ISampler sampler = new ConstSampler(sample: true);

                OpenTracing.ITracer tracer = new Jaeger.Tracer.Builder(serviceName)
                                             .WithLoggerFactory(loggerFactory)
                                             .WithSampler(sampler)
                                             .Build();

                GlobalTracer.Register(tracer);

                return(tracer);
            });

            services.AddOpenTracing();
        }
Example #7
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);
        }