Skip to content
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

Add scope of waitForCompletion in LRO #29033

Merged
merged 15 commits into from
Jun 22, 2022
32 changes: 21 additions & 11 deletions sdk/core/Azure.Core/src/Shared/OperationInternalBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ namespace Azure.Core
internal abstract class OperationInternalBase
{
private readonly ClientDiagnostics _diagnostics;
private readonly string _updateStatusScopeName;
private readonly IReadOnlyDictionary<string, string>? _scopeAttributes;
private readonly DelayStrategy? _fallbackStrategy;
private readonly AsyncLockWithValue<Response> _responseLock;

protected readonly string _operationTypeName;

protected OperationInternalBase(Response rawResponse)
{
_diagnostics = new ClientDiagnostics(ClientOptions.Default);
_updateStatusScopeName = string.Empty;
_operationTypeName = string.Empty;
_scopeAttributes = default;
_fallbackStrategy = default;
_responseLock = new AsyncLockWithValue<Response>(rawResponse);
Expand All @@ -32,7 +33,7 @@ protected OperationInternalBase(Response rawResponse)
protected OperationInternalBase(ClientDiagnostics clientDiagnostics, string operationTypeName, IEnumerable<KeyValuePair<string, string>>? scopeAttributes = null, DelayStrategy? fallbackStrategy = null)
{
_diagnostics = clientDiagnostics;
_updateStatusScopeName = $"{operationTypeName}.UpdateStatus";
_operationTypeName = operationTypeName;
_scopeAttributes = scopeAttributes?.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
_fallbackStrategy = fallbackStrategy;
_responseLock = new AsyncLockWithValue<Response>();
Expand Down Expand Up @@ -188,20 +189,29 @@ private async ValueTask<Response> WaitForCompletionResponseAsync(bool async, Tim
return lockOrValue.Value;
}

var poller = new OperationPoller(_fallbackStrategy);
var response = async
? await poller.WaitForCompletionResponseAsync(this, pollingInterval, cancellationToken).ConfigureAwait(false)
: poller.WaitForCompletionResponse(this, pollingInterval, cancellationToken);
using var scope = CreateScope($"{_operationTypeName}.WaitForCompletionResponse");
try
{
var poller = new OperationPoller(_fallbackStrategy);
var response = async
? await poller.WaitForCompletionResponseAsync(this, pollingInterval, cancellationToken).ConfigureAwait(false)
: poller.WaitForCompletionResponse(this, pollingInterval, cancellationToken);

lockOrValue.SetValue(response);
return response;
lockOrValue.SetValue(response);
return response;
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}

protected abstract ValueTask<Response> UpdateStatusAsync(bool async, CancellationToken cancellationToken);

protected DiagnosticScope CreateScope()
protected DiagnosticScope CreateScope(string scopeName)
{
DiagnosticScope scope = _diagnostics.CreateScope(_updateStatusScopeName);
DiagnosticScope scope = _diagnostics.CreateScope(scopeName);

if (_scopeAttributes != null)
{
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/Azure.Core/src/Shared/OperationInternalOfT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ protected override async ValueTask<Response> UpdateStatusAsync(bool async, Cance
return GetResponseFromState(asyncLock.Value);
}

using var scope = CreateScope();
using var scope = CreateScope($"{_operationTypeName}.UpdateStatus");
try
{
var state = await _operation.UpdateStateAsync(async, cancellationToken).ConfigureAwait(false);
Expand Down