Skip to content

Commit b2a2f3e

Browse files
Retain instrumentation after adding event to batch (Azure#35124)
* Retain instrumentation after adding event to batch * Update sdk/eventhub/Azure.Messaging.EventHubs/CHANGELOG.md Co-authored-by: Jesse Squire <jesse.squire@gmail.com> * update tests --------- Co-authored-by: Jesse Squire <jesse.squire@gmail.com>
1 parent 703474b commit b2a2f3e

File tree

8 files changed

+16
-60
lines changed

8 files changed

+16
-60
lines changed

sdk/core/Azure.Core/src/Shared/MessagingClientDiagnostics.cs

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,10 @@ public static bool TryExtractTraceContext(IDictionary<string, object> properties
156156
/// </summary>
157157
/// <param name="properties">The dictionary of application message properties.</param>
158158
/// <param name="activityName">The activity name to use for the diagnostic scope.</param>
159-
/// <param name="scopeCreated">Whether or not a new scope was created while instrumenting the message.</param>
160159
/// <param name="traceparent">The traceparent that was either added, or that already existed in the message properties.</param>
161160
/// <param name="tracestate">The tracestate that was either added, or that already existed in the message properties.</param>
162-
public void InstrumentMessage(IDictionary<string, object> properties, string activityName, out bool scopeCreated, out string? traceparent, out string? tracestate)
161+
public void InstrumentMessage(IDictionary<string, object> properties, string activityName, out string? traceparent, out string? tracestate)
163162
{
164-
scopeCreated = false;
165163
traceparent = null;
166164
tracestate = null;
167165

@@ -171,7 +169,6 @@ public void InstrumentMessage(IDictionary<string, object> properties, string act
171169
activityName,
172170
DiagnosticScope.ActivityKind.Producer);
173171
messageScope.Start();
174-
scopeCreated = true;
175172

176173
Activity activity = Activity.Current;
177174
if (activity != null)
@@ -194,20 +191,5 @@ public void InstrumentMessage(IDictionary<string, object> properties, string act
194191
TryExtractTraceContext(properties, out traceparent, out tracestate);
195192
}
196193
}
197-
198-
/// <summary>
199-
/// Resets the instrumentation associated with a properties bag.
200-
/// </summary>
201-
///
202-
/// <param name="properties">The properties to reset.</param>
203-
public static void ResetEvent(IDictionary<string, object> properties)
204-
{
205-
properties.Remove(DiagnosticIdAttribute);
206-
if (ActivityExtensions.SupportsActivitySource())
207-
{
208-
properties.Remove(TraceParent);
209-
properties.Remove(TraceState);
210-
}
211-
}
212194
}
213195
}

sdk/eventhub/Azure.Messaging.EventHubs/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
### Breaking Changes
88

9+
- If diagnostic tracing is enabled, diagnostic tracing information is retained on `EventData` instances when they are added to an `EventDataBatch`. This matches the existing behavior when sending events using the `SendEventsAsync` method that takes an `IEnumerable<EventData>`.
10+
911
### Bugs Fixed
1012

1113
- Changed the approach that the event processor uses to validate permissions on startup to ensure that it does not interrupt other processors already running by temporarily asserting ownership of a partition.

sdk/eventhub/Azure.Messaging.EventHubs/src/Producer/EventDataBatch.cs

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -180,34 +180,16 @@ public bool TryAdd(EventData eventData)
180180
{
181181
AssertNotLocked();
182182

183-
var messageScopeCreated = false;
184-
var traceparent = default(string);
183+
ClientDiagnostics.InstrumentMessage(eventData.Properties, DiagnosticProperty.EventActivityName, out var traceparent, out var tracestate);
185184

186-
try
187-
{
188-
ClientDiagnostics.InstrumentMessage(eventData.Properties, DiagnosticProperty.EventActivityName, out messageScopeCreated, out traceparent, out var tracestate);
189-
190-
var added = InnerBatch.TryAdd(eventData);
191-
192-
if ((added) && (traceparent != null))
193-
{
194-
EventDiagnosticIdentifiers.Add((traceparent, tracestate));
195-
}
185+
var added = InnerBatch.TryAdd(eventData);
196186

197-
return added;
198-
}
199-
finally
187+
if ((added) && (traceparent != null))
200188
{
201-
// If a new message scope was added when instrumenting the instance, the identifier was
202-
// added during this call. If so, remove it so that the source event is not modified; the
203-
// instrumentation will have been captured by the batch's copy of the event, if it was accepted
204-
// into the batch.
205-
206-
if ((messageScopeCreated) && (traceparent != null))
207-
{
208-
MessagingClientDiagnostics.ResetEvent(eventData.Properties);
209-
}
189+
EventDiagnosticIdentifiers.Add((traceparent, tracestate));
210190
}
191+
192+
return added;
211193
}
212194
}
213195

sdk/eventhub/Azure.Messaging.EventHubs/src/Producer/EventHubProducerClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -883,7 +883,7 @@ private async Task SendInternalAsync(IReadOnlyCollection<EventData> events,
883883

884884
foreach (var eventData in events)
885885
{
886-
ClientDiagnostics.InstrumentMessage(eventData.Properties, DiagnosticProperty.EventActivityName, out _, out var traceparent, out var tracestate);
886+
ClientDiagnostics.InstrumentMessage(eventData.Properties, DiagnosticProperty.EventActivityName, out var traceparent, out var tracestate);
887887

888888
if (traceparent != null)
889889
{

sdk/eventhub/Azure.Messaging.EventHubs/tests/Diagnostics/DiagnosticsActivitySourceTests.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,7 @@ public async Task EventHubProducerAppliesDiagnosticIdToEventsOnBatchSend()
224224
.Returns<EventData>(e =>
225225
{
226226
var hasSpace = writtenEventsData.Count <= 1;
227-
if (hasSpace)
228-
{
229-
writtenEventsData.Add(e.Clone());
230-
}
227+
writtenEventsData.Add(e.Clone());
231228
return hasSpace;
232229
});
233230

@@ -254,15 +251,13 @@ public async Task EventHubProducerAppliesDiagnosticIdToEventsOnBatchSend()
254251
await producer.SendAsync(batch);
255252

256253
activity.Stop();
257-
Assert.That(writtenEventsData.Count, Is.EqualTo(2), "Each of the events in the batch should have been instrumented.");
254+
Assert.That(writtenEventsData.Count, Is.EqualTo(3), "Each of the events should have been instrumented when attempting to add them to the batch.");
258255

259256
foreach (EventData eventData in writtenEventsData)
260257
{
261258
Assert.That(eventData.Properties.TryGetValue(MessagingClientDiagnostics.DiagnosticIdAttribute, out object value), Is.True, "The events should have a diagnostic identifier property.");
262259
Assert.That(value, Is.EqualTo(activity.Id), "The diagnostics identifier should match the activity in the active scope.");
263260
}
264-
265-
Assert.That(eventData3.Properties.ContainsKey(MessagingClientDiagnostics.DiagnosticIdAttribute), Is.False, "Events that were not accepted into the batch should not have been instrumented.");
266261
}
267262

268263
/// <summary>

sdk/eventhub/Azure.Messaging.EventHubs/tests/Diagnostics/DiagnosticsTests.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,7 @@ public async Task EventHubProducerAppliesDiagnosticIdToEventsOnBatchSend()
209209
.Returns<EventData>(e =>
210210
{
211211
var hasSpace = writtenEventsData.Count <= 1;
212-
if (hasSpace)
213-
{
214-
writtenEventsData.Add(e.Clone());
215-
}
212+
writtenEventsData.Add(e.Clone());
216213
return hasSpace;
217214
});
218215

@@ -239,15 +236,13 @@ public async Task EventHubProducerAppliesDiagnosticIdToEventsOnBatchSend()
239236
await producer.SendAsync(batch);
240237

241238
activity.Stop();
242-
Assert.That(writtenEventsData.Count, Is.EqualTo(2), "Each of the events in the batch should have been instrumented.");
239+
Assert.That(writtenEventsData.Count, Is.EqualTo(3), "Each of the events should have been instrumented when attempting to add them to the batch.");
243240

244241
foreach (EventData eventData in writtenEventsData)
245242
{
246243
Assert.That(eventData.Properties.TryGetValue(MessagingClientDiagnostics.DiagnosticIdAttribute, out object value), Is.True, "The events should have a diagnostic identifier property.");
247244
Assert.That(value, Is.EqualTo(activity.Id), "The diagnostics identifier should match the activity in the active scope.");
248245
}
249-
250-
Assert.That(eventData3.Properties.ContainsKey(MessagingClientDiagnostics.DiagnosticIdAttribute), Is.False, "Events that were not accepted into the batch should not have been instrumented.");
251246
}
252247

253248
/// <summary>

sdk/servicebus/Azure.Messaging.ServiceBus/src/Sender/ServiceBusMessageBatch.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public bool TryAddMessage(ServiceBusMessage message)
113113
{
114114
AssertNotLocked();
115115

116-
_clientDiagnostics.InstrumentMessage(message.ApplicationProperties, DiagnosticProperty.MessageActivityName, out var _, out var _, out var _);
116+
_clientDiagnostics.InstrumentMessage(message.ApplicationProperties, DiagnosticProperty.MessageActivityName, out var _, out var _);
117117
return _innerBatch.TryAddMessage(message);
118118
}
119119
}

sdk/servicebus/Azure.Messaging.ServiceBus/src/Sender/ServiceBusSender.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ private DiagnosticScope CreateDiagnosticScope(IReadOnlyCollection<ServiceBusMess
266266
{
267267
foreach (ServiceBusMessage message in messages)
268268
{
269-
_clientDiagnostics.InstrumentMessage(message.ApplicationProperties, DiagnosticProperty.MessageActivityName, out var _, out var _, out var _);
269+
_clientDiagnostics.InstrumentMessage(message.ApplicationProperties, DiagnosticProperty.MessageActivityName, out var _, out var _);
270270
}
271271

272272
// create a new scope for the specified operation

0 commit comments

Comments
 (0)