-
Notifications
You must be signed in to change notification settings - Fork 287
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
remove httpcontext from rolename initializer #1340
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1f3f36b
remove httpcontext from rolename initializer
TimothyMothra af459d6
cleanup
TimothyMothra 85e6d12
cleanup
TimothyMothra 93e5227
add bunch of tests
TimothyMothra ca6f93d
Merge branch 'develop' into tilee/httpcontext_rolename
TimothyMothra 23bdb30
changelog
TimothyMothra 6ff03aa
change RoleNameContainer to streamline use
TimothyMothra 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 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 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
66 changes: 66 additions & 0 deletions
66
NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/Implementation/RoleNameContainer.cs
This file contains 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,66 @@ | ||
namespace Microsoft.ApplicationInsights.AspNetCore.Implementation | ||
{ | ||
using System; | ||
using System.Threading; | ||
|
||
using Microsoft.AspNetCore.Http; | ||
|
||
internal static class RoleNameContainer | ||
{ | ||
private const string WebAppHostNameHeaderName = "WAS-DEFAULT-HOSTNAME"; | ||
private const string WebAppHostNameEnvironmentVariable = "WEBSITE_HOSTNAME"; | ||
|
||
private static string roleName = string.Empty; | ||
|
||
public static string RoleName | ||
{ | ||
get => roleName; | ||
|
||
set | ||
{ | ||
if (value != roleName) | ||
{ | ||
Interlocked.Exchange(ref roleName, value); | ||
} | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Gets or sets 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 static string HostNameSuffix { get; set; } = ".azurewebsites.net"; | ||
|
||
public static void SetFromEnvironmentVariable(out bool isAzureWebApp) | ||
{ | ||
var value = Environment.GetEnvironmentVariable(WebAppHostNameEnvironmentVariable); | ||
ParseAndSetRoleName(value); | ||
|
||
isAzureWebApp = !string.IsNullOrEmpty(value); | ||
} | ||
|
||
public static void Set(IHeaderDictionary requestHeaders) | ||
{ | ||
string headerValue = requestHeaders[WebAppHostNameHeaderName]; | ||
ParseAndSetRoleName(headerValue); | ||
} | ||
|
||
private static void ParseAndSetRoleName(string input) | ||
{ | ||
if (string.IsNullOrEmpty(input)) | ||
{ | ||
// do nothing | ||
} | ||
else if (input.EndsWith(HostNameSuffix, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
RoleName = input.Substring(0, input.Length - HostNameSuffix.Length); | ||
} | ||
else | ||
{ | ||
RoleName = input; | ||
} | ||
} | ||
} | ||
} |
This file contains 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 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.