Skip to content

Commit 835b2de

Browse files
committed
Add Span sampled class for DT
1 parent f0ccf89 commit 835b2de

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

newrelic/core/stats_engine.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,50 @@ def merge(self, other_data_set, priority=None):
446446
self.num_seen += other_data_set.num_seen - other_data_set.num_samples
447447

448448

449+
class SpanSampledDataSet:
450+
def __init__(self, capacity=100):
451+
self.pq = []
452+
self.heap = False
453+
self.capacity = capacity
454+
self.num_seen = 0
455+
self.ft_num_seen = 0
456+
457+
if capacity <= 0:
458+
459+
def add(*args, **kwargs):
460+
self.num_seen += 1
461+
462+
self.add = add
463+
464+
@property
465+
def ft_samples(self):
466+
return (x[-1] if is_ft(x[-1]) for x in self.pq)
467+
468+
def is_ft(sample):
469+
# It's a FT span if it's an exit or entry span.
470+
return (len(sample) > 3 and sample[3]) or not sample[0].get("parentId")
471+
472+
def add(self, sample, priority=None):
473+
self.num_seen += 1
474+
if is_ft(sample:
475+
self.ft_num_seen += 1
476+
477+
if priority is None:
478+
priority = random.random() # noqa: S311
479+
480+
entry = (priority, self.num_seen, sample)
481+
if self.num_seen == self.capacity:
482+
self.pq.append(entry)
483+
self.heap = self.heap or heapify(self.pq) or True
484+
elif not self.heap:
485+
self.pq.append(entry)
486+
else:
487+
sampled = self.should_sample(priority)
488+
if not sampled:
489+
return
490+
heapreplace(self.pq, entry)
491+
492+
449493
class LimitedDataSet(list):
450494
def __init__(self, capacity=200):
451495
super().__init__()

0 commit comments

Comments
 (0)