Пример #1
0
        public static IServiceCollection AddJaegerTracing(
            this IServiceCollection services,
            Action <JaegerTracingOptions> setupAction = null)
        {
            if (setupAction != null)
            {
                services.ConfigureJaegerTracing(setupAction);
            }

            services.AddSingleton <ITracer>(cli =>
            {
                var options = cli.GetService <IOptions <JaegerTracingOptions> >().Value;

                var senderConfig = new Jaeger.Configuration.SenderConfiguration(options.LoggerFactory)
                                   .WithAgentHost(options.JaegerAgentHost)
                                   .WithAgentPort(options.JaegerAgentPort);

                var reporter = new RemoteReporter.Builder()
                               .WithLoggerFactory(options.LoggerFactory)
                               .WithSender(senderConfig.GetSender())
                               .Build();

                var sampler = new GuaranteedThroughputSampler(options.SamplingRate, options.LowerBound);

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

                // Allows code that can't use dependency injection to have access to the tracer.
                if (!GlobalTracer.IsRegistered())
                {
                    GlobalTracer.Register(tracer);
                }

                return(tracer);
            });

            services.AddOpenTracing(builder => {
                builder.ConfigureAspNetCore(options => {
                    options.Hosting.IgnorePatterns.Add(x => {
                        return(x.Request.Path == "/health");
                    });
                    options.Hosting.IgnorePatterns.Add(x => {
                        return(x.Request.Path == "/metrics");
                    });
                });
            });

            return(services);
        }
        public static IServiceCollection AddTracing(this IServiceCollection services, IConfiguration configuration)
        {
            var applicationName = Assembly.GetCallingAssembly().GetName().Name?.ToLower();

            var section = configuration.GetSection(nameof(TracingSettings));

            if (string.IsNullOrWhiteSpace(section["AgentHost"]))
            {
                return(services);
            }

            services
            .AddOpenTracing()
            .Configure <TracingSettings>(configuration.GetSection(nameof(TracingSettings)))
            .AddSingleton <ITracer>(x =>
            {
                var loggerFactory = x.GetRequiredService <ILoggerFactory>();
                var options       = x.GetService <IOptions <TracingSettings> >()?.Value;

                if (options == null)
                {
                    return(new MockTracer());
                }

                var senderConfig = new Jaeger.Configuration.SenderConfiguration(loggerFactory)
                                   .WithAgentHost(options.AgentHost)
                                   .WithAgentPort(options.AgentPort);

                var reporter = new RemoteReporter.Builder()
                               .WithLoggerFactory(loggerFactory)
                               .WithSender(senderConfig.GetSender())
                               .Build();

                var sampler = new GuaranteedThroughputSampler(options.SamplingRate, options.LowerBound);

                var tracer = new Tracer.Builder(applicationName)
                             .WithLoggerFactory(loggerFactory)
                             .WithSampler(new ConstSampler(true))
                             .WithReporter(reporter)
                             .WithSampler(sampler)
                             .Build();

                GlobalTracer.Register(tracer);

                return(tracer);
            });

            return(services);
        }
Пример #3
0
        public static IServiceCollection AddJaegerTracing(this IServiceCollection services, Action <JaegerOptions> setupAction = null)
        {
            if (setupAction != null)
            {
                services.ConfigureJaegerTracing(setupAction);
            }

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

                ILogger logger = loggerFactory.CreateLogger("Jaeger");

                JaegerOptions options = cli.GetService <IOptions <JaegerOptions> >().Value;

                Configuration.SenderConfiguration.DefaultSenderResolver = new SenderResolver(loggerFactory)
                                                                          .RegisterSenderFactory <ThriftSenderFactory>();

                var senderConfig = new Jaeger.Configuration.SenderConfiguration(loggerFactory)
                                   .WithAgentHost(options.Host)
                                   .WithAgentPort(options.Port);

                var reporter = new RemoteReporter.Builder()
                               .WithLoggerFactory(loggerFactory)
                               .WithSender(senderConfig.GetSender())
                               .Build();

                logger.LogInformation($"Jaeger sending to {senderConfig.GetSender()}");

                var sampler = new GuaranteedThroughputSampler(options.SamplingRate, options.LowerBound);

                var tracer = new Tracer.Builder(options.ServiceName ?? "Not Set")
                             .WithLoggerFactory(loggerFactory)
                             .WithReporter(reporter)
                             .WithSampler(sampler)
                             .Build();

                // Allows code that can't use dependency injection to have access to the tracer.
                if (!GlobalTracer.IsRegistered())
                {
                    GlobalTracer.Register(tracer);
                }

                return(tracer);
            });

            services.AddOpenTracing(builder =>
            {
                builder.ConfigureAspNetCore(options =>
                {
                    options.Hosting.IgnorePatterns.Add(x =>
                    {
                        return(x.Request.Path.Value.EndsWith("live", StringComparison.InvariantCultureIgnoreCase));
                    });
                    options.Hosting.IgnorePatterns.Add(x =>
                    {
                        return(x.Request.Path.ToString().EndsWith("ready", StringComparison.InvariantCultureIgnoreCase));
                    });
                    options.Hosting.IgnorePatterns.Add(x =>
                    {
                        return(x.Request.Path == "/metrics");
                    });
                });
            });

            return(services);
        }