|
3 | 3 | import os
|
4 | 4 | import time
|
5 | 5 | from unittest import TestCase
|
| 6 | +from unittest.mock import patch |
6 | 7 |
|
7 |
| -from amazon.opentelemetry.distro.aws_opentelemetry_configurator import AwsOpenTelemetryConfigurator |
| 8 | +from amazon.opentelemetry.distro.aws_opentelemetry_configurator import ( |
| 9 | + AwsOpenTelemetryConfigurator, |
| 10 | + _custom_import_sampler, |
| 11 | +) |
8 | 12 | from amazon.opentelemetry.distro.aws_opentelemetry_distro import AwsOpenTelemetryDistro
|
| 13 | +from amazon.opentelemetry.distro.sampler._aws_xray_sampling_client import _AwsXRaySamplingClient |
| 14 | +from amazon.opentelemetry.distro.sampler.aws_xray_remote_sampler import AwsXRayRemoteSampler |
9 | 15 | from opentelemetry.environment_variables import OTEL_LOGS_EXPORTER, OTEL_METRICS_EXPORTER, OTEL_TRACES_EXPORTER
|
10 | 16 | from opentelemetry.sdk.environment_variables import OTEL_TRACES_SAMPLER, OTEL_TRACES_SAMPLER_ARG
|
11 | 17 | from opentelemetry.sdk.trace import Span, Tracer, TracerProvider
|
| 18 | +from opentelemetry.sdk.trace.sampling import Sampler |
12 | 19 | from opentelemetry.trace import get_tracer_provider
|
13 | 20 |
|
14 | 21 |
|
@@ -52,3 +59,93 @@ def test_trace_id_ratio_sampler(self):
|
52 | 59 | span.end()
|
53 | 60 | # Configured for 1%, confirm there are at most 5% to account for randomness and reduce test flakiness.
|
54 | 61 | self.assertGreater(0.05, num_sampled / num_spans)
|
| 62 | + |
| 63 | + # Test method for importing xray sampler |
| 64 | + # Cannot test this logic via `aws_otel_configurator.configure()` because that will |
| 65 | + # attempt to setup tracer provider again, which can be only be done once (already done) |
| 66 | + @patch.object(AwsXRayRemoteSampler, "_AwsXRayRemoteSampler__start_sampling_rule_poller", lambda x: None) |
| 67 | + @patch.object(AwsXRayRemoteSampler, "_AwsXRayRemoteSampler__start_sampling_target_poller", lambda x: None) |
| 68 | + def test_import_xray_sampler_without_environment_arguments(self): |
| 69 | + os.environ.pop(OTEL_TRACES_SAMPLER_ARG, None) |
| 70 | + |
| 71 | + # May log http request error as xray sampler will attempt to fetch rules |
| 72 | + xray_sampler: Sampler = _custom_import_sampler(None, resource=None) |
| 73 | + xray_client: _AwsXRaySamplingClient = xray_sampler._AwsXRayRemoteSampler__xray_client |
| 74 | + self.assertEqual(xray_sampler._AwsXRayRemoteSampler__polling_interval, 300) |
| 75 | + self.assertEqual( |
| 76 | + xray_client._AwsXRaySamplingClient__get_sampling_rules_endpoint, "http://127.0.0.1:2000/GetSamplingRules" |
| 77 | + ) |
| 78 | + |
| 79 | + @patch.object(AwsXRayRemoteSampler, "_AwsXRayRemoteSampler__start_sampling_rule_poller", lambda x: None) |
| 80 | + @patch.object(AwsXRayRemoteSampler, "_AwsXRayRemoteSampler__start_sampling_target_poller", lambda x: None) |
| 81 | + def test_import_xray_sampler_with_valid_environment_arguments(self): |
| 82 | + os.environ.pop(OTEL_TRACES_SAMPLER_ARG, None) |
| 83 | + os.environ.setdefault(OTEL_TRACES_SAMPLER_ARG, "endpoint=http://localhost:2000,polling_interval=600") |
| 84 | + |
| 85 | + # May log http request error as xray sampler will attempt to fetch rules |
| 86 | + xray_sampler: Sampler = _custom_import_sampler(None, resource=None) |
| 87 | + xray_client: _AwsXRaySamplingClient = xray_sampler._AwsXRayRemoteSampler__xray_client |
| 88 | + self.assertEqual(xray_sampler._AwsXRayRemoteSampler__polling_interval, 600) |
| 89 | + self.assertEqual( |
| 90 | + xray_client._AwsXRaySamplingClient__get_sampling_rules_endpoint, "http://localhost:2000/GetSamplingRules" |
| 91 | + ) |
| 92 | + |
| 93 | + os.environ.pop(OTEL_TRACES_SAMPLER_ARG, None) |
| 94 | + os.environ.setdefault(OTEL_TRACES_SAMPLER_ARG, "polling_interval=123") |
| 95 | + |
| 96 | + # May log http request error as xray sampler will attempt to fetch rules |
| 97 | + xray_sampler: Sampler = _custom_import_sampler(None, resource=None) |
| 98 | + xray_client: _AwsXRaySamplingClient = xray_sampler._AwsXRayRemoteSampler__xray_client |
| 99 | + self.assertEqual(xray_sampler._AwsXRayRemoteSampler__polling_interval, 123) |
| 100 | + self.assertEqual( |
| 101 | + xray_client._AwsXRaySamplingClient__get_sampling_rules_endpoint, "http://127.0.0.1:2000/GetSamplingRules" |
| 102 | + ) |
| 103 | + |
| 104 | + os.environ.pop(OTEL_TRACES_SAMPLER_ARG, None) |
| 105 | + os.environ.setdefault(OTEL_TRACES_SAMPLER_ARG, "endpoint=http://cloudwatch-agent.amazon-cloudwatch:2000") |
| 106 | + |
| 107 | + # May log http request error as xray sampler will attempt to fetch rules |
| 108 | + xray_sampler: Sampler = _custom_import_sampler(None, resource=None) |
| 109 | + xray_client: _AwsXRaySamplingClient = xray_sampler._AwsXRayRemoteSampler__xray_client |
| 110 | + self.assertEqual(xray_sampler._AwsXRayRemoteSampler__polling_interval, 300) |
| 111 | + self.assertEqual( |
| 112 | + xray_client._AwsXRaySamplingClient__get_sampling_rules_endpoint, |
| 113 | + "http://cloudwatch-agent.amazon-cloudwatch:2000/GetSamplingRules", |
| 114 | + ) |
| 115 | + |
| 116 | + @patch.object(AwsXRayRemoteSampler, "_AwsXRayRemoteSampler__start_sampling_rule_poller", lambda x: None) |
| 117 | + @patch.object(AwsXRayRemoteSampler, "_AwsXRayRemoteSampler__start_sampling_target_poller", lambda x: None) |
| 118 | + def test_import_xray_sampler_with_invalid_environment_arguments(self): |
| 119 | + os.environ.pop(OTEL_TRACES_SAMPLER_ARG, None) |
| 120 | + os.environ.setdefault(OTEL_TRACES_SAMPLER_ARG, "endpoint=h=tt=p://=loca=lho=st:2000,polling_interval=FOOBAR") |
| 121 | + |
| 122 | + # May log http request error as xray sampler will attempt to fetch rules |
| 123 | + xray_sampler: Sampler = _custom_import_sampler(None, resource=None) |
| 124 | + xray_client: _AwsXRaySamplingClient = xray_sampler._AwsXRayRemoteSampler__xray_client |
| 125 | + self.assertEqual(xray_sampler._AwsXRayRemoteSampler__polling_interval, 300) |
| 126 | + self.assertEqual( |
| 127 | + xray_client._AwsXRaySamplingClient__get_sampling_rules_endpoint, |
| 128 | + "h=tt=p://=loca=lho=st:2000/GetSamplingRules", |
| 129 | + ) |
| 130 | + |
| 131 | + os.environ.pop(OTEL_TRACES_SAMPLER_ARG, None) |
| 132 | + os.environ.setdefault(OTEL_TRACES_SAMPLER_ARG, ",,=,==,,===,") |
| 133 | + |
| 134 | + # May log http request error as xray sampler will attempt to fetch rules |
| 135 | + xray_sampler: Sampler = _custom_import_sampler(None, resource=None) |
| 136 | + xray_client: _AwsXRaySamplingClient = xray_sampler._AwsXRayRemoteSampler__xray_client |
| 137 | + self.assertEqual(xray_sampler._AwsXRayRemoteSampler__polling_interval, 300) |
| 138 | + self.assertEqual( |
| 139 | + xray_client._AwsXRaySamplingClient__get_sampling_rules_endpoint, "http://127.0.0.1:2000/GetSamplingRules" |
| 140 | + ) |
| 141 | + |
| 142 | + os.environ.pop(OTEL_TRACES_SAMPLER_ARG, None) |
| 143 | + os.environ.setdefault(OTEL_TRACES_SAMPLER_ARG, "endpoint,polling_interval") |
| 144 | + |
| 145 | + # May log http request error as xray sampler will attempt to fetch rules |
| 146 | + xray_sampler: Sampler = _custom_import_sampler(None, resource=None) |
| 147 | + xray_client: _AwsXRaySamplingClient = xray_sampler._AwsXRayRemoteSampler__xray_client |
| 148 | + self.assertEqual(xray_sampler._AwsXRayRemoteSampler__polling_interval, 300) |
| 149 | + self.assertEqual( |
| 150 | + xray_client._AwsXRaySamplingClient__get_sampling_rules_endpoint, "http://127.0.0.1:2000/GetSamplingRules" |
| 151 | + ) |
0 commit comments