public SamplingStatus Sample(string operation, TraceId id) { lock (_lock) { if (OperationNameToSampler.TryGetValue(operation, out var sampler)) { return(sampler.Sample(operation, id)); } if (OperationNameToSampler.Count < MaxOperations) { var newSampler = new GuaranteedThroughputSampler(DefaultSampler.SamplingRate, LowerBound); OperationNameToSampler[operation] = newSampler; return(newSampler.Sample(operation, id)); } return(DefaultSampler.Sample(operation, id)); } }
/// <summary> /// Updates the <see cref="GuaranteedThroughputSampler"/> for each operation. /// </summary> /// <param name="strategies">The parameters for operation sampling.</param> /// <returns><c>true</c>, if any samplers were updated.</returns> public bool Update(OperationSamplingParameters strategies) { lock (_lock) { var isUpdated = false; LowerBound = strategies.DefaultLowerBoundTracesPerSecond; ProbabilisticSampler defaultSampler = new ProbabilisticSampler(strategies.DefaultSamplingProbability); if (!defaultSampler.Equals(DefaultSampler)) { DefaultSampler.Close(); DefaultSampler = defaultSampler; isUpdated = true; } foreach (var strategy in strategies.PerOperationStrategies) { var operation = strategy.Operation; var samplingRate = strategy.ProbabilisticSampling.SamplingRate; if (OperationNameToSampler.TryGetValue(operation, out var sampler)) { isUpdated = sampler.Update(samplingRate, LowerBound) || isUpdated; } else { if (OperationNameToSampler.Count < MaxOperations) { sampler = new GuaranteedThroughputSampler(samplingRate, LowerBound); OperationNameToSampler[operation] = sampler; isUpdated = true; } else { _logger.LogInformation("Exceeded the maximum number of operations {maxOperations} for per operations sampling", MaxOperations); } } } return(isUpdated); } }