File: test_sampling.py

package info (click to toggle)
python-azure 20230112%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 749,544 kB
  • sloc: python: 6,815,827; javascript: 287; makefile: 195; xml: 109; sh: 105
file content (40 lines) | stat: -rw-r--r-- 1,550 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import unittest
from unittest import mock

from azure.monitor.opentelemetry.exporter.export.trace._sampling import (
    ApplicationInsightsSampler,
)

# pylint: disable=protected-access
class TestApplicationInsightsSampler(unittest.TestCase):

    def test_constructor(self):
        sampler = ApplicationInsightsSampler(0.75)
        self.assertEqual(sampler._ratio, 0.75)
        self.assertEqual(sampler._sample_rate, 75)

    def test_invalid_ratio(self):
        self.assertRaises(
            ValueError, lambda: ApplicationInsightsSampler(1.01)
        )
        self.assertRaises(
            ValueError, lambda: ApplicationInsightsSampler(-0.01)
        )

    @mock.patch.object(ApplicationInsightsSampler, '_get_DJB2_sample_score')
    def test_should_sample(self, score_mock):
        sampler = ApplicationInsightsSampler(0.75)
        score_mock.return_value = 0.7
        result = sampler.should_sample(None, 0, "test")
        self.assertEqual(result.attributes["_MS.sampleRate"], 75)
        self.assertTrue(result.decision.is_sampled())

    @mock.patch.object(ApplicationInsightsSampler, '_get_DJB2_sample_score')
    def test_should_sample_not_sampled(self, score_mock):
        sampler = ApplicationInsightsSampler(0.5)
        score_mock.return_value = 0.7
        result = sampler.should_sample(None, 0, "test")
        self.assertEqual(result.attributes["_MS.sampleRate"], 50)
        self.assertFalse(result.decision.is_sampled())