Skip to content

Commit 815ddd9

Browse files
authored
Refactored DJB2 code for Application Insights Sampler (#42210)
* Refactored DJB2 code for Application Insights Sampler * Updated CHANGELOG
1 parent e7736e3 commit 815ddd9

File tree

3 files changed

+7
-24
lines changed

3 files changed

+7
-24
lines changed

sdk/monitor/azure-monitor-opentelemetry-exporter/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
([#41971](https://github.com/Azure/azure-sdk-for-python/pull/41971))
2525
- Added RateLimited Sampler
2626
([#41954](https://github.com/Azure/azure-sdk-for-python/pull/41954))
27+
- Refactored Application Insights Sampler Code
28+
([#42210](https://github.com/Azure/azure-sdk-for-python/pull/42210))
2729

2830
- Support AI Foundry by Handling GEN_AI_SYSTEM Attributes with [Spec](https://github.com/aep-health-and-standards/Telemetry-Collection-Spec/blob/main/ApplicationInsights/genai_semconv_mapping.md) ([#41705](https://github.com/Azure/azure-sdk-for-python/pull/41705))
2931

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

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
# Licensed under the MIT License.
33
from typing import Optional, Sequence
44

5-
# pylint:disable=no-name-in-module
6-
from fixedint import Int32
7-
85
from opentelemetry.context import Context
96
from opentelemetry.trace import Link, SpanKind, format_trace_id
107
from opentelemetry.sdk.trace.sampling import (
@@ -16,12 +13,10 @@
1613
from opentelemetry.trace.span import TraceState
1714
from opentelemetry.util.types import Attributes
1815

19-
from azure.monitor.opentelemetry.exporter._constants import _SAMPLE_RATE_KEY
16+
from azure.monitor.opentelemetry.exporter.export.trace._utils import _get_DJB2_sample_score
2017

18+
from azure.monitor.opentelemetry.exporter._constants import _SAMPLE_RATE_KEY
2119

22-
_HASH = 5381
23-
_INTEGER_MAX: int = Int32.maxval
24-
_INTEGER_MIN: int = Int32.minval
2520

2621

2722
# Sampler is responsible for the following:
@@ -57,7 +52,7 @@ def should_sample(
5752
decision = Decision.RECORD_AND_SAMPLE
5853
else:
5954
# Determine if should sample from ratio and traceId
60-
sample_score = self._get_DJB2_sample_score(format_trace_id(trace_id).lower())
55+
sample_score = _get_DJB2_sample_score(format_trace_id(trace_id).lower())
6156
if sample_score < self._ratio:
6257
decision = Decision.RECORD_AND_SAMPLE
6358
else:
@@ -72,20 +67,6 @@ def should_sample(
7267
_get_parent_trace_state(parent_context), # type: ignore
7368
)
7469

75-
def _get_DJB2_sample_score(self, trace_id_hex: str) -> float:
76-
# This algorithm uses 32bit integers
77-
hash_value = Int32(_HASH)
78-
for char in trace_id_hex:
79-
hash_value = ((hash_value << 5) + hash_value) + ord(char)
80-
81-
if hash_value == _INTEGER_MIN:
82-
hash_value = int(_INTEGER_MAX)
83-
else:
84-
hash_value = abs(hash_value)
85-
86-
# divide by _INTEGER_MAX for value between 0 and 1 for sampling score
87-
return float(hash_value) / _INTEGER_MAX
88-
8970
def get_description(self) -> str:
9071
return "ApplicationInsightsSampler{}".format(self._ratio)
9172

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ def test_invalid_ratio(self):
2626
self.assertRaises(ValueError, lambda: ApplicationInsightsSampler(1.01))
2727
self.assertRaises(ValueError, lambda: ApplicationInsightsSampler(-0.01))
2828

29-
@mock.patch.object(ApplicationInsightsSampler, "_get_DJB2_sample_score")
29+
@mock.patch("azure.monitor.opentelemetry.exporter.export.trace._sampling._get_DJB2_sample_score")
3030
def test_should_sample(self, score_mock):
3131
sampler = ApplicationInsightsSampler(0.75)
3232
score_mock.return_value = 0.7
3333
result = sampler.should_sample(None, 0, "test")
3434
self.assertEqual(result.attributes["_MS.sampleRate"], 75)
3535
self.assertTrue(result.decision.is_sampled())
3636

37-
@mock.patch.object(ApplicationInsightsSampler, "_get_DJB2_sample_score")
37+
@mock.patch("azure.monitor.opentelemetry.exporter.export.trace._sampling._get_DJB2_sample_score")
3838
def test_should_sample_not_sampled(self, score_mock):
3939
sampler = ApplicationInsightsSampler(0.5)
4040
score_mock.return_value = 0.7

0 commit comments

Comments
 (0)