Skip to content

Commit

Permalink
replace secrets with random
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewqian2001datadog committed Dec 12, 2024
1 parent a5d4b15 commit 7cf00b1
Showing 1 changed file with 2 additions and 7 deletions.
9 changes: 2 additions & 7 deletions datadog/dogstatsd/buffered_metrics_context.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from threading import Lock
import secrets
import random


class BufferedMetricContexts:
Expand All @@ -8,7 +8,6 @@ def __init__(self, buffered_metric_type):
self.lock = Lock()
self.values = {}
self.buffered_metric_type = buffered_metric_type
self.random = secrets

def flush(self):
metrics = []
Expand All @@ -26,14 +25,11 @@ def flush(self):
def sample(self, name, value, tags, rate, context_key):
"""Sample a metric and store it if it meets the criteria."""
keeping_sample = self.should_sample(rate)
print("keeping sample is ", keeping_sample)
print("context_key is ", context_key)
with self.lock:
if context_key not in self.values:
# Create a new metric if it doesn't exist
self.values[context_key] = self.buffered_metric_type(name, tags, rate)
metric = self.values[context_key]
print("values are :", self.values.keys())
if keeping_sample:
metric.maybe_keep_sample(value)
else:
Expand All @@ -43,9 +39,8 @@ def should_sample(self, rate):
"""Determine if a sample should be kept based on the specified rate."""
if rate >= 1:
return True
return secrets.SystemRandom().random() < rate
return random.random() < rate # Replace `secrets` with `random`

def get_nb_context(self):
"""Return the number of contexts."""
return self.nb_context

0 comments on commit 7cf00b1

Please sign in to comment.