-
Notifications
You must be signed in to change notification settings - Fork 247
Implement Connection Strings for Azure Exporters #767
Changes from all commits
06c3a07
02f47e1
ce9233c
a349111
8a87ead
8d7a282
1453830
00c2a3a
4ab7f1f
a07b17e
0d30076
1e811e1
bfd81ea
98f66ea
81d573f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,14 +17,81 @@ | |
|
|
||
| from opencensus.ext.azure.common.protocol import BaseObject | ||
|
|
||
| INGESTION_ENDPOINT = 'ingestionendpoint' | ||
| INSTRUMENTATION_KEY = 'instrumentationkey' | ||
|
|
||
|
|
||
| def process_options(options): | ||
| code_cs = parse_connection_string(options.connection_string) | ||
| code_ikey = options.instrumentation_key | ||
| env_cs = parse_connection_string( | ||
| os.getenv('APPLICATIONINSIGHTS_CONNECTION_STRING')) | ||
| env_ikey = os.getenv('APPINSIGHTS_INSTRUMENTATIONKEY') | ||
|
|
||
| # The priority of which value takes on the instrumentation key is: | ||
| # 1. Key from explicitly passed in connection string | ||
| # 2. Key from explicitly passed in instrumentation key | ||
| # 3. Key from connection string in environment variable | ||
| # 4. Key from instrumentation key in environment variable | ||
| options.instrumentation_key = code_cs.get(INSTRUMENTATION_KEY) \ | ||
| or code_ikey \ | ||
| or env_cs.get(INSTRUMENTATION_KEY) \ | ||
| or env_ikey | ||
| # The priority of the ingestion endpoint is as follows: | ||
| # 1. The endpoint explicitly passed in connection string | ||
| # 2. The endpoint from the connection string in environment variable | ||
| # 3. The default breeze endpoint | ||
| endpoint = code_cs.get(INGESTION_ENDPOINT) \ | ||
| or env_cs.get(INGESTION_ENDPOINT) \ | ||
| or 'https://dc.services.visualstudio.com' | ||
| options.endpoint = endpoint + '/v2/track' | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this endpoint encoded?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you explain more about how the endpoint would be encoded?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For example, do you expect the endpoint to be something like https://zh.wikipedia.org/wiki/%E6%A3%89%E5%B0%BE%E5%85%94%E5%B1%9E ?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do not believe that an encoded endpoint would be intentionally passed in. According to Mothra, none of the current endpoint configurations handle strings in any special way, so I believe it's safe to leave the url string as is. |
||
|
|
||
|
|
||
| def parse_connection_string(connection_string): | ||
| if connection_string is None: | ||
| return {} | ||
| try: | ||
| pairs = connection_string.split(';') | ||
| result = dict(s.split('=') for s in pairs) | ||
| # Convert keys to lower-case due to case type-insensitive checking | ||
| result = {key.lower(): value for key, value in result.items()} | ||
| except Exception: | ||
| raise ValueError('Invalid connection string') | ||
| # Validate authorization | ||
| auth = result.get('authorization') | ||
| if auth is not None and auth.lower() != 'ikey': | ||
| raise ValueError('Invalid authorization mechanism') | ||
| # Construct the ingestion endpoint if not passed in explicitly | ||
| if result.get(INGESTION_ENDPOINT) is None: | ||
| endpoint_suffix = '' | ||
| location_prefix = '' | ||
| suffix = result.get('endpointsuffix') | ||
| if suffix is not None: | ||
| endpoint_suffix = suffix | ||
| # Get regional information if provided | ||
| prefix = result.get('location') | ||
| if prefix is not None: | ||
| location_prefix = prefix + '.' | ||
| endpoint = 'https://' + location_prefix + 'dc.' + endpoint_suffix | ||
| result[INGESTION_ENDPOINT] = endpoint | ||
| else: | ||
| # Default to None if cannot construct | ||
| result[INGESTION_ENDPOINT] = None | ||
| return result | ||
|
|
||
|
|
||
| class Options(BaseObject): | ||
| def __init__(self, *args, **kwargs): | ||
| super(Options, self).__init__(*args, **kwargs) | ||
| process_options(self) | ||
|
|
||
| _default = BaseObject( | ||
| connection_string=None, | ||
| enable_standard_metrics=True, | ||
| endpoint='https://dc.services.visualstudio.com/v2/track', | ||
| export_interval=15.0, | ||
| grace_period=5.0, | ||
| instrumentation_key=os.getenv('APPINSIGHTS_INSTRUMENTATIONKEY', None), | ||
| instrumentation_key=None, | ||
| max_batch_size=100, | ||
| minimum_retry_interval=60, # minimum retry interval in seconds | ||
| proxy=None, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.