Esempio n. 1
0
        public void Constructs_CartOnlyRule_From_Config_String()
        {
            var config = "rate=0.5, service=shopping-cart.*";
            var rule   = RegexSamplingRule.BuildFromConfigurationString(config).Single();

            Assert.True(rule.IsMatch(CartCheckoutSpan));
            Assert.True(rule.IsMatch(AddToCartSpan));
            Assert.False(rule.IsMatch(ShippingAuthSpan));
            Assert.False(rule.IsMatch(ShippingRevertSpan));
            Assert.False(rule.IsMatch(RequestShippingSpan));
        }
Esempio n. 2
0
        public void Constructs_ZeroRateOnly_From_Config_String()
        {
            var config = "rate=0";
            var rule   = RegexSamplingRule.BuildFromConfigurationString(config).Single();

            Assert.Equal(expected: 0, actual: rule.GetSamplingRate());

            Assert.True(rule.IsMatch(CartCheckoutSpan));
            Assert.True(rule.IsMatch(AddToCartSpan));
            Assert.True(rule.IsMatch(ShippingAuthSpan));
            Assert.True(rule.IsMatch(ShippingRevertSpan));
            Assert.True(rule.IsMatch(RequestShippingSpan));
        }
Esempio n. 3
0
        public void Constructs_All_Expected_From_Config_String()
        {
            var config = "rate=0.5, service=.*cart.*;  rate=1, service=.*shipping.*, operation=authorize; rate=0.1, service=.*shipping.*; rate=0.05";
            var rules  = RegexSamplingRule.BuildFromConfigurationString(config).ToArray();

            var cartRule = rules[0];

            Assert.Equal(expected: 0.5f, actual: cartRule.GetSamplingRate());

            Assert.True(cartRule.IsMatch(CartCheckoutSpan));
            Assert.True(cartRule.IsMatch(AddToCartSpan));
            Assert.False(cartRule.IsMatch(ShippingAuthSpan));
            Assert.False(cartRule.IsMatch(ShippingRevertSpan));
            Assert.False(cartRule.IsMatch(RequestShippingSpan));

            var shippingAuthRule = rules[1];

            Assert.Equal(expected: 1f, actual: shippingAuthRule.GetSamplingRate());

            Assert.False(shippingAuthRule.IsMatch(CartCheckoutSpan));
            Assert.False(shippingAuthRule.IsMatch(AddToCartSpan));
            Assert.True(shippingAuthRule.IsMatch(ShippingAuthSpan));
            Assert.False(shippingAuthRule.IsMatch(ShippingRevertSpan));
            Assert.False(shippingAuthRule.IsMatch(RequestShippingSpan));

            var fallbackShippingRule = rules[2];

            Assert.Equal(expected: 0.1f, actual: fallbackShippingRule.GetSamplingRate());

            Assert.False(fallbackShippingRule.IsMatch(CartCheckoutSpan));
            Assert.False(fallbackShippingRule.IsMatch(AddToCartSpan));
            Assert.True(fallbackShippingRule.IsMatch(ShippingAuthSpan));
            Assert.True(fallbackShippingRule.IsMatch(ShippingRevertSpan));
            Assert.True(fallbackShippingRule.IsMatch(RequestShippingSpan));

            var fallbackRule = rules[3];

            Assert.Equal(expected: 0.05f, actual: fallbackRule.GetSamplingRate());

            Assert.True(fallbackRule.IsMatch(CartCheckoutSpan));
            Assert.True(fallbackRule.IsMatch(AddToCartSpan));
            Assert.True(fallbackRule.IsMatch(ShippingAuthSpan));
            Assert.True(fallbackRule.IsMatch(ShippingRevertSpan));
            Assert.True(fallbackRule.IsMatch(RequestShippingSpan));
        }
Esempio n. 4
0
        internal Tracer(TracerSettings settings, IAgentWriter agentWriter, ISampler sampler, IScopeManager scopeManager, IStatsd statsd)
        {
            // fall back to default implementations of each dependency if not provided
            Settings = settings ?? TracerSettings.FromDefaultSources();

            // only set DogStatsdClient if tracer metrics are enabled
            if (Settings.TracerMetricsEnabled)
            {
                Statsd = statsd ?? CreateDogStatsdClient(Settings);
            }

            IApi apiClient = new Api(Settings.AgentUri, delegatingHandler: null, Statsd);

            _agentWriter  = agentWriter ?? new AgentWriter(apiClient, Statsd);
            _scopeManager = scopeManager ?? new AsyncLocalScopeManager();
            Sampler       = sampler ?? new RuleBasedSampler(new RateLimiter(Settings.MaxTracesSubmittedPerSecond));

            if (!string.IsNullOrWhiteSpace(Settings.CustomSamplingRules))
            {
                foreach (var rule in RegexSamplingRule.BuildFromConfigurationString(Settings.CustomSamplingRules))
                {
                    Sampler.RegisterRule(rule);
                }
            }

            // if not configured, try to determine an appropriate service name
            DefaultServiceName = Settings.ServiceName ??
                                 GetApplicationName() ??
                                 UnknownServiceName;

            // Register callbacks to make sure we flush the traces before exiting
            AppDomain.CurrentDomain.ProcessExit        += CurrentDomain_ProcessExit;
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            Console.CancelKeyPress += Console_CancelKeyPress;

            // If configured, add/remove the correlation identifiers into the
            // LibLog logging context when a scope is activated/closed
            if (Settings.LogsInjectionEnabled)
            {
                InitializeLibLogScopeEventSubscriber(_scopeManager);
            }
        }