2323from opencensus .ext .azure .common import Options
2424from opencensus .ext .azure .common .transport import _requests_map
2525from opencensus .ext .azure .common .version import __version__ as ext_version
26- from opencensus .ext .azure .metrics_exporter import statsbeat_metrics
26+ from opencensus .ext .azure .metrics_exporter import (
27+ MetricsExporter ,
28+ statsbeat_metrics ,
29+ )
2730from opencensus .ext .azure .metrics_exporter .statsbeat_metrics .statsbeat import (
31+ _DEFAULT_EU_STATS_CONNECTION_STRING ,
32+ _DEFAULT_NON_EU_STATS_CONNECTION_STRING ,
2833 _ENDPOINT_TYPES ,
2934 _FEATURE_TYPES ,
3035 _RP_NAMES ,
3742 _get_feature_properties ,
3843 _get_network_properties ,
3944 _get_retry_count_value ,
45+ _get_stats_connection_string ,
4046 _get_success_count_value ,
4147 _get_throttle_count_value ,
4248 _shorten_host ,
@@ -78,6 +84,7 @@ class TestStatsbeatMetrics(unittest.TestCase):
7884 def setUp (self ):
7985 # pylint: disable=protected-access
8086 statsbeat_metrics ._STATSBEAT_METRICS = None
87+ statsbeat_metrics ._STATSBEAT_EXPORTER = None
8188
8289 def test_producer_ctor (self ):
8390 # pylint: disable=protected-access
@@ -121,6 +128,12 @@ def test_collect_statsbeat_metrics(self, thread_mock, stats_mock):
121128 statsbeat_metrics ._AzureStatsbeatMetricsProducer
122129 )
123130 )
131+ self .assertTrue (
132+ isinstance (
133+ statsbeat_metrics ._STATSBEAT_EXPORTER ,
134+ MetricsExporter ,
135+ )
136+ )
124137 self .assertEqual (
125138 statsbeat_metrics ._STATSBEAT_METRICS ._statsbeat ._instrumentation_key , "ikey" ) # noqa: E501
126139 thread_mock .assert_called_once ()
@@ -137,6 +150,74 @@ def test_collect_statsbeat_metrics_exists(self, thread_mock, stats_mock):
137150 thread_mock .assert_not_called ()
138151 stats_mock .assert_not_called ()
139152
153+ @mock .patch .object (_StatsbeatMetrics , 'get_initial_metrics' )
154+ @mock .patch ('opencensus.metrics.transport.get_exporter_thread' )
155+ def test_collect_statsbeat_metrics_non_eu (self , thread_mock , stats_mock ):
156+ # pylint: disable=protected-access
157+ cs = "InstrumentationKey=1aa11111-bbbb-1ccc-8ddd-eeeeffff3333;IngestionEndpoint=https://westus-0.in.applicationinsights.azure.com/" # noqa: E501
158+ non_eu = Options (
159+ connection_string = cs
160+ )
161+ with mock .patch .dict (
162+ os .environ , {
163+ "APPLICATION_INSIGHTS_STATS_CONNECTION_STRING" : "" ,
164+ }):
165+ statsbeat_metrics .collect_statsbeat_metrics (non_eu )
166+ self .assertTrue (
167+ isinstance (
168+ statsbeat_metrics ._STATSBEAT_METRICS ,
169+ statsbeat_metrics ._AzureStatsbeatMetricsProducer
170+ )
171+ )
172+ self .assertTrue (
173+ isinstance (
174+ statsbeat_metrics ._STATSBEAT_EXPORTER ,
175+ MetricsExporter ,
176+ )
177+ )
178+ self .assertEqual (
179+ statsbeat_metrics ._STATSBEAT_EXPORTER .options .instrumentation_key , # noqa: E501
180+ _DEFAULT_NON_EU_STATS_CONNECTION_STRING .split (";" )[0 ].split ("=" )[1 ] # noqa: E501
181+ )
182+ self .assertEqual (
183+ statsbeat_metrics ._STATSBEAT_EXPORTER .options .endpoint ,
184+ _DEFAULT_NON_EU_STATS_CONNECTION_STRING .split (";" )[1 ].split ("=" )[1 ] # noqa: E501
185+ )
186+
187+ @mock .patch .object (_StatsbeatMetrics , 'get_initial_metrics' )
188+ @mock .patch ('opencensus.metrics.transport.get_exporter_thread' )
189+ def test_collect_statsbeat_metrics_eu (self , thread_mock , stats_mock ):
190+ # pylint: disable=protected-access
191+ cs = "InstrumentationKey=1aa11111-bbbb-1ccc-8ddd-eeeeffff3333;IngestionEndpoint=https://northeurope-0.in.applicationinsights.azure.com/" # noqa: E501
192+ eu = Options (
193+ connection_string = cs
194+ )
195+ with mock .patch .dict (
196+ os .environ , {
197+ "APPLICATION_INSIGHTS_STATS_CONNECTION_STRING" : "" ,
198+ }):
199+ statsbeat_metrics .collect_statsbeat_metrics (eu )
200+ self .assertTrue (
201+ isinstance (
202+ statsbeat_metrics ._STATSBEAT_METRICS ,
203+ statsbeat_metrics ._AzureStatsbeatMetricsProducer
204+ )
205+ )
206+ self .assertTrue (
207+ isinstance (
208+ statsbeat_metrics ._STATSBEAT_EXPORTER ,
209+ MetricsExporter ,
210+ )
211+ )
212+ self .assertEqual (
213+ statsbeat_metrics ._STATSBEAT_EXPORTER .options .instrumentation_key , # noqa: E501
214+ _DEFAULT_EU_STATS_CONNECTION_STRING .split (";" )[0 ].split ("=" )[1 ] # noqa: E501
215+ )
216+ self .assertEqual (
217+ statsbeat_metrics ._STATSBEAT_EXPORTER .options .endpoint ,
218+ _DEFAULT_EU_STATS_CONNECTION_STRING .split (";" )[1 ].split ("=" )[1 ] # noqa: E501
219+ )
220+
140221 @mock .patch (
141222 'opencensus.ext.azure.metrics_exporter.statsbeat_metrics.statsbeat._get_feature_properties' ) # noqa: E501
142223 @mock .patch (
@@ -149,6 +230,7 @@ def test_statsbeat_metric_init(self, attach_mock, network_mock, feature_mock):
149230 self .assertEqual (len (metric ._vm_data ), 0 )
150231 self .assertTrue (metric ._vm_retry )
151232 self .assertEqual (metric ._instrumentation_key , "ikey" )
233+ self .assertEqual (metric ._feature , 1 )
152234 self .assertTrue (
153235 isinstance (
154236 metric ._attach_metric ,
@@ -327,7 +409,7 @@ def test_get_feature_metric(self):
327409 self .assertEqual (
328410 properties [8 ].value , ext_version ) # noqa: E501
329411
330- def test_get_feature_metric_wtih_aad (self ):
412+ def test_get_feature_metric_with_aad (self ):
331413 aad_options = Options (
332414 instrumentation_key = "ikey" ,
333415 enable_local_storage = True ,
@@ -584,7 +666,7 @@ def test_get_azure_compute_metadata_not_vm_timeout(self):
584666 self .assertEqual (len (stats ._vm_data ), 0 )
585667 self .assertFalse (stats ._vm_retry )
586668
587- def test_get_azure_compute_metadata__vm_retry (self ):
669+ def test_get_azure_compute_metadata_vm_retry (self ):
588670 with mock .patch (
589671 'requests.get' ,
590672 throw (requests .exceptions .RequestException )
@@ -612,3 +694,39 @@ def test_shorten_host(self):
612694 self .assertEqual (_shorten_host (url ), "fakehost" )
613695 url = "http://fakehost-5/"
614696 self .assertEqual (_shorten_host (url ), "fakehost-5" )
697+
698+ def test_get_stats_connection_string_env (self ):
699+ cs = "InstrumentationKey=1aa11111-bbbb-1ccc-8ddd-eeeeffff3333;IngestionEndpoint=https://westus-0.in.applicationinsights.azure.com/" # noqa: E501
700+ with mock .patch .dict (
701+ os .environ , {
702+ "APPLICATION_INSIGHTS_STATS_CONNECTION_STRING" : cs
703+ }
704+ ):
705+ stats_cs = _get_stats_connection_string (_OPTIONS .endpoint )
706+ self .assertEqual (stats_cs , cs )
707+
708+ def test_get_stats_connection_string_non_eu (self ):
709+ with mock .patch .dict (
710+ os .environ , {
711+ "APPLICATION_INSIGHTS_STATS_CONNECTION_STRING" : ""
712+ }
713+ ):
714+ cs = "InstrumentationKey=1aa11111-bbbb-1ccc-8ddd-eeeeffff3333;IngestionEndpoint=https://westus-0.in.applicationinsights.azure.com/" # noqa: E501
715+ non_eu = Options (
716+ connection_string = cs ,
717+ )
718+ stats_cs = _get_stats_connection_string (non_eu .endpoint )
719+ self .assertEqual (stats_cs , _DEFAULT_NON_EU_STATS_CONNECTION_STRING )
720+
721+ def test_get_stats_connection_string_eu (self ):
722+ with mock .patch .dict (
723+ os .environ , {
724+ "APPLICATION_INSIGHTS_STATS_CONNECTION_STRING" : ""
725+ }
726+ ):
727+ cs = "InstrumentationKey=1aa11111-bbbb-1ccc-8ddd-eeeeffff3333;IngestionEndpoint=https://northeurope-0.in.applicationinsights.azure.com/" # noqa: E501
728+ eu = Options (
729+ connection_string = cs ,
730+ )
731+ stats_cs = _get_stats_connection_string (eu .endpoint )
732+ self .assertEqual (stats_cs , _DEFAULT_EU_STATS_CONNECTION_STRING )
0 commit comments