Skip to content

Commit 5e4ac33

Browse files
committed
Fixed djb2 arguments
1 parent 8d1e0f4 commit 5e4ac33

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/export/trace/_rate_limited_sampling.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def should_sample(
104104
return parent_result
105105

106106
sampling_percentage = self._sampling_percentage_generator.get()
107-
sampling_score = _get_DJB2_sample_score(format_trace_id(trace_id).lower())
107+
sampling_score = _get_DJB2_sample_score(format_trace_id(trace_id).lower(), "rate_limited")
108108

109109
if sampling_score < sampling_percentage:
110110
decision = Decision.RECORD_AND_SAMPLE

sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/export/trace/_utils.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ def _get_url_for_http_request(attributes: Attributes) -> Optional[str]:
340340
)
341341
return url
342342

343-
def _get_DJB2_sample_score(trace_id_hex: str) -> float:
343+
def _get_DJB2_sample_score(trace_id_hex: str, sampler_type) -> float:
344344
# This algorithm uses 32bit integers
345345
hash_value = Int32(_SAMPLING_HASH)
346346
for char in trace_id_hex:
@@ -351,8 +351,12 @@ def _get_DJB2_sample_score(trace_id_hex: str) -> float:
351351
else:
352352
hash_value = abs(hash_value)
353353

354-
# divide by _INTEGER_MAX for value between 0 and 1, then multiply by 100 for percentage
355-
return 100.0 * (float(hash_value) / _INTEGER_MAX)
354+
if sampler_type == "rate_limited":
355+
# divide by _INTEGER_MAX for value between 0 and 1, then multiply by 100 for percentage
356+
return 100.0 * (float(hash_value) / _INTEGER_MAX)
357+
else:
358+
# divide by _INTEGER_MAX for value between 0 and 1 for sampling score
359+
return float(hash_value) / _INTEGER_MAX
356360

357361
def _round_down_to_nearest(sampling_percentage: float) -> float:
358362
if sampling_percentage == 0:

sdk/monitor/azure-monitor-opentelemetry-exporter/tests/trace/test_rate_limited_sampling.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ def test_djb2_hash_consistency(self):
469469

470470
trace_id = "test-trace-id-12345"
471471

472-
scores = [_get_DJB2_sample_score(trace_id) for _ in range(10)]
472+
scores = [_get_DJB2_sample_score(trace_id, "rate_limited") for _ in range(10)]
473473

474474
self.assertTrue(all(score == scores[0] for score in scores))
475475

@@ -485,7 +485,7 @@ def test_djb2_hash_edge_cases(self):
485485

486486
for trace_id in edge_cases:
487487
with self.subTest(trace_id=trace_id):
488-
score = _get_DJB2_sample_score(trace_id)
488+
score = _get_DJB2_sample_score(trace_id, "rate_limited")
489489
self.assertIsInstance(score, float)
490490
self.assertGreaterEqual(score, 0)
491491
self.assertLess(score, 100)

0 commit comments

Comments
 (0)