def test_invalid_key_suffix(self):
     self.assertRaises(
         ValueError,
         lambda: ConnectionStringParser(
             connection_string=
             "InstrumentationKey=1234abcd-5678-4efa-8abc-1234567890abtest"),
     )
 def test_invalid_key_section5_length(self):
     self.assertRaises(
         ValueError,
         lambda: ConnectionStringParser(
             connection_string=
             "InstrumentationKey=234abcd-678-4efa-8abc-11234567890ab"),
     )
 def test_process_options_ikey_env_cs(self):
     os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"] = (
         "Authorization=ikey;InstrumentationKey=" +
         self._valid_instrumentation_key)
     os.environ["APPINSIGHTS_INSTRUMENTATIONKEY"] = "101112"
     parser = ConnectionStringParser(connection_string=None)
     self.assertEqual(parser.instrumentation_key,
                      self._valid_instrumentation_key)
    def __init__(self, **kwargs: Any) -> None:
        """Azure Monitor base exporter for OpenTelemetry.

        :keyword str api_version: The service API version used. Defaults to latest.
        :rtype: None
        """
        parsed_connection_string = ConnectionStringParser(
            kwargs.get('connection_string'))

        self._instrumentation_key = parsed_connection_string.instrumentation_key
        self._timeout = 10.0  # networking timeout in seconds
        self._api_version = kwargs.get('api_version') or _SERVICE_API_LATEST
        self._consecutive_redirects = 0  # To prevent circular redirects

        temp_suffix = self._instrumentation_key or ""
        default_storage_path = os.path.join(tempfile.gettempdir(),
                                            _TEMPDIR_PREFIX + temp_suffix)
        config = AzureMonitorClientConfiguration(
            parsed_connection_string.endpoint, **kwargs)
        policies = [
            RequestIdPolicy(**kwargs),
            config.headers_policy,
            config.user_agent_policy,
            config.proxy_policy,
            ContentDecodePolicy(**kwargs),
            # Handle redirects in exporter, set new endpoint if redirected
            RedirectPolicy(permit_redirects=False),
            config.retry_policy,
            config.authentication_policy,
            config.custom_hook_policy,
            config.logging_policy,
            # Explicitly disabling to avoid infinite loop of Span creation when data is exported
            # DistributedTracingPolicy(**kwargs),
            config.http_logging_policy or HttpLoggingPolicy(**kwargs)
        ]
        self.client = AzureMonitorClient(
            host=parsed_connection_string.endpoint,
            connection_timeout=self._timeout,
            policies=policies,
            **kwargs)
        self.storage = LocalFileStorage(
            path=default_storage_path,
            max_size=50 * 1024 * 1024,  # Maximum size in bytes.
            maintenance_period=60,  # Maintenance interval in seconds.
            retention_period=7 * 24 * 60 * 60,  # Retention period in seconds
        )
 def test_invalid_key_empty(self):
     self.assertRaises(ValueError,
                       lambda: ConnectionStringParser(connection_string=""))
 def test_parse_connection_string_suffix_no_location(self):
     parser = ConnectionStringParser(
         connection_string=
         "Authorization=ikey;EndpointSuffix=123;InstrumentationKey=" +
         self._valid_instrumentation_key, )
     self.assertEqual(parser.endpoint, "https://dc.123")
 def test_parse_connection_string_invalid_auth(self):
     self.assertRaises(
         ValueError,
         lambda: ConnectionStringParser(connection_string=
                                        "Authorization=asd", ),
     )
 def test_parse_connection_string_invalid(self):
     self.assertRaises(
         ValueError,
         lambda: ConnectionStringParser(connection_string="asd"))
 def test_process_options_endpoint_default(self):
     parser = ConnectionStringParser(
         connection_string=self._valid_connection_string, )
     self.assertEqual(parser.endpoint,
                      "https://dc.services.visualstudio.com")
 def test_process_options_endpoint_env_cs(self):
     os.environ[
         "APPLICATIONINSIGHTS_CONNECTION_STRING"] = "Authorization=ikey;IngestionEndpoint=456;InstrumentationKey=" + self._valid_instrumentation_key
     parser = ConnectionStringParser(connection_string=None, )
     self.assertEqual(parser.endpoint, "456")
 def test_validate_connection_String(self):
     parser = ConnectionStringParser(
         connection_string=self._valid_connection_string)
     self.assertEqual(parser._connection_string,
                      self._valid_connection_string)
 def test_process_options_ikey_env_ikey(self):
     os.environ[
         "APPINSIGHTS_INSTRUMENTATIONKEY"] = self._valid_instrumentation_key
     parser = ConnectionStringParser(connection_string=None)
     self.assertEqual(parser.instrumentation_key,
                      self._valid_instrumentation_key)