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(string.IsNullOrEmpty(_operationTypeName) ? string.Empty : $"{_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(string.IsNullOrEmpty(_operationTypeName) ? string.Empty : $"{_operationTypeName}.UpdateStatus");
try
{
var state = await _operation.UpdateStateAsync(async, cancellationToken).ConfigureAwait(false);
Expand Down
71 changes: 71 additions & 0 deletions sdk/core/Azure.Core/tests/OperationInternalOfTTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,19 @@ public async Task UpdateStatusCreatesDiagnosticScope([Values(true, false)] bool
testListener.AssertScope($"{expectedTypeName}.UpdateStatus", expectedAttributes);
}

[Test]
public async Task UpdateStatusNotCreateDiagnosticScope([Values(true, false)] bool async)
{
using ClientDiagnosticListener testListener = new(DiagnosticNamespace);

var operationInternal = OperationInternal<int>.Succeeded(InitialResponse, 1);
_ = async
? await operationInternal.UpdateStatusAsync(CancellationToken.None)
: operationInternal.UpdateStatus(CancellationToken.None);

CollectionAssert.IsEmpty(testListener.Scopes);
}

[Test]
public async Task UpdateStatusSetsFailedScopeWhenOperationFails([Values(true, false)] bool async)
{
Expand Down Expand Up @@ -205,6 +218,64 @@ public async Task UpdateStatusPassesTheCancellationTokenToUpdateState([Values(tr
Assert.AreEqual(originalToken, passedToken);
}

[Test]
public async Task WaitForCompletionResponseCreatesDiagnosticScope([Values(true, false)] bool async, [Values(null, "CustomTypeName")] string operationTypeName)
{
using ClientDiagnosticListener testListener = new(DiagnosticNamespace);

string expectedTypeName = operationTypeName ?? nameof(TestOperation);
KeyValuePair<string, string>[] expectedAttributes = { new("key1", "value1"), new("key2", "value2") };
var operationInternal = new OperationInternal<int>(ClientDiagnostics, TestOperation.Succeeded(1), InitialResponse, operationTypeName, expectedAttributes);

_ = async
? await operationInternal.WaitForCompletionResponseAsync(CancellationToken.None)
: operationInternal.WaitForCompletionResponse(CancellationToken.None);

testListener.AssertScope($"{expectedTypeName}.WaitForCompletionResponse", expectedAttributes);
}

[Test]
public async Task WaitForCompletionCreatesDiagnosticScope([Values(true, false)] bool async, [Values(null, "CustomTypeName")] string operationTypeName)
{
using ClientDiagnosticListener testListener = new(DiagnosticNamespace);

string expectedTypeName = operationTypeName ?? nameof(TestOperation);
KeyValuePair<string, string>[] expectedAttributes = { new("key1", "value1"), new("key2", "value2") };
var operationInternal = new OperationInternal<int>(ClientDiagnostics, TestOperation.Succeeded(1), InitialResponse, operationTypeName, expectedAttributes);

_ = async
? await operationInternal.WaitForCompletionAsync(CancellationToken.None)
: operationInternal.WaitForCompletion(CancellationToken.None);

testListener.AssertScope($"{expectedTypeName}.WaitForCompletionResponse", expectedAttributes);
}

[Test]
public async Task WaitForCompletionResponseNotCreateDiagnosticScope([Values(true, false)] bool async)
{
using ClientDiagnosticListener testListener = new(DiagnosticNamespace);

var operationInternal = OperationInternal<int>.Succeeded(InitialResponse, 1);
_ = async
? await operationInternal.WaitForCompletionResponseAsync(CancellationToken.None)
: operationInternal.WaitForCompletionResponse(CancellationToken.None);

CollectionAssert.IsEmpty(testListener.Scopes);
}

[Test]
public async Task WaitForCompletionNotCreateDiagnosticScope([Values(true, false)] bool async)
{
using ClientDiagnosticListener testListener = new(DiagnosticNamespace);

var operationInternal = OperationInternal<int>.Succeeded(InitialResponse, 1);
_ = async
? await operationInternal.WaitForCompletionAsync(CancellationToken.None)
: operationInternal.WaitForCompletion(CancellationToken.None);

CollectionAssert.IsEmpty(testListener.Scopes);
}

[Test]
public async Task WaitForCompletionCallsUntilOperationCompletes([Values(true, false)] bool useDefaultPollingInterval)
{
Expand Down
42 changes: 42 additions & 0 deletions sdk/core/Azure.Core/tests/OperationInternalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,19 @@ public async Task UpdateStatusCreatesDiagnosticScope([Values(true, false)] bool
testListener.AssertScope($"{expectedTypeName}.UpdateStatus", expectedAttributes);
}

[Test]
public async Task UpdateStatusNotCreateDiagnosticScope([Values(true, false)] bool async)
{
using ClientDiagnosticListener testListener = new(DiagnosticNamespace);

var operationInternal = OperationInternal.Succeeded(InitialResponse);
_ = async
? await operationInternal.UpdateStatusAsync(CancellationToken.None)
: operationInternal.UpdateStatus(CancellationToken.None);

CollectionAssert.IsEmpty(testListener.Scopes);
}

[Test]
public async Task UpdateStatusSetsFailedScopeWhenOperationFails([Values(true, false)] bool async)
{
Expand Down Expand Up @@ -187,6 +200,35 @@ public async Task UpdateStatusPassesTheCancellationTokenToUpdateState([Values(tr
Assert.AreEqual(originalToken, passedToken);
}

[Test]
public async Task WaitForCompletionResponseCreatesDiagnosticScope([Values(true, false)] bool async, [Values(null, "CustomTypeName")] string operationTypeName)
{
using ClientDiagnosticListener testListener = new(DiagnosticNamespace);

string expectedTypeName = operationTypeName ?? nameof(TestOperation);
KeyValuePair<string, string>[] expectedAttributes = { new("key1", "value1"), new("key2", "value2") };
var operationInternal = new OperationInternal(ClientDiagnostics, TestOperation.Succeeded(), InitialResponse, operationTypeName, expectedAttributes);

_ = async
? await operationInternal.WaitForCompletionResponseAsync(CancellationToken.None)
: operationInternal.WaitForCompletionResponse(CancellationToken.None);

testListener.AssertScope($"{expectedTypeName}.WaitForCompletionResponse", expectedAttributes);
}

[Test]
public async Task WaitForCompletionResponseNotCreateDiagnosticScope([Values(true, false)] bool async)
{
using ClientDiagnosticListener testListener = new(DiagnosticNamespace);

var operationInternal = OperationInternal.Succeeded(InitialResponse);
_ = async
? await operationInternal.WaitForCompletionResponseAsync(CancellationToken.None)
: operationInternal.WaitForCompletionResponse(CancellationToken.None);

CollectionAssert.IsEmpty(testListener.Scopes);
}

[Test]
public async Task WaitForCompletionCallsUntilOperationCompletes([Values(true, false)] bool useDefaultPollingInterval)
{
Expand Down