/
EventProcessorClientEventSource.cs
369 lines (336 loc) · 20.1 KB
/
EventProcessorClientEventSource.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Diagnostics.Tracing;
using System.Runtime.CompilerServices;
using Azure.Core.Diagnostics;
namespace Azure.Messaging.EventHubs.Processor.Diagnostics
{
/// <summary>
/// Serves as an ETW event source for logging of information about
/// Event Processor client.
/// </summary>
///
/// <remarks>
/// When defining Start/Stop tasks, it is highly recommended that the
/// the StopEvent.Id must be exactly StartEvent.Id + 1.
/// </remarks>
///
[EventSource(Name = EventSourceName)]
internal class EventProcessorClientEventSource : AzureEventSource
{
/// <summary>The name to use for the event source.</summary>
private const string EventSourceName = "Azure-Messaging-EventHubs-Processor-EventProcessorClient";
/// <summary>
/// Provides a singleton instance of the event source for callers to
/// use for logging.
/// </summary>
///
public static EventProcessorClientEventSource Log { get; } = new EventProcessorClientEventSource();
/// <summary>
/// Prevents an instance of the <see cref="EventProcessorClientEventSource" /> class from being created
/// outside the scope of this library. Exposed for testing purposes only.
/// </summary>
///
protected EventProcessorClientEventSource() : base(EventSourceName)
{
}
/// <summary>
/// Indicates that an <see cref="EventProcessorClient" /> has begin processing a batch of events for a partition.
/// </summary>
///
/// <param name="partitionId">The identifier of the Event Hub partition whose processing is taking place.</param>
/// <param name="identifier">A unique name used to identify the event processor.</param>
/// <param name="eventHubName">The name of the Event Hub that the processor is associated with.</param>
/// <param name="consumerGroup">The name of the consumer group that the processor is associated with.</param>
/// <param name="operationId">An identifier for the processing operation, allowing its activities to be correlated.</param>
///
[Event(20, Level = EventLevel.Verbose, Message = "Starting to process a batch of events for partition '{0}' by processor instance with identifier '{1}' for Event Hub: {2} and Consumer Group: {3}. Operation Id: '{4}'")]
public virtual void EventBatchProcessingStart(string partitionId,
string identifier,
string eventHubName,
string consumerGroup,
string operationId)
{
if (IsEnabled())
{
WriteEvent(20, partitionId ?? string.Empty, identifier ?? string.Empty, eventHubName ?? string.Empty, consumerGroup ?? string.Empty, operationId ?? string.Empty);
}
}
/// <summary>
/// Indicates that an <see cref="EventProcessorClient" /> instance has experienced an exception while processing a batch of events.
/// </summary>
///
/// <param name="partitionId">The identifier of the Event Hub partition whose processing is taking place.</param>
/// <param name="identifier">A unique name used to identify the event processor.</param>
/// <param name="eventHubName">The name of the Event Hub that the processor is associated with.</param>
/// <param name="consumerGroup">The name of the consumer group that the processor is associated with.</param>
/// <param name="operationId">An identifier for the processing operation, allowing its activities to be correlated.</param>
///
[Event(21, Level = EventLevel.Verbose, Message = "Completed processing a batch of events for partition '{0}' by processor instance with identifier '{1}' for Event Hub: {2} and Consumer Group: {3}. Operation Id: '{4}'")]
public virtual void EventBatchProcessingComplete(string partitionId,
string identifier,
string eventHubName,
string consumerGroup,
string operationId)
{
if (IsEnabled())
{
WriteEvent(21, partitionId ?? string.Empty, identifier ?? string.Empty, eventHubName ?? string.Empty, consumerGroup ?? string.Empty, operationId ?? string.Empty);
}
}
/// <summary>
/// Indicates that an <see cref="EventProcessorClient" /> has completed processing for a batch of events.
/// </summary>
///
/// <param name="partitionId">The identifier of the Event Hub partition whose processing is taking place.</param>
/// <param name="identifier">A unique name used to identify the event processor.</param>
/// <param name="eventHubName">The name of the Event Hub that the processor is associated with.</param>
/// <param name="consumerGroup">The name of the consumer group that the processor is associated with.</param>
/// <param name="errorMessage">The message for the exception that occurred.</param>
/// <param name="operationId">An identifier for the processing operation, allowing its activities to be correlated.</param>
///
[Event(22, Level = EventLevel.Error, Message = "An exception occurred while processing events for partition '{0}' by processor instance with identifier '{1}' for Event Hub: {2} and Consumer Group: {3}. Error Message: '{4}'; Operation Id: '{5}'")]
public virtual void EventBatchProcessingError(string partitionId,
string identifier,
string eventHubName,
string consumerGroup,
string errorMessage,
string operationId)
{
if (IsEnabled())
{
WriteEvent(22, partitionId ?? string.Empty, identifier ?? string.Empty, eventHubName ?? string.Empty, consumerGroup ?? string.Empty, errorMessage ?? string.Empty, operationId ?? string.Empty);
}
}
/// <summary>
/// Indicates that process of updating the checkpoint in the chosen storage service has started.
/// </summary>
///
/// <param name="partitionId">The identifier of the Event Hub partition whose processing is taking place.</param>
/// <param name="identifier">A unique name used to identify the event processor.</param>
/// <param name="eventHubName">The name of the Event Hub that the processor is associated with.</param>
/// <param name="consumerGroup">The name of the consumer group that the processor is associated with.</param>
///
[Event(23, Level = EventLevel.Verbose, Message = "Starting to perform a checkpoint update for partition '{0}' by processor instance with identifier '{1}' for Event Hub: {2} and Consumer Group: {3}.")]
public virtual void UpdateCheckpointStart(string partitionId,
string identifier,
string eventHubName,
string consumerGroup)
{
if (IsEnabled())
{
WriteEvent(23, partitionId ?? string.Empty, identifier ?? string.Empty, eventHubName ?? string.Empty, consumerGroup ?? string.Empty);
}
}
/// <summary>
/// Indicates that process of updating the checkpoint in the chosen storage service has completed.
/// </summary>
///
/// <param name="partitionId">The identifier of the Event Hub partition whose processing is taking place.</param>
/// <param name="identifier">A unique name used to identify the event processor.</param>
/// <param name="eventHubName">The name of the Event Hub that the processor is associated with.</param>
/// <param name="consumerGroup">The name of the consumer group that the processor is associated with.</param>
///
[Event(24, Level = EventLevel.Verbose, Message = "Completed performing a checkpoint update for partition '{0}' by processor instance with identifier '{1}' for Event Hub: {2} and Consumer Group: {3}.")]
public virtual void UpdateCheckpointComplete(string partitionId,
string identifier,
string eventHubName,
string consumerGroup)
{
if (IsEnabled())
{
WriteEvent(24, partitionId ?? string.Empty, identifier ?? string.Empty, eventHubName ?? string.Empty, consumerGroup ?? string.Empty);
}
}
/// <summary>
/// Indicates that the process of updating the checkpoint in the chosen storage service has experienced an exception.
/// </summary>
///
/// <param name="partitionId">The identifier of the Event Hub partition whose processing is taking place.</param>
/// <param name="identifier">A unique name used to identify the event processor.</param>
/// <param name="eventHubName">The name of the Event Hub that the processor is associated with.</param>
/// <param name="consumerGroup">The name of the consumer group that the processor is associated with.</param>
/// <param name="errorMessage">The message for the exception that occurred.</param>
///
[Event(25, Level = EventLevel.Error, Message = "An exception occurred while attempting to perform a checkpoint update for partition '{0}' by processor instance with identifier '{1}' for Event Hub: {2} and Consumer Group: {3}. Error Message: '{4}'")]
public virtual void UpdateCheckpointError(string partitionId,
string identifier,
string eventHubName,
string consumerGroup,
string errorMessage)
{
if (IsEnabled())
{
WriteEvent(25, partitionId ?? string.Empty, identifier ?? string.Empty, eventHubName ?? string.Empty, consumerGroup ?? string.Empty, errorMessage ?? string.Empty);
}
}
/// <summary>
/// Indicates that the process of cleaning up after startup validation has experienced an exception.
/// </summary>
///
/// <param name="identifier">A unique name used to identify the event processor.</param>
/// <param name="eventHubName">The name of the Event Hub that the processor is associated with.</param>
/// <param name="consumerGroup">The name of the consumer group that the processor is associated with.</param>
/// <param name="errorMessage">The message for the exception that occurred.</param>
///
[Event(26, Level = EventLevel.Error, Message = "An exception occurred while attempting to perform cleanup after validating the processor configuration and permissions during startup for processor instance with identifier '{1}' for Event Hub: {2} and Consumer Group: {3}. Error Message: '{4}'")]
public virtual void ValidationCleanupError(string identifier,
string eventHubName,
string consumerGroup,
string errorMessage)
{
if (IsEnabled())
{
WriteEvent(26, identifier ?? string.Empty, eventHubName ?? string.Empty, consumerGroup ?? string.Empty, errorMessage ?? string.Empty);
}
}
/// <summary>
/// Indicates that an <see cref="EventProcessorClient" /> has begin processing a batch of events for a partition.
/// </summary>
///
/// <param name="sequenceNumber">The sequence number of the event being passed to the handler for processing.</param>
/// <param name="partitionId">The identifier of the Event Hub partition whose processing is taking place.</param>
/// <param name="identifier">A unique name used to identify the event processor.</param>
/// <param name="eventHubName">The name of the Event Hub that the processor is associated with.</param>
/// <param name="consumerGroup">The name of the consumer group that the processor is associated with.</param>
/// <param name="operationId">An identifier for the processing operation, allowing its activities to be correlated.</param>
///
[Event(27, Level = EventLevel.Verbose, Message = "Invoking the event processing handler for sequence number '{0}'. Partition '{1}'; Processor Identifier '{2}'; Event Hub: {3}; Consumer Group: {4}; Operation Id: '{5}'")]
public virtual void EventBatchProcessingHandlerCall(string sequenceNumber,
string partitionId,
string identifier,
string eventHubName,
string consumerGroup,
string operationId)
{
if (IsEnabled())
{
WriteEvent(27, sequenceNumber ?? string.Empty, partitionId ?? string.Empty, identifier ?? string.Empty, eventHubName ?? string.Empty, consumerGroup ?? string.Empty, operationId ?? string.Empty);
}
}
/// <summary>
/// Writes an event with four string arguments into a stack allocated <see cref="EventSource.EventData"/> struct
/// to avoid the parameter array allocation on the WriteEvent methods.
/// </summary>
///
/// <param name="eventId">The identifier of the event.</param>
/// <param name="arg1">The first argument.</param>
/// <param name="arg2">The second argument.</param>
/// <param name="arg3">The third argument.</param>
/// <param name="arg4">The fourth argument.</param>
///
[NonEvent]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private unsafe void WriteEvent(int eventId,
string arg1,
string arg2,
string arg3,
string arg4)
{
fixed (char* arg1Ptr = arg1)
fixed (char* arg2Ptr = arg2)
fixed (char* arg3Ptr = arg3)
fixed (char* arg4Ptr = arg4)
{
var eventPayload = stackalloc EventData[4];
eventPayload[0].Size = (arg1.Length + 1) * sizeof(char);
eventPayload[0].DataPointer = (IntPtr)arg1Ptr;
eventPayload[1].Size = (arg2.Length + 1) * sizeof(char);
eventPayload[1].DataPointer = (IntPtr)arg2Ptr;
eventPayload[2].Size = (arg3.Length + 1) * sizeof(char);
eventPayload[2].DataPointer = (IntPtr)arg3Ptr;
eventPayload[3].Size = (arg4.Length + 1) * sizeof(char);
eventPayload[3].DataPointer = (IntPtr)arg4Ptr;
WriteEventCore(eventId, 4, eventPayload);
}
}
/// <summary>
/// Writes an event with five string arguments into a stack allocated
/// <see cref="EventSource.EventData"/> struct to avoid the parameter array allocation on the WriteEvent methods.
/// </summary>
///
/// <param name="eventId">The identifier of the event.</param>
/// <param name="arg1">The first argument.</param>
/// <param name="arg2">The second argument.</param>
/// <param name="arg3">The third argument.</param>
/// <param name="arg4">The fourth argument.</param>
/// <param name="arg5">The fifth argument.</param>
///
[NonEvent]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private unsafe void WriteEvent(int eventId,
string arg1,
string arg2,
string arg3,
string arg4,
string arg5)
{
fixed (char* arg1Ptr = arg1)
fixed (char* arg2Ptr = arg2)
fixed (char* arg3Ptr = arg3)
fixed (char* arg4Ptr = arg4)
fixed (char* arg5Ptr = arg5)
{
var eventPayload = stackalloc EventData[5];
eventPayload[0].Size = (arg1.Length + 1) * sizeof(char);
eventPayload[0].DataPointer = (IntPtr)arg1Ptr;
eventPayload[1].Size = (arg2.Length + 1) * sizeof(char);
eventPayload[1].DataPointer = (IntPtr)arg2Ptr;
eventPayload[2].Size = (arg3.Length + 1) * sizeof(char);
eventPayload[2].DataPointer = (IntPtr)arg3Ptr;
eventPayload[3].Size = (arg4.Length + 1) * sizeof(char);
eventPayload[3].DataPointer = (IntPtr)arg4Ptr;
eventPayload[4].Size = (arg5.Length + 1) * sizeof(char);
eventPayload[4].DataPointer = (IntPtr)arg5Ptr;
WriteEventCore(eventId, 5, eventPayload);
}
}
/// <summary>
/// Writes an event with five string arguments into a stack allocated
/// <see cref="EventSource.EventData"/> struct to avoid the parameter array allocation on the WriteEvent methods.
/// </summary>
///
/// <param name="eventId">The identifier of the event.</param>
/// <param name="arg1">The first argument.</param>
/// <param name="arg2">The second argument.</param>
/// <param name="arg3">The third argument.</param>
/// <param name="arg4">The fourth argument.</param>
/// <param name="arg5">The fifth argument.</param>
/// <param name="arg6">The sixth argument.</param>
///
[NonEvent]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private unsafe void WriteEvent(int eventId,
string arg1,
string arg2,
string arg3,
string arg4,
string arg5,
string arg6)
{
fixed (char* arg1Ptr = arg1)
fixed (char* arg2Ptr = arg2)
fixed (char* arg3Ptr = arg3)
fixed (char* arg4Ptr = arg4)
fixed (char* arg5Ptr = arg5)
fixed (char* arg6Ptr = arg6)
{
var eventPayload = stackalloc EventData[6];
eventPayload[0].Size = (arg1.Length + 1) * sizeof(char);
eventPayload[0].DataPointer = (IntPtr)arg1Ptr;
eventPayload[1].Size = (arg2.Length + 1) * sizeof(char);
eventPayload[1].DataPointer = (IntPtr)arg2Ptr;
eventPayload[2].Size = (arg3.Length + 1) * sizeof(char);
eventPayload[2].DataPointer = (IntPtr)arg3Ptr;
eventPayload[3].Size = (arg4.Length + 1) * sizeof(char);
eventPayload[3].DataPointer = (IntPtr)arg4Ptr;
eventPayload[4].Size = (arg5.Length + 1) * sizeof(char);
eventPayload[4].DataPointer = (IntPtr)arg5Ptr;
eventPayload[5].Size = (arg5.Length + 1) * sizeof(char);
eventPayload[5].DataPointer = (IntPtr)arg5Ptr;
WriteEventCore(eventId, 6, eventPayload);
}
}
}
}