public void SetDefaultSampleRates(IEnumerable <KeyValuePair <string, float> > sampleRates)
        {
            // to avoid locking if writers and readers can access the dictionary at the same time,
            // build the new dictionary first, then replace the old one
            var rates = new Dictionary <SampleRateKey, float>();

            if (sampleRates != null)
            {
                foreach (var pair in sampleRates)
                {
                    // No point in adding default rates
                    if (pair.Value == 1.0f)
                    {
                        continue;
                    }

                    var key = SampleRateKey.Parse(pair.Key);

                    if (key == null)
                    {
                        Log.Warning("Could not parse sample rate key {SampleRateKey}", pair.Key);
                        continue;
                    }

                    rates.Add(key.Value, pair.Value);
                }
            }

            _sampleRates = rates;
        }
        public float GetSamplingRate(Span span)
        {
            Log.Debug("Using the default sampling logic");

            if (_sampleRates.Count == 0)
            {
                return(1);
            }

            string env;

            if (span.Tags is CommonTags tags)
            {
                env = tags.Environment;
            }
            else
            {
                env = span.GetTag(Tags.Env);
            }

            var service = span.ServiceName;

            var key = new SampleRateKey(service, env);

            if (_sampleRates.TryGetValue(key, out var sampleRate))
            {
                span.SetMetric(Metrics.SamplingAgentDecision, sampleRate);
                return(sampleRate);
            }

            Log.Debug("Could not establish sample rate for trace {TraceId}", span.TraceId);

            return(1);
        }