internal void CopyFrom(Reservior r) { _lock.EnterWriteLock(); try { if (r.Quota != null) { Quota = r.Quota.Value; } if (r.TTL != null) { TTL = new TimeStamp(); TTL.CopyFrom(r.TTL); } if (r.Interval != null) { Interval = r.Interval.Value; } if (r._refereshedAt != null) { _refereshedAt = new TimeStamp(); _refereshedAt.CopyFrom(r._refereshedAt); } } finally { _lock.ExitWriteLock(); } }
internal bool ShouldReport(TimeStamp now) { if (EverMatched() && Reservior.ShouldReport(now)) { return(true); } return(false); }
public const String Default = "Default"; // Reserved keyword by X-Ray service public SamplingRule(string ruleName, int priority, double fixedRate, int reservoirSize, string host, string serviceName, string httpMethod, string urlPath, string serviceType, string resourceARN, Dictionary <string, string> attributes) { RuleName = ruleName; Priority = priority; Rate = fixedRate; ReservoirSize = reservoirSize; CanBorrow = reservoirSize > 0; ServiceName = serviceName; HTTPMethod = httpMethod; URLPath = urlPath; Host = host; ServiceType = serviceType; ResourceARN = resourceARN; Attributes = attributes; Reservior = new Reservior(); Statistics = new Statistics(); }
private SamplingResponse ProcessMatchedRule(SamplingRule sampleRule, TimeStamp time) { bool shouldSample = true; Reservior reservior = sampleRule.Reservior; SamplingResponse sampleResult = null; sampleRule.IncrementRequestCount(); // increment request counter for matched rule ReserviorDecision reserviorDecision = reservior.BorrowOrTake(time, sampleRule.CanBorrow); // check if we can borrow or take from reservior if (reserviorDecision == ReserviorDecision.Borrow) { sampleRule.IncrementBorrowCount(); } else if (reserviorDecision == ReserviorDecision.Take) { sampleRule.IncrementSampledCount(); } else if (ThreadSafeRandom.NextDouble() <= sampleRule.GetRate()) // compute based on fixed rate { sampleRule.IncrementSampledCount(); } else { shouldSample = false; } if (shouldSample) { sampleResult = new SamplingResponse(sampleRule.RuleName, SampleDecision.Sampled); } else { sampleResult = new SamplingResponse(SampleDecision.NotSampled); } return(sampleResult); }
// Migrate all stateful attributes from the old rule internal void Merge(SamplingRule oldRule) { Statistics.CopyFrom(oldRule.Statistics); Reservior.CopyFrom(oldRule.Reservior); oldRule = null; }