Esempio n. 1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // Create an OpenTracing ITracer with the default setting
            OpenTracing.ITracer tracer = OpenTracingTracerFactory.CreateTracer();

            // Use the tracer with ASP.NET Core dependency injection
            services.AddSingleton <ITracer>(tracer);

            // Use the tracer with OpenTracing.GlobalTracer.Instance
            GlobalTracer.Register(tracer);

            services.AddControllersWithViews();

            services.AddDbContext <ProductsContext>(options =>
                                                    options.UseSqlServer(Configuration.GetConnectionString("ProductsContext")));
        }
Esempio n. 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(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);


                OpenTracing.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);
        }