Skip to content

Commit 5655518

Browse files
mrm9084xiangyan99
andauthored
App Configuration Provider Update (Azure#30477)
* Update async_key_vault_reference_provided_clients_sample.py * Adding disable * setting retry backoff max * Update CHANGELOG.md * Fixing formatting * Fixed None issue * Fixing kwargs usage * Update _azureappconfigurationprovider.py * fixed retry * Update README.md * Update README.md * setting max time to 2 and max time to 1 * Update _azureappconfigurationproviderasync.py * Fixed Whitespace and Updated Change Log * Update sdk/appconfiguration/azure-appconfiguration-provider/azure/appconfiguration/provider/_azureappconfigurationprovider.py Co-authored-by: Xiang Yan <[email protected]> * Review comments, Fixing constant, retry usage * Update _azureappconfigurationproviderasync.py * Update CHANGELOG.md * Updating async too, formatting * changed get_correlation_context into get_header * Cleaning up user agent. fixed indentation on async * fixing headers * Updated env check * Update _azureappconfigurationproviderasync.py * Clean up imports * Update _azureappconfigurationprovider.py --------- Co-authored-by: Xiang Yan <[email protected]>
1 parent f5a1b44 commit 5655518

File tree

5 files changed

+93
-41
lines changed

5 files changed

+93
-41
lines changed

sdk/appconfiguration/azure-appconfiguration-provider/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010

1111
### Other Changes
1212

13+
* Updated to use AZURE_APP_CONFIGURATION_TRACING_DISABLED environment variable to disable tracing.
14+
* Changed the maximum number of retries to 2 from the default of 3 retries.
15+
* Changed the maximum back off time between retries to 1 minute from the default of 2 minutes.
16+
1317
## 1.0.0 (2023-03-09)
1418

1519
### Breaking Changes

sdk/appconfiguration/azure-appconfiguration-provider/README.md

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,20 @@ Alternatively, get the connection string from the Azure Portal.
2121
You can create a client with a connection string:
2222

2323
```python
24+
from azure.appconfiguration.provider import load
25+
2426
config = load(connection_string="your-connection-string")
2527
```
2628

2729
or with AAD:
2830

2931
```python
32+
from azure.appconfiguration.provider import load
33+
3034
config = load(endpoint="your-endpoint", credential=DefaultAzureCredential())
3135
```
3236

33-
these providers will by default load all configurations with `(No Label)` from your configuration store.
37+
these providers will by default load all configurations with `(No Label)` from your configuration store into a dictionary of key/values.
3438

3539
### Features
3640

@@ -59,16 +63,23 @@ List of features we are going to add to the Python Provider in the future.
5963
You can refine or expand the configurations loaded from your store by using `SettingSelector`s. Setting selectors provide a way to pass a key filter and label filter into the provider.
6064

6165
```python
66+
from azure.appconfiguration.provider import load, SettingSelector
67+
from azure.identity import DefaultAzureCredential
68+
6269
selects = {SettingSelector(key_filter="*", label_filter="\0"), SettingSelector(key_filter="*", label_filter="dev")}
6370
config = load(endpoint=endpoint, credential=DefaultAzureCredential(), selects=selects)
6471
```
72+
6573
In this example all configuration with empty label and the dev label are loaded. Because the dev selector is listed last, any configurations from dev take priority over those with `(No Label)` when duplicates are found.
6674

6775
### Trimming Keys
6876

6977
You can trim the prefix off of keys by providing a list of trimmed key prefixes to the provider. For example, if you have the key(s) like `/application/message` in your configuration store, you could trim `/application/` from them.
7078

7179
```python
80+
from azure.appconfiguration.provider import load
81+
from azure.identity import DefaultAzureCredential
82+
7283
trim_prefixes={"/application/"}
7384
config = load(endpoint=endpoint, credential=DefaultAzureCredential(), trim_prefixes=trim_prefixes)
7485
print(config["message"])
@@ -83,14 +94,21 @@ Key Vault References can be resolved by providing credentials to your key vault
8394
You can provide `AzureAppConfigurationKeyVaultOptions` with a credential and all key vault references will be resolved with it. The provider will attempt to connect to any key vault referenced with the credential provided.
8495

8596
```python
97+
from azure.appconfiguration.provider import load, AzureAppConfigurationKeyVaultOptions
98+
from azure.identity import DefaultAzureCredential
99+
86100
key_vault_options = AzureAppConfigurationKeyVaultOptions(credential=DefaultAzureCredential())
87101
config = load(endpoint=endpoint, credential=DefaultAzureCredential(), key_vault_options=key_vault_options)
88102
```
103+
89104
### With Clients
90105

91106
You can provide `AzureAppConfigurationKeyVaultOptions` with a list of `SecretClients`.
92107

93108
```python
109+
from azure.appconfiguration.provider import load, AzureAppConfigurationKeyVaultOptions
110+
from azure.identity import DefaultAzureCredential
111+
94112
key_vault_options = AzureAppConfigurationKeyVaultOptions(
95113
client_configs={key_vault_uri: {'credential': credential}})
96114
config = load(endpoint=endpoint, credential=DefaultAzureCredential(), key_vault_options=key_vault_options)
@@ -101,6 +119,9 @@ config = load(endpoint=endpoint, credential=DefaultAzureCredential(), key_vault_
101119
If no Credentials or Clients are provided a secret resolver can be used. Secret resolver provides a way to return any value you want to a key vault reference.
102120

103121
```python
122+
from azure.appconfiguration.provider import load, AzureAppConfigurationKeyVaultOptions
123+
from azure.identity import DefaultAzureCredential
124+
104125
def secret_resolver(uri):
105126
return "From Secret Resolver"
106127

@@ -126,7 +147,7 @@ Check out our Django and Flask examples to see how to use the provider in a web
126147
This project welcomes contributions and suggestions. Most contributions require
127148
you to agree to a Contributor License Agreement (CLA) declaring that you have
128149
the right to, and actually do, grant us the rights to use your contribution.
129-
For details, visit https://cla.microsoft.com.
150+
For details, visit <https://cla.microsoft.com>.
130151

131152
When you submit a pull request, a CLA-bot will automatically determine whether
132153
you need to provide a CLA and decorate the PR appropriately (e.g., label,
@@ -135,11 +156,8 @@ need to do this once across all repos using our CLA.
135156

136157
This project has adopted the
137158
[Microsoft Open Source Code of Conduct][code_of_conduct]. For more information,
138-
see the Code of Conduct FAQ or contact [email protected] with any
159+
see the Code of Conduct FAQ or contact <[email protected]> with any
139160
additional questions or comments.
140161

141162
[azure_cli]: https://learn.microsoft.com/cli/azure/appconfig
142-
[cla]: https://cla.microsoft.com
143163
[code_of_conduct]: https://opensource.microsoft.com/codeofconduct/
144-
[coc_faq]: https://opensource.microsoft.com/codeofconduct/faq/
145-
[coc_contact]: mailto:[email protected]

sdk/appconfiguration/azure-appconfiguration-provider/azure/appconfiguration/provider/_azureappconfigurationprovider.py

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from ._constants import (
1717
FEATURE_MANAGEMENT_KEY,
1818
FEATURE_FLAG_PREFIX,
19+
REQUEST_TRACING_DISABLED_ENVIRONMENT_VARIABLE,
1920
ServiceFabricEnvironmentVariable,
2021
AzureFunctionEnvironmentVariable,
2122
AzureWebAppEnvironmentVariable,
@@ -117,7 +118,6 @@ def load(*args, **kwargs) -> "AzureAppConfigurationProvider":
117118
key_filter=select.key_filter, label_filter=select.label_filter
118119
)
119120
for config in configurations:
120-
121121
trimmed_key = config.key
122122
# Trim the key if it starts with one of the prefixes provided
123123
for trim in provider._trim_prefixes:
@@ -148,26 +148,29 @@ def load(*args, **kwargs) -> "AzureAppConfigurationProvider":
148148
return provider
149149

150150

151-
def _get_correlation_context(key_vault_options: Optional[AzureAppConfigurationKeyVaultOptions]) -> str:
152-
correlation_context = "RequestType=Startup"
153-
if key_vault_options and (
154-
key_vault_options.credential or key_vault_options.client_configs or key_vault_options.secret_resolver
155-
):
156-
correlation_context += ",UsesKeyVault"
157-
host_type = ""
158-
if os.environ.get(AzureFunctionEnvironmentVariable) is not None:
159-
host_type = "AzureFunctions"
160-
elif os.environ.get(AzureWebAppEnvironmentVariable) is not None:
161-
host_type = "AzureWebApps"
162-
elif os.environ.get(ContainerAppEnvironmentVariable) is not None:
163-
host_type = "ContainerApps"
164-
elif os.environ.get(KubernetesEnvironmentVariable) is not None:
165-
host_type = "Kubernetes"
166-
elif os.environ.get(ServiceFabricEnvironmentVariable) is not None:
167-
host_type = "ServiceFabric"
168-
if host_type:
169-
correlation_context += ",Host=" + host_type
170-
return correlation_context
151+
def _get_headers(key_vault_options: Optional[AzureAppConfigurationKeyVaultOptions], **kwargs) -> str:
152+
headers = kwargs.pop("headers", {})
153+
if os.environ.get(REQUEST_TRACING_DISABLED_ENVIRONMENT_VARIABLE, default="").lower() != "true":
154+
correlation_context = "RequestType=Startup"
155+
if key_vault_options and (
156+
key_vault_options.credential or key_vault_options.client_configs or key_vault_options.secret_resolver
157+
):
158+
correlation_context += ",UsesKeyVault"
159+
host_type = ""
160+
if AzureFunctionEnvironmentVariable in os.environ:
161+
host_type = "AzureFunction"
162+
elif AzureWebAppEnvironmentVariable in os.environ:
163+
host_type = "AzureWebApp"
164+
elif ContainerAppEnvironmentVariable in os.environ:
165+
host_type = "ContainerApp"
166+
elif KubernetesEnvironmentVariable in os.environ:
167+
host_type = "Kubernetes"
168+
elif ServiceFabricEnvironmentVariable in os.environ:
169+
host_type = "ServiceFabric"
170+
if host_type:
171+
correlation_context += ",Host=" + host_type
172+
headers["Correlation-Context"] = correlation_context
173+
return headers
171174

172175

173176
def _buildprovider(
@@ -179,17 +182,29 @@ def _buildprovider(
179182
) -> "AzureAppConfigurationProvider":
180183
# pylint:disable=protected-access
181184
provider = AzureAppConfigurationProvider()
182-
headers = kwargs.pop("headers", {})
183-
headers["Correlation-Context"] = _get_correlation_context(key_vault_options)
184-
useragent = USER_AGENT
185+
headers = _get_headers(key_vault_options, **kwargs)
186+
187+
retry_total = kwargs.pop("retry_total", 2)
188+
retry_backoff_max = kwargs.pop("retry_backoff_max", 60)
185189

186190
if connection_string:
187191
provider._client = AzureAppConfigurationClient.from_connection_string(
188-
connection_string, user_agent=useragent, headers=headers, **kwargs
192+
connection_string,
193+
user_agent=USER_AGENT,
194+
headers=headers,
195+
retry_total=retry_total,
196+
retry_backoff_max=retry_backoff_max,
197+
**kwargs
189198
)
190199
return provider
191200
provider._client = AzureAppConfigurationClient(
192-
endpoint, credential, user_agent=useragent, headers=headers, **kwargs
201+
endpoint,
202+
credential,
203+
user_agent=USER_AGENT,
204+
headers=headers,
205+
retry_total=retry_total,
206+
retry_backoff_max=retry_backoff_max,
207+
**kwargs
193208
)
194209
return provider
195210

sdk/appconfiguration/azure-appconfiguration-provider/azure/appconfiguration/provider/_constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
EMPTY_LABEL = "\0"
1111

12-
RequestTracingDisabledEnvironmentVariable = "AZURE_APP_CONFIGURATION_TRACING_DISABLED"
12+
REQUEST_TRACING_DISABLED_ENVIRONMENT_VARIABLE = "AZURE_APP_CONFIGURATION_TRACING_DISABLED"
1313
AzureFunctionEnvironmentVariable = "FUNCTIONS_EXTENSION_VERSION"
1414
AzureWebAppEnvironmentVariable = "WEBSITE_SITE_NAME"
1515
ContainerAppEnvironmentVariable = "CONTAINER_APP_NAME"

sdk/appconfiguration/azure-appconfiguration-provider/azure/appconfiguration/provider/aio/_azureappconfigurationproviderasync.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@
1212
from azure.keyvault.secrets import KeyVaultSecretIdentifier
1313

1414
from .._models import AzureAppConfigurationKeyVaultOptions, SettingSelector
15-
from .._constants import FEATURE_MANAGEMENT_KEY, FEATURE_FLAG_PREFIX, EMPTY_LABEL
16-
from .._azureappconfigurationprovider import _is_json_content_type, _get_correlation_context
15+
from .._constants import (
16+
FEATURE_MANAGEMENT_KEY,
17+
FEATURE_FLAG_PREFIX,
18+
EMPTY_LABEL,
19+
)
20+
from .._azureappconfigurationprovider import _is_json_content_type, _get_headers
1721
from .._user_agent import USER_AGENT
1822

1923
if TYPE_CHECKING:
@@ -108,7 +112,6 @@ async def load(*args, **kwargs) -> "AzureAppConfigurationProvider":
108112
key_filter=select.key_filter, label_filter=select.label_filter
109113
)
110114
async for config in configurations:
111-
112115
trimmed_key = config.key
113116
# Trim the key if it starts with one of the prefixes provided
114117
for trim in provider._trim_prefixes:
@@ -148,17 +151,29 @@ def _buildprovider(
148151
) -> "AzureAppConfigurationProvider":
149152
# pylint:disable=protected-access
150153
provider = AzureAppConfigurationProvider()
151-
headers = kwargs.pop("headers", {})
152-
headers["Correlation-Context"] = _get_correlation_context(key_vault_options)
153-
useragent = USER_AGENT
154+
headers = _get_headers(key_vault_options, **kwargs)
155+
156+
retry_total = kwargs.pop("retry_total", 2)
157+
retry_backoff_max = kwargs.pop("retry_backoff_max", 60)
154158

155159
if connection_string:
156160
provider._client = AzureAppConfigurationClient.from_connection_string(
157-
connection_string, user_agent=useragent, headers=headers, **kwargs
161+
connection_string,
162+
user_agent=USER_AGENT,
163+
headers=headers,
164+
retry_total=retry_total,
165+
retry_backoff_max=retry_backoff_max,
166+
**kwargs
158167
)
159168
return provider
160169
provider._client = AzureAppConfigurationClient(
161-
endpoint, credential, user_agent=useragent, headers=headers, **kwargs
170+
endpoint,
171+
credential,
172+
user_agent=USER_AGENT,
173+
headers=headers,
174+
retry_total=retry_total,
175+
retry_backoff_max=retry_backoff_max,
176+
**kwargs
162177
)
163178
return provider
164179

0 commit comments

Comments
 (0)