Skip to content

Commit f665705

Browse files
Set the linked activity before starting the scope. (Azure#20376)
* Set the linked activity before starting the scope. * Undo obsoletion
1 parent 8c0301c commit f665705

File tree

5 files changed

+115
-143
lines changed

5 files changed

+115
-143
lines changed

sdk/core/Azure.Core.TestFramework/src/ClientDiagnosticListener.cs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,23 @@ public class ClientDiagnosticListener : IObserver<KeyValuePair<string, object>>,
1616
private readonly AsyncLocal<bool> _collectThisStack;
1717

1818
private List<IDisposable> _subscriptions = new List<IDisposable>();
19+
private readonly Action<ProducedDiagnosticScope> _scopeStartCallback;
1920

2021
public List<ProducedDiagnosticScope> Scopes { get; } = new List<ProducedDiagnosticScope>();
2122

22-
public ClientDiagnosticListener(string name, bool asyncLocal = false): this(n => n == name, asyncLocal)
23+
public ClientDiagnosticListener(string name, bool asyncLocal = false, Action<ProducedDiagnosticScope> scopeStartCallback = default)
24+
: this(n => n == name, asyncLocal, scopeStartCallback)
2325
{
2426
}
2527

26-
public ClientDiagnosticListener(Func<string, bool> filter, bool asyncLocal = false)
28+
public ClientDiagnosticListener(Func<string, bool> filter, bool asyncLocal = false, Action<ProducedDiagnosticScope> scopeStartCallback = default)
2729
{
2830
if (asyncLocal)
2931
{
3032
_collectThisStack = new AsyncLocal<bool> { Value = true };
3133
}
3234
_sourceNameFilter = filter;
35+
_scopeStartCallback = scopeStartCallback;
3336
DiagnosticListener.AllListeners.Subscribe(this);
3437
}
3538

@@ -69,6 +72,7 @@ public void OnNext(KeyValuePair<string, object> value)
6972
};
7073

7174
Scopes.Add(scope);
75+
_scopeStartCallback?.Invoke(scope);
7276
}
7377
else if (value.Key.EndsWith(stopSuffix))
7478
{
@@ -144,7 +148,10 @@ public void Dispose()
144148
}
145149
}
146150

147-
public ProducedDiagnosticScope AssertScopeStarted(string name, params KeyValuePair<string, string>[] expectedAttributes)
151+
public ProducedDiagnosticScope AssertScopeStarted(string name, params KeyValuePair<string, string>[] expectedAttributes) =>
152+
AssertScopeStartedInternal(name, false, expectedAttributes);
153+
154+
private ProducedDiagnosticScope AssertScopeStartedInternal(string name, bool remove, params KeyValuePair<string, string>[] expectedAttributes)
148155
{
149156
lock (Scopes)
150157
{
@@ -160,16 +167,28 @@ public ProducedDiagnosticScope AssertScopeStarted(string name, params KeyValuePa
160167
}
161168
}
162169

170+
if (remove)
171+
{
172+
Scopes.Remove(producedDiagnosticScope);
173+
}
174+
163175
return producedDiagnosticScope;
164176
}
165177
}
166178
throw new InvalidOperationException($"Event '{name}' was not started");
167179
}
168180
}
169181

170-
public ProducedDiagnosticScope AssertScope(string name, params KeyValuePair<string, string>[] expectedAttributes)
182+
public ProducedDiagnosticScope AssertScope(string name, params KeyValuePair<string, string>[] expectedAttributes) =>
183+
AssertScopeInternal(name, false, expectedAttributes);
184+
185+
public ProducedDiagnosticScope AssertAndRemoveScope(string name, params KeyValuePair<string, string>[] expectedAttributes) =>
186+
AssertScopeInternal(name, true, expectedAttributes);
187+
188+
private ProducedDiagnosticScope AssertScopeInternal(string name, bool remove,
189+
params KeyValuePair<string, string>[] expectedAttributes)
171190
{
172-
ProducedDiagnosticScope scope = AssertScopeStarted(name, expectedAttributes);
191+
ProducedDiagnosticScope scope = AssertScopeStartedInternal(name, remove, expectedAttributes);
173192
if (!scope.IsCompleted)
174193
{
175194
throw new InvalidOperationException($"'{name}' is not completed");

sdk/core/Azure.Core.TestFramework/src/TestDiagnosticListener.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Azure.Core.Tests
99
{
10+
// DO NOT USE - use ClientDiagnosticListener instead
1011
public class TestDiagnosticListener : IObserver<DiagnosticListener>, IDisposable
1112
{
1213
private readonly Func<DiagnosticListener, bool> _selector;

sdk/servicebus/Azure.Messaging.ServiceBus/src/Processor/ReceiverManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ await RaiseExceptionReceived(
116116
protected async Task ProcessOneMessageWithinScopeAsync(ServiceBusReceivedMessage message, string activityName, CancellationToken cancellationToken)
117117
{
118118
using DiagnosticScope scope = _scopeFactory.CreateScope(activityName, DiagnosticProperty.ConsumerKind);
119-
scope.Start();
120119
scope.SetMessageData(new ServiceBusReceivedMessage[] { message });
120+
scope.Start();
121121
try
122122
{
123123
await ProcessOneMessage(

0 commit comments

Comments
 (0)