Example #1
0
        public void TestProbabilityTagsOverrideRateLimitingTags()
        {
            _undertest = new GuaranteedThroughputSampler(0.999, 1.0);

            SamplingStatus samplingStatus = _undertest.Sample("test", new TraceId(0L));

            Assert.True(samplingStatus.IsSampled);
            var tags = samplingStatus.Tags;

            Assert.Equal(ProbabilisticSampler.Type, tags[Constants.SamplerTypeTagKey]);
            Assert.Equal(0.999, tags[Constants.SamplerParamTagKey]);
        }
Example #2
0
        public void TestRateLimitingLowerBound()
        {
            _undertest = new GuaranteedThroughputSampler(0.0001, 1.0);

            SamplingStatus samplingStatus = _undertest.Sample("test", new TraceId(long.MaxValue));

            Assert.True(samplingStatus.IsSampled);
            var tags = samplingStatus.Tags;

            Assert.Equal(GuaranteedThroughputSampler.Type, tags[Constants.SamplerTypeTagKey]);
            Assert.Equal(0.0001, tags[Constants.SamplerParamTagKey]);
        }
Example #3
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 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);
        }
Example #4
0
        public static IServiceCollection AddJaegerTracingForService(this IServiceCollection services, Action <JaegerTracingOptions> setupAction = null)
        {
            // Run setup action
            if (setupAction != null)
            {
                services.ConfigureJaegerTracing(setupAction);
            }

            // Configure Open Tracing with non-default behavior, skipping ASP.Net and Entity Framework
            services.AddOpenTracingCoreServices(builder =>
                                                builder.AddCoreFx()
                                                .AddLoggerProvider());

            services.AddSingleton <ITracer>(serviceProvider =>
            {
                // Get the options for the various parts of the tracer
                var options = serviceProvider.GetService <IOptions <JaegerTracingOptions> >().Value;

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

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

                var sender = senderConfig.GetSender();

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

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

                var tracer = new Tracer.Builder(options.ServiceName)
                             .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);
            });

            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);
        }
Example #6
0
        // Jaeger for .NET Core sources:
        // https://github.com/jaegertracing/jaeger-client-csharp
        // https://medium.com/imaginelearning/jaeger-tracing-on-kubernetes-with-net-core-8b5feddb6f2f
        // https://itnext.io/jaeger-tracing-on-kubernetes-with-asp-net-core-and-traefik-86b1d9fd5489


        // Note: redundant code in this file, can refactor

        public static IServiceCollection AddJaegerTracingForApi(this IServiceCollection services,
                                                                Action <JaegerTracingOptions> setupAction,
                                                                Action <AspNetCoreDiagnosticOptions> aspnetOptionsAction)
        {
            services.ConfigureJaegerTracing(setupAction);

            // Configure Open Tracing with default behavior for .NET
            services.AddOpenTracing(builder =>
            {
                builder.ConfigureAspNetCore(aspnetOptionsAction);
            });

            services.AddSingleton <ITracer>(serviceProvider =>
            {
                // Get the options for the various parts of the tracer
                var options = serviceProvider.GetService <IOptions <JaegerTracingOptions> >().Value;

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

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

                var sender = senderConfig.GetSender();

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

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

                var tracer = new Tracer.Builder(options.ServiceName)
                             .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);
            });

            return(services);
        }
Example #7
0
        public void TestPerOperationSamplerWithKnownOperation()
        {
            GuaranteedThroughputSampler sampler = Substitute.ForPartsOf <GuaranteedThroughputSampler>(0, 0);

            _operationToSamplers.Add(operation, sampler);

            sampler.Sample(operation, TraceId)
            .Returns(new SamplingStatus(true, EmptyTags));

            SamplingStatus samplingStatus = _undertest.Sample(operation, TraceId);

            Assert.True(samplingStatus.IsSampled);
            sampler.Received(1).Sample(operation, TraceId);
            //verifyNoMoreInteractions(_defaultProbabilisticSampler);
        }
Example #8
0
        public void TestUpdate_rateLimitingSampler()
        {
            _undertest = new GuaranteedThroughputSampler(0.001, 1);

            Assert.False(_undertest.Update(0.001, 1));
            Assert.True(_undertest.Update(0.001, 0));

            SamplingStatus samplingStatus = _undertest.Sample("test", new TraceId(0L));

            Assert.True(samplingStatus.IsSampled);
            var tags = samplingStatus.Tags;

            Assert.Equal(ProbabilisticSampler.Type, tags[Constants.SamplerTypeTagKey]);
            Assert.Equal(0.001, tags[Constants.SamplerParamTagKey]);
        }
Example #9
0
        public void TestUpdate()
        {
            GuaranteedThroughputSampler guaranteedThroughputSampler = Substitute.ForPartsOf <GuaranteedThroughputSampler>(0, 0);

            _operationToSamplers.Add(operation, guaranteedThroughputSampler);

            var perOperationSamplingParameters = new PerOperationSamplingParameters(operation, new ProbabilisticSamplingStrategy(SamplingRate));
            var parametersList = new List <PerOperationSamplingParameters>();

            parametersList.Add(perOperationSamplingParameters);

            var parameters = new OperationSamplingParameters(DefaultSamplingProbability, DefaultLowerBoundTracesPerSecond, parametersList);

            Assert.True(_undertest.Update(parameters));
            guaranteedThroughputSampler.Received(1).Update(SamplingRate, DefaultLowerBoundTracesPerSecond);
            //verifyNoMoreInteractions(guaranteedThroughputSampler);
        }
        /// <summary>
        /// Построение нового экземпляра класса Jaeger.Tracer для
        /// отправки запросов и метрики в удаленный сервис-агента.
        /// </summary>
        /// <param name="options"></param>
        /// <returns></returns>
        public static ITracer ConfigureJaegerTracer(ITracerOptions options = null)
        {
            var senderConfig = new 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);

            return(new Jaeger.Tracer.Builder(options.ServiceName)
                   .WithLoggerFactory(options.LoggerFactory)
                   .WithReporter(reporter)
                   .WithSampler(sampler)
                   .Build());
        }
Example #11
0
        public void TestUpdateIgnoreGreaterThanMax()
        {
            GuaranteedThroughputSampler guaranteedThroughputSampler = Substitute.ForPartsOf <GuaranteedThroughputSampler>(0, 0);

            _operationToSamplers.Add(operation, guaranteedThroughputSampler);

            PerOperationSampler undertest = new PerOperationSampler(1, _operationToSamplers,
                                                                    _defaultProbabilisticSampler, DefaultLowerBoundTracesPerSecond, _loggerFactory);

            var perOperationSamplingParameters1 = new PerOperationSamplingParameters(operation, new ProbabilisticSamplingStrategy(SamplingRate));
            var perOperationSamplingParameters2 = new PerOperationSamplingParameters("second OPERATION", new ProbabilisticSamplingStrategy(SamplingRate));
            var parametersList = new List <PerOperationSamplingParameters>();

            parametersList.Add(perOperationSamplingParameters1);
            parametersList.Add(perOperationSamplingParameters2);

            undertest.Update(new OperationSamplingParameters(DefaultSamplingProbability,
                                                             DefaultLowerBoundTracesPerSecond, parametersList));

            Assert.Single(_operationToSamplers);
            Assert.NotNull(_operationToSamplers[operation]);
        }
        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);
        }