-
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System.Threading; | ||
|
||
namespace Azure.Core | ||
{ | ||
/// <summary> | ||
/// Shared source helpers for <see cref="RequestContext"/>. | ||
/// </summary> | ||
internal static class RequestContextExtensions | ||
{ | ||
/// <summary> | ||
/// Singleton 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 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 RequestContext.</param> | ||
/// <param name="cancellationToken">A cancellation token.</param> | ||
public static void 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; | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4442,39 +4442,63 @@ private async Task<Response<bool>> ExistsInternal( | |
nameof(BlobBaseClient), | ||
message: | ||
$"{nameof(Uri)}: {Uri}"); | ||
|
||
string operationName = $"{nameof(BlobBaseClient)}.{nameof(Exists)}"; | ||
|
||
DiagnosticScope scope = ClientConfiguration.ClientDiagnostics.CreateScope($"{nameof(BlobBaseClient)}.{nameof(Exists)}"); | ||
annelo-msft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
try | ||
{ | ||
Response<BlobProperties> response = await GetPropertiesInternal( | ||
conditions: default, | ||
async: async, | ||
cancellationToken: cancellationToken, | ||
operationName) | ||
.ConfigureAwait(false); | ||
scope.Start(); | ||
|
||
return Response.FromValue(true, response.GetRawResponse()); | ||
} | ||
catch (RequestFailedException storageRequestFailedException) | ||
when (storageRequestFailedException.ErrorCode == BlobErrorCode.BlobNotFound | ||
|| storageRequestFailedException.ErrorCode == BlobErrorCode.ContainerNotFound) | ||
{ | ||
return Response.FromValue(false, default); | ||
} | ||
catch (RequestFailedException storageRequestFailedException) | ||
when (storageRequestFailedException.ErrorCode == BlobErrorCode.BlobUsesCustomerSpecifiedEncryption) | ||
{ | ||
return Response.FromValue(true, default); | ||
// Get the blob properties, but don't throw any exceptions | ||
RequestContext context = new() | ||
{ | ||
ErrorOptions = ErrorOptions.NoThrow, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will suppress an exception from being thrown from within GetProperties, but error logging and tracing will still occur in the pipeline. In order to suppress those as well, you'll need to add classifiers using context.AddClassifier(...) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sigh... That's what I get for rushing and only checking the Storage tests. I updated and verified that distributed tracing is clean too. #define Fixed
using Azure;
using Azure.Core;
using Azure.Monitor.OpenTelemetry.Exporter;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using Azure.Storage.Blobs.Specialized;
using OpenTelemetry.Trace;
using System.Diagnostics;
// Settings
const string StorageConnStr = "...";
const string AppInsightsConnStr = "...";
const string Name =
#if !Fixed
"Current";
#else
"Fixed";
#endif
// Collect telemetry in Azure Monitor
AppContext.SetSwitch("Azure.Experimental.EnableActivitySource", true);
using TracerProvider telemetry =
OpenTelemetry.Sdk
.CreateTracerProviderBuilder()
.AddSource(Name, "Azure.*")
.AddAzureMonitorTraceExporter(options => options.ConnectionString = AppInsightsConnStr)
.Build();
using ActivitySource source = new(Name);
using Activity? group = source.StartActivity(Name)!;
// Check if a fake blob exists
BlobClient fake = new BlobClient(StorageConnStr, "fakecontainer", "fakeblob");
Console.WriteLine($"Exists: {fake.Exists().Value}");
// Try acquiring a lease from a fake blob
#if !Fixed
try
{
BlobLease lease = fake.GetBlobLeaseClient().Acquire(TimeSpan.FromSeconds(15));
Console.WriteLine($"Lease: {lease.LeaseId}");
}
catch (RequestFailedException ex)
{
Console.WriteLine($"Failed to obtain lease!\n{ex}");
}
#else
RequestContext context =
new RequestContext() { ErrorOptions = ErrorOptions.NoThrow }
.AddClassifier(404, BlobErrorCode.BlobNotFound.ToString(), false)
.AddClassifier(404, BlobErrorCode.ContainerNotFound.ToString(), false)
.AddClassifier(409, BlobErrorCode.LeaseAlreadyPresent.ToString(), false);
Response<BlobLease> response =
fake.GetBlobLeaseClient().Acquire(
TimeSpan.FromSeconds(15),
conditions: null,
context);
Response raw = response.GetRawResponse();
if (raw.Status == 201)
{
Console.WriteLine($"Lease: {response.Value.LeaseId}");
}
else
{
Console.WriteLine($"Failed to obtain lease!\n{raw.Content.ToString()}");
}
#endif |
||
CancellationToken = cancellationToken | ||
}; | ||
Response response = async ? | ||
await BlobRestClient.GetPropertiesAsync(context: context).ConfigureAwait(false) : | ||
BlobRestClient.GetProperties(context: context); | ||
|
||
// If the response wasn't an error, then the blob exists | ||
if (!response.IsError) | ||
{ | ||
return Response.FromValue(true, response); | ||
} | ||
|
||
// Otherwise it depends on the error | ||
string code = response.GetErrorCode(); | ||
if (code == BlobErrorCode.BlobNotFound || | ||
code == BlobErrorCode.ContainerNotFound) | ||
{ | ||
// If the failure was just "we can't find the blob" then | ||
// we'll say it doesn't exist instead of throwing an exception | ||
return Response.FromValue(false, response); | ||
} | ||
else if (code == BlobErrorCode.BlobUsesCustomerSpecifiedEncryption) | ||
{ | ||
return Response.FromValue(true, response); | ||
} | ||
else | ||
{ | ||
// If we get any other failure (auth, throttling, etc.) then | ||
// we'll still throw an exception | ||
RequestFailedException ex = async ? | ||
await ClientConfiguration.ClientDiagnostics.CreateRequestFailedExceptionAsync(response).ConfigureAwait(false) : | ||
ClientConfiguration.ClientDiagnostics.CreateRequestFailedException(response); | ||
// Can't simply do the following because of https://github.com/Azure/azure-sdk-for-net/issues/27614 | ||
// ex = new RequestFailedException(response); | ||
|
||
throw ex; | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
ClientConfiguration.Pipeline.LogException(ex); | ||
scope.Failed(ex); | ||
throw; | ||
} | ||
finally | ||
{ | ||
ClientConfiguration.Pipeline.LogMethodExit(nameof(BlobBaseClient)); | ||
scope.Dispose(); | ||
} | ||
} | ||
} | ||
|
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.