-
Notifications
You must be signed in to change notification settings - Fork 204
[ApplicationInsights] removing env var reads from hot path #1996
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/DotNetWorker.ApplicationInsights/Initializers/AppServiceEnvironmentVariableMonitor.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Extensions.Hosting; | ||
| using Microsoft.Extensions.Options; | ||
| using Microsoft.Extensions.Primitives; | ||
|
|
||
| namespace Microsoft.Azure.Functions.Worker.ApplicationInsights.Initializers; | ||
|
|
||
| internal class AppServiceEnvironmentVariableMonitor : BackgroundService, IOptionsChangeTokenSource<AppServiceOptions> | ||
| { | ||
| private readonly TimeSpan _refreshInterval; | ||
|
|
||
| private IChangeToken _changeToken; | ||
| private CancellationTokenSource _cancellationTokenSource = new(); | ||
|
|
||
| private readonly Dictionary<string, string?> _monitoredVariableCache = new(StringComparer.OrdinalIgnoreCase); | ||
|
|
||
| public AppServiceEnvironmentVariableMonitor() : this(TimeSpan.FromSeconds(5)) | ||
| { | ||
| } | ||
|
|
||
| public AppServiceEnvironmentVariableMonitor(TimeSpan refreshInterval) | ||
| { | ||
| _refreshInterval = refreshInterval; | ||
| _changeToken = new CancellationChangeToken(_cancellationTokenSource.Token); | ||
| } | ||
|
|
||
| public string Name => string.Empty; | ||
|
|
||
| public IChangeToken GetChangeToken() => _changeToken; | ||
|
|
||
| protected override async Task ExecuteAsync(CancellationToken stoppingToken) | ||
| { | ||
| while (!stoppingToken.IsCancellationRequested) | ||
| { | ||
| bool changeDetected = false; | ||
|
|
||
| foreach (string envVar in AppServiceOptionsInitializer.EnvironmentVariablesToMonitor) | ||
| { | ||
| string? currentVal = Environment.GetEnvironmentVariable(envVar); | ||
| _monitoredVariableCache.TryGetValue(envVar, out string? cachedVal); | ||
|
|
||
| if (!string.Equals(currentVal, cachedVal, StringComparison.Ordinal)) | ||
| { | ||
| changeDetected = true; | ||
| _monitoredVariableCache[envVar] = currentVal; | ||
| } | ||
| } | ||
|
|
||
| if (changeDetected) | ||
| { | ||
| var oldTokenSource = Interlocked.Exchange(ref _cancellationTokenSource, new CancellationTokenSource()); | ||
| Interlocked.Exchange(ref _changeToken, new CancellationChangeToken(_cancellationTokenSource.Token)); | ||
|
|
||
| if (!oldTokenSource.IsCancellationRequested) | ||
| { | ||
| oldTokenSource.Cancel(); | ||
| oldTokenSource.Dispose(); | ||
| } | ||
| } | ||
|
|
||
| try | ||
| { | ||
| await Task.Delay(_refreshInterval, stoppingToken); | ||
| } | ||
| catch (OperationCanceledException) | ||
| { | ||
| // happens during normal shutdown | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
11 changes: 11 additions & 0 deletions
11
src/DotNetWorker.ApplicationInsights/Initializers/AppServiceOptions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| namespace Microsoft.Azure.Functions.Worker.ApplicationInsights.Initializers; | ||
|
|
||
| internal class AppServiceOptions | ||
| { | ||
| public string? AzureWebsiteName { get; set; } | ||
|
|
||
| public string? AzureWebsiteSlotName { get; set; } | ||
|
|
||
| public string? AzureWebsiteCloudRoleName { get; set; } | ||
| } | ||
|
|
37 changes: 37 additions & 0 deletions
37
src/DotNetWorker.ApplicationInsights/Initializers/AppServiceOptionsInitializer.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| using System; | ||
| using Microsoft.Azure.Functions.Worker.ApplicationInsights.Initializers; | ||
| using Microsoft.Extensions.Options; | ||
|
|
||
| internal class AppServiceOptionsInitializer : IConfigureOptions<AppServiceOptions> | ||
| { | ||
| internal const string AzureWebsiteName = "WEBSITE_SITE_NAME"; | ||
| internal const string AzureWebsiteSlotName = "WEBSITE_SLOT_NAME"; | ||
| internal const string AzureWebsiteCloudRoleName = "WEBSITE_CLOUD_ROLENAME"; | ||
| internal const string DefaultProductionSlotName = "production"; | ||
|
|
||
| internal static string[] EnvironmentVariablesToMonitor = new[] { AzureWebsiteName, AzureWebsiteSlotName, AzureWebsiteCloudRoleName }; | ||
|
|
||
| public void Configure(AppServiceOptions options) | ||
| { | ||
| options.AzureWebsiteName = Environment.GetEnvironmentVariable(AzureWebsiteName); | ||
|
brettsam marked this conversation as resolved.
|
||
| options.AzureWebsiteCloudRoleName = Environment.GetEnvironmentVariable(AzureWebsiteCloudRoleName); | ||
|
|
||
| // Compute the slot name by appending non-production slot to the site name (i.e. mysite-staging) | ||
| string slotName = Environment.GetEnvironmentVariable(AzureWebsiteSlotName); | ||
| options.AzureWebsiteSlotName = GetAzureWebsiteUniqueSlotName(options.AzureWebsiteName, slotName); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets a value that uniquely identifies the site and slot. | ||
| /// </summary> | ||
| private static string? GetAzureWebsiteUniqueSlotName(string? websiteName, string? slotName) | ||
| { | ||
| if (!string.IsNullOrEmpty(slotName) && | ||
| !string.Equals(slotName, DefaultProductionSlotName, StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| websiteName += $"-{slotName}"; | ||
| } | ||
|
|
||
| return websiteName?.ToLowerInvariant(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| ## What's Changed | ||
|
|
||
| - GA release (no functional changes) | ||
| - Moving hot-path environment variable checks to a background task (#1996) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.