-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
[Draft] Minimize noisy Storage exceptions via DPG methods #27645
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
159 changes: 159 additions & 0 deletions
159
sdk/core/Azure.Core/src/Shared/RequestContextExtensions.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,159 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Threading; | ||
|
||
namespace Azure.Core | ||
{ | ||
/// <summary> | ||
/// Shared source helpers for <see cref="RequestContext"/>. | ||
/// </summary> | ||
internal static class RequestContextExtensions | ||
{ | ||
/// <summary> | ||
/// Singleton <see cref="RequestContext"/> to use when we don't have an | ||
/// instance. | ||
/// </summary> | ||
public static RequestContext Empty { get; } = new RequestContext(); | ||
|
||
/// <summary> | ||
/// Create a RequestContext instance from a CancellationToken. | ||
/// </summary> | ||
/// <param name="cancellationToken">The cancellation token.</param> | ||
/// <returns> | ||
/// A <see cref="RequestContext"/> wrapping the cancellation token. | ||
/// </returns> | ||
public static RequestContext ToRequestContext(this CancellationToken cancellationToken) => | ||
cancellationToken == CancellationToken.None ? | ||
Empty : | ||
new() { CancellationToken = cancellationToken }; | ||
|
||
/// <summary> | ||
/// Link a CancellationToken with an existing RequestContext. | ||
/// </summary> | ||
/// <param name="context"> | ||
/// The <see cref="RequestContext"/> for the current operation. | ||
/// </param> | ||
/// <param name="cancellationToken">A cancellation token.</param> | ||
/// <returns> | ||
/// The <see cref="RequestContext"/> for the current operation. | ||
/// </returns> | ||
public static RequestContext Link(this RequestContext context, CancellationToken cancellationToken) | ||
{ | ||
if (cancellationToken == CancellationToken.None) | ||
{ | ||
// Don't link if there's nothing there | ||
} | ||
else if (context.CancellationToken == CancellationToken.None) | ||
{ | ||
// Don't link if we can just replace | ||
context.CancellationToken = cancellationToken; | ||
} | ||
else | ||
{ | ||
// Otherwise link them together | ||
CancellationTokenSource source = CancellationTokenSource.CreateLinkedTokenSource( | ||
context.CancellationToken, | ||
cancellationToken); | ||
context.CancellationToken = source.Token; | ||
} | ||
return context; | ||
} | ||
|
||
/// <summary> | ||
/// Customizes the <see cref="ResponseClassifier"/> for this operation | ||
/// to change the default <see cref="Response"/> classification behavior | ||
/// so that it evalutes the anonymous classifier to determine whether | ||
/// an error occured or not. | ||
/// | ||
/// Custom classifiers are applied after all | ||
/// <see cref="ResponseClassificationHandler"/> classifiers. This is | ||
/// useful for cases where you'd like to prevent specific situations | ||
/// from being treated as errors by logging and distributed tracing | ||
/// policies -- that is, if a response is not classified as an error, | ||
/// it will not appear as an error in logs or distributed traces. | ||
/// </summary> | ||
/// <param name="context"> | ||
/// The <see cref="RequestContext"/> for the current operation. | ||
/// </param> | ||
/// <param name="classifier"> | ||
/// A function to classify the response. It should return true if the | ||
/// message contains an error, false if not an error, and null if it | ||
/// was unable to classify. | ||
/// </param> | ||
/// <returns> | ||
/// The <see cref="RequestContext"/> for the current operation. | ||
/// </returns> | ||
public static RequestContext AddClassifier(this RequestContext context, Func<HttpMessage, bool?> classifier) | ||
{ | ||
Argument.AssertNotNull(context, nameof(context)); | ||
Argument.AssertNotNull(classifier, nameof(classifier)); | ||
context.AddClassifier(new LambdaClassifier(classifier)); | ||
return context; | ||
} | ||
|
||
// Anonymous reponse classifier | ||
private class LambdaClassifier : ResponseClassificationHandler | ||
{ | ||
private Func<HttpMessage, bool?> _classifier; | ||
|
||
public LambdaClassifier(Func<HttpMessage, bool?> classifier) => | ||
_classifier = classifier; | ||
|
||
public override bool TryClassify(HttpMessage message, out bool isError) | ||
{ | ||
if (message.HasResponse) | ||
{ | ||
bool? result = _classifier(message); | ||
if (result is not null) | ||
{ | ||
isError = result.Value; | ||
return true; | ||
} | ||
} | ||
|
||
isError = false; | ||
return false; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Customizes the <see cref="ResponseClassifier"/> for this operation | ||
/// to change the default <see cref="Response"/> classification behavior | ||
/// so that it considers the passed-in status code and error code to be | ||
/// an error or not, as specified. | ||
/// | ||
/// Custom classifiers are applied after all | ||
/// <see cref="ResponseClassificationHandler"/> classifiers. This is | ||
/// useful for cases where you'd like to prevent specific response | ||
/// status and error codes from being treated as errors by logging and | ||
/// distributed tracing policies -- that is, if a response is not | ||
/// classified as an error, it will not appear as an error in logs or | ||
/// distributed traces. | ||
/// </summary> | ||
/// <param name="context"> | ||
/// The <see cref="RequestContext"/> for the current operation. | ||
/// </param> | ||
/// <param name="statusCode"> | ||
/// The status code to customize classification for. | ||
/// </param> | ||
/// <param name="errorCode"> | ||
/// The error code to customize classification for. | ||
/// </param> | ||
/// <param name="isError"> | ||
/// Whether the passed-in status code should be classified as an error. | ||
/// </param> | ||
/// <returns> | ||
/// The <see cref="RequestContext"/> for the current operation. | ||
/// </returns> | ||
public static RequestContext AddClassifier(this RequestContext context, int statusCode, string errorCode, bool isError) => | ||
context.AddClassifier( | ||
message => | ||
message.Response.Status == statusCode && | ||
message.Response.Headers.TryGetValue("x-ms-error-code", out string code) && | ||
code == errorCode ? | ||
isError : | ||
null); | ||
} | ||
} |
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
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.
How do we determine that we want to add
RequestContext
to HLC methods vs using it internally.I.e. why this instead of
AcquireIfNotYetAcuired
API that usesRequestContext
internally?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.
Because it's not really doing
AcquireIfNotYetAcquired
. It's doingAcquireIfBlobNotFoundOrContainerNotFoundOrLeaseAlreadyAcquired
and we don't want to add an overload for that specific, non-general scenario.