-
Notifications
You must be signed in to change notification settings - Fork 294
remove httpcontext from rolename initializer #1340
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
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1f3f36b
remove httpcontext from rolename initializer
af459d6
cleanup
85e6d12
cleanup
93e5227
add bunch of tests
ca6f93d
Merge branch 'develop' into tilee/httpcontext_rolename
23bdb30
changelog
6ff03aa
change RoleNameContainer to streamline use
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
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
92 changes: 92 additions & 0 deletions
92
NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/Implementation/RoleNameContainer.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,92 @@ | ||
namespace Microsoft.ApplicationInsights.AspNetCore.Implementation | ||
{ | ||
using System; | ||
using System.Threading; | ||
|
||
using Microsoft.AspNetCore.Http; | ||
|
||
/// <summary> | ||
/// Static container that holds the RoleName. | ||
/// </summary> | ||
internal class RoleNameContainer | ||
{ | ||
private const string WebAppHostNameHeaderName = "WAS-DEFAULT-HOSTNAME"; | ||
private const string WebAppHostNameEnvironmentVariable = "WEBSITE_HOSTNAME"; | ||
|
||
private static string roleName = string.Empty; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="RoleNameContainer"/> class. | ||
/// Will set the RoleName based on an environment variable. | ||
/// </summary> | ||
/// <param name="hostNameSuffix">Host name suffix will be used to parse the prefix from the host name. The value of the prefix is the RoleName.</param> | ||
public RoleNameContainer(string hostNameSuffix = ".azurewebsites.net") | ||
{ | ||
this.HostNameSuffix = hostNameSuffix; | ||
var enVarValue = Environment.GetEnvironmentVariable(WebAppHostNameEnvironmentVariable); | ||
this.ParseAndSetRoleName(enVarValue); | ||
|
||
this.IsAzureWebApp = !string.IsNullOrEmpty(enVarValue); | ||
} | ||
|
||
/// <summary> | ||
/// Gets or sets static instance for Initializer and DiagnosticListener to share access to RoleName variable. | ||
/// </summary> | ||
public static RoleNameContainer Instance { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets role name of the current application. | ||
/// </summary> | ||
public string RoleName | ||
{ | ||
get => roleName; | ||
|
||
set | ||
{ | ||
if (value != roleName) | ||
{ | ||
Interlocked.Exchange(ref roleName, value); | ||
} | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Gets a value indicating whether indicates if the current app is an Azure Web App based on the presence of a specific environment variable. Set in constructor. | ||
/// </summary> | ||
public bool IsAzureWebApp { get; private set; } | ||
|
||
/// <summary> | ||
/// Gets suffix of website name. This must be changed when running in non public Azure region. | ||
/// Default value (Public Cloud): ".azurewebsites.net" | ||
/// For US Gov Cloud: ".azurewebsites.us" | ||
/// For Azure Germany: ".azurewebsites.de". | ||
/// </summary> | ||
public string HostNameSuffix { get; private set; } | ||
|
||
/// <summary> | ||
/// Attempt to set the role name from a given collection of request headers. | ||
/// </summary> | ||
/// <param name="requestHeaders">Request headers to check for role name.</param> | ||
public void Set(IHeaderDictionary requestHeaders) | ||
{ | ||
string headerValue = requestHeaders[WebAppHostNameHeaderName]; | ||
this.ParseAndSetRoleName(headerValue); | ||
} | ||
|
||
private void ParseAndSetRoleName(string input) | ||
{ | ||
if (string.IsNullOrEmpty(input)) | ||
{ | ||
// do nothing | ||
} | ||
else if (input.EndsWith(this.HostNameSuffix, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
this.RoleName = input.Substring(0, input.Length - this.HostNameSuffix.Length); | ||
} | ||
else | ||
{ | ||
this.RoleName = input; | ||
} | ||
} | ||
} | ||
} |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not using Interlocked.CompareExchange for comparison and exchange?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Simple answer is, i didn't know the correct way to use CompareExchange.
Now that I have better test coverage, i'll take another look at this. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you know something I don't, please share. :)
From what I'm reading, CompareExchange will always replace the
ref variable
with an incoming value.In my use case, I only need to replace the
ref variable
when the incoming value does not match.Specifically, when a VIP SWAP occurs, we need to update the role name.
My understanding is, replacing this with a CompareExchange will always replace the
ref variable
with the incoming value regardless of if it's new.I believe CompareExchange doesn't help me in this use case.
But please correct me if i'm wrong.