| | 1 | | // Copyright (c) Microsoft. All rights reserved. |
| | 2 | | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| | 3 | |
|
| | 4 | | namespace Microsoft.Azure.ServiceBus |
| | 5 | | { |
| | 6 | | using System; |
| | 7 | | using System.Collections.Generic; |
| | 8 | | using System.Diagnostics; |
| | 9 | | using System.Linq; |
| | 10 | | using System.Threading.Tasks; |
| | 11 | |
|
| | 12 | | using Microsoft.Azure.ServiceBus.Diagnostics; |
| | 13 | |
|
| | 14 | | internal class ServiceBusDiagnosticSource |
| | 15 | | { |
| | 16 | | public const string DiagnosticListenerName = "Microsoft.Azure.ServiceBus"; |
| | 17 | | public const string BaseActivityName = "Microsoft.Azure.ServiceBus."; |
| | 18 | |
|
| | 19 | | public const string ExceptionEventName = BaseActivityName + "Exception"; |
| | 20 | | public const string ProcessActivityName = BaseActivityName + "Process"; |
| | 21 | |
|
| | 22 | | public const string ActivityIdPropertyName = "Diagnostic-Id"; |
| | 23 | | public const string CorrelationContextPropertyName = "Correlation-Context"; |
| | 24 | | public const string RelatedToTag = "RelatedTo"; |
| | 25 | | public const string MessageIdTag = "MessageId"; |
| | 26 | | public const string SessionIdTag = "SessionId"; |
| | 27 | |
|
| 0 | 28 | | private static readonly DiagnosticListener DiagnosticListener = new DiagnosticListener(DiagnosticListenerName); |
| | 29 | | private readonly string entityPath; |
| | 30 | | private readonly Uri endpoint; |
| | 31 | |
|
| 12 | 32 | | public ServiceBusDiagnosticSource(string entityPath, Uri endpoint) |
| | 33 | | { |
| 12 | 34 | | this.entityPath = entityPath; |
| 12 | 35 | | this.endpoint = endpoint; |
| 12 | 36 | | } |
| | 37 | |
|
| | 38 | | public static bool IsEnabled() |
| | 39 | | { |
| 0 | 40 | | return DiagnosticListener.IsEnabled(); |
| | 41 | | } |
| | 42 | |
|
| | 43 | |
|
| | 44 | | #region Send |
| | 45 | |
|
| | 46 | | internal Activity SendStart(IList<Message> messageList) |
| | 47 | | { |
| 0 | 48 | | Activity activity = Start("Send", () => new |
| 0 | 49 | | { |
| 0 | 50 | | Messages = messageList, |
| 0 | 51 | | Entity = this.entityPath, |
| 0 | 52 | | Endpoint = this.endpoint |
| 0 | 53 | | }, |
| 0 | 54 | | a => SetTags(a, messageList) |
| 0 | 55 | | ); |
| | 56 | |
|
| 0 | 57 | | Inject(messageList); |
| | 58 | |
|
| 0 | 59 | | return activity; |
| | 60 | | } |
| | 61 | |
|
| | 62 | | internal void SendStop(Activity activity, IList<Message> messageList, TaskStatus? status) |
| | 63 | | { |
| 0 | 64 | | if (activity != null) |
| | 65 | | { |
| 0 | 66 | | DiagnosticListener.StopActivity(activity, new |
| 0 | 67 | | { |
| 0 | 68 | | Messages = messageList, |
| 0 | 69 | | Entity = this.entityPath, |
| 0 | 70 | | Endpoint = this.endpoint, |
| 0 | 71 | | Status = status ?? TaskStatus.Faulted |
| 0 | 72 | | }); |
| | 73 | | } |
| 0 | 74 | | } |
| | 75 | |
|
| | 76 | | #endregion |
| | 77 | |
|
| | 78 | |
|
| | 79 | | #region Process |
| | 80 | |
|
| | 81 | | internal Activity ProcessStart(Message message) |
| | 82 | | { |
| 0 | 83 | | return ProcessStart("Process", message, () => new |
| 0 | 84 | | { |
| 0 | 85 | | Message = message, |
| 0 | 86 | | Entity = this.entityPath, |
| 0 | 87 | | Endpoint = this.endpoint |
| 0 | 88 | | }, |
| 0 | 89 | | a => SetTags(a, message)); |
| | 90 | | } |
| | 91 | |
|
| | 92 | | internal void ProcessStop(Activity activity, Message message, TaskStatus? status) |
| | 93 | | { |
| 0 | 94 | | if (activity != null) |
| | 95 | | { |
| 0 | 96 | | DiagnosticListener.StopActivity(activity, new |
| 0 | 97 | | { |
| 0 | 98 | | Message = message, |
| 0 | 99 | | Entity = this.entityPath, |
| 0 | 100 | | Endpoint = this.endpoint, |
| 0 | 101 | | Status = status ?? TaskStatus.Faulted |
| 0 | 102 | | }); |
| | 103 | | } |
| 0 | 104 | | } |
| | 105 | |
|
| | 106 | | #endregion |
| | 107 | |
|
| | 108 | |
|
| | 109 | | #region ProcessSession |
| | 110 | |
|
| | 111 | | internal Activity ProcessSessionStart(IMessageSession session, Message message) |
| | 112 | | { |
| 0 | 113 | | return ProcessStart("ProcessSession", message, () => new |
| 0 | 114 | | { |
| 0 | 115 | | Session = session, |
| 0 | 116 | | Message = message, |
| 0 | 117 | | Entity = this.entityPath, |
| 0 | 118 | | Endpoint = this.endpoint |
| 0 | 119 | | }, |
| 0 | 120 | | a => SetTags(a, message)); |
| | 121 | | } |
| | 122 | |
|
| | 123 | | internal void ProcessSessionStop(Activity activity, IMessageSession session, Message message, TaskStatus? status |
| | 124 | | { |
| 0 | 125 | | if (activity != null) |
| | 126 | | { |
| 0 | 127 | | DiagnosticListener.StopActivity(activity, new |
| 0 | 128 | | { |
| 0 | 129 | | Session = session, |
| 0 | 130 | | Message = message, |
| 0 | 131 | | Entity = this.entityPath, |
| 0 | 132 | | Endpoint = this.endpoint, |
| 0 | 133 | | Status = status ?? TaskStatus.Faulted |
| 0 | 134 | | }); |
| | 135 | | } |
| 0 | 136 | | } |
| | 137 | |
|
| | 138 | | #endregion |
| | 139 | |
|
| | 140 | |
|
| | 141 | | #region Schedule |
| | 142 | |
|
| | 143 | | internal Activity ScheduleStart(Message message, DateTimeOffset scheduleEnqueueTimeUtc) |
| | 144 | | { |
| 0 | 145 | | Activity activity = Start("Schedule", () => new |
| 0 | 146 | | { |
| 0 | 147 | | Message = message, |
| 0 | 148 | | ScheduleEnqueueTimeUtc = scheduleEnqueueTimeUtc, |
| 0 | 149 | | Entity = this.entityPath, |
| 0 | 150 | | Endpoint = this.endpoint |
| 0 | 151 | | }, |
| 0 | 152 | | a => SetTags(a, message)); |
| | 153 | |
|
| 0 | 154 | | Inject(message); |
| | 155 | |
|
| 0 | 156 | | return activity; |
| | 157 | | } |
| | 158 | |
|
| | 159 | | internal void ScheduleStop(Activity activity, Message message, DateTimeOffset scheduleEnqueueTimeUtc, TaskStatus |
| | 160 | | { |
| 0 | 161 | | if (activity != null) |
| | 162 | | { |
| 0 | 163 | | DiagnosticListener.StopActivity(activity, new |
| 0 | 164 | | { |
| 0 | 165 | | Message = message, |
| 0 | 166 | | ScheduleEnqueueTimeUtc = scheduleEnqueueTimeUtc, |
| 0 | 167 | | Entity = this.entityPath, |
| 0 | 168 | | Endpoint = this.endpoint, |
| 0 | 169 | | SequenceNumber = sequenceNumber, |
| 0 | 170 | | Status = status ?? TaskStatus.Faulted |
| 0 | 171 | | }); |
| | 172 | | } |
| 0 | 173 | | } |
| | 174 | |
|
| | 175 | | #endregion |
| | 176 | |
|
| | 177 | |
|
| | 178 | | #region Cancel |
| | 179 | |
|
| | 180 | | internal Activity CancelStart(long sequenceNumber) |
| | 181 | | { |
| 0 | 182 | | return Start("Cancel", () => new |
| 0 | 183 | | { |
| 0 | 184 | | SequenceNumber = sequenceNumber, |
| 0 | 185 | | Entity = this.entityPath, |
| 0 | 186 | | Endpoint = this.endpoint |
| 0 | 187 | | }, |
| 0 | 188 | | null); |
| | 189 | | } |
| | 190 | |
|
| | 191 | | internal void CancelStop(Activity activity, long sequenceNumber, TaskStatus? status) |
| | 192 | | { |
| 0 | 193 | | if (activity != null) |
| | 194 | | { |
| 0 | 195 | | DiagnosticListener.StopActivity(activity, new |
| 0 | 196 | | { |
| 0 | 197 | | SequenceNumber = sequenceNumber, |
| 0 | 198 | | Entity = this.entityPath, |
| 0 | 199 | | Endpoint = this.endpoint, |
| 0 | 200 | | Status = status ?? TaskStatus.Faulted |
| 0 | 201 | | }); |
| | 202 | | } |
| 0 | 203 | | } |
| | 204 | |
|
| | 205 | | #endregion |
| | 206 | |
|
| | 207 | |
|
| | 208 | | #region Receive |
| | 209 | |
|
| | 210 | | internal Activity ReceiveStart(int messageCount) |
| | 211 | | { |
| 0 | 212 | | return Start("Receive", () => new |
| 0 | 213 | | { |
| 0 | 214 | | RequestedMessageCount = messageCount, |
| 0 | 215 | | Entity = this.entityPath, |
| 0 | 216 | | Endpoint = this.endpoint |
| 0 | 217 | | }, |
| 0 | 218 | | null); |
| | 219 | | } |
| | 220 | |
|
| | 221 | | internal void ReceiveStop(Activity activity, int messageCount, TaskStatus? status, IList<Message> messageList) |
| | 222 | | { |
| 0 | 223 | | if (activity != null) |
| | 224 | | { |
| 0 | 225 | | SetRelatedOperations(activity, messageList); |
| 0 | 226 | | SetTags(activity, messageList); |
| 0 | 227 | | DiagnosticListener.StopActivity(activity, new |
| 0 | 228 | | { |
| 0 | 229 | | RequestedMessageCount = messageCount, |
| 0 | 230 | | Entity = this.entityPath, |
| 0 | 231 | | Endpoint = this.endpoint, |
| 0 | 232 | | Status = status ?? TaskStatus.Faulted, |
| 0 | 233 | | Messages = messageList |
| 0 | 234 | | }); |
| | 235 | | } |
| 0 | 236 | | } |
| | 237 | |
|
| | 238 | | #endregion |
| | 239 | |
|
| | 240 | |
|
| | 241 | | #region Peek |
| | 242 | |
|
| | 243 | | internal Activity PeekStart(long fromSequenceNumber, int messageCount) |
| | 244 | | { |
| 0 | 245 | | return Start("Peek", () => new |
| 0 | 246 | | { |
| 0 | 247 | | FromSequenceNumber = fromSequenceNumber, |
| 0 | 248 | | RequestedMessageCount = messageCount, |
| 0 | 249 | | Entity = this.entityPath, |
| 0 | 250 | | Endpoint = this.endpoint |
| 0 | 251 | | }, |
| 0 | 252 | | null); |
| | 253 | | } |
| | 254 | |
|
| | 255 | | internal void PeekStop(Activity activity, long fromSequenceNumber, int messageCount, TaskStatus? status, IList<M |
| | 256 | | { |
| 0 | 257 | | if (activity != null) |
| | 258 | | { |
| 0 | 259 | | SetRelatedOperations(activity, messageList); |
| 0 | 260 | | SetTags(activity, messageList); |
| | 261 | |
|
| 0 | 262 | | DiagnosticListener.StopActivity(activity, new |
| 0 | 263 | | { |
| 0 | 264 | | FromSequenceNumber = fromSequenceNumber, |
| 0 | 265 | | RequestedMessageCount = messageCount, |
| 0 | 266 | | Entity = this.entityPath, |
| 0 | 267 | | Endpoint = this.endpoint, |
| 0 | 268 | | Status = status ?? TaskStatus.Faulted, |
| 0 | 269 | | Messages = messageList |
| 0 | 270 | | }); |
| | 271 | | } |
| 0 | 272 | | } |
| | 273 | |
|
| | 274 | | #endregion |
| | 275 | |
|
| | 276 | |
|
| | 277 | | #region ReceiveDeferred |
| | 278 | |
|
| | 279 | | internal Activity ReceiveDeferredStart(IEnumerable<long> sequenceNumbers) |
| | 280 | | { |
| 0 | 281 | | return Start("ReceiveDeferred", () => new |
| 0 | 282 | | { |
| 0 | 283 | | SequenceNumbers = sequenceNumbers, |
| 0 | 284 | | Entity = this.entityPath, |
| 0 | 285 | | Endpoint = this.endpoint |
| 0 | 286 | | }, |
| 0 | 287 | | null); |
| | 288 | | } |
| | 289 | |
|
| | 290 | | internal void ReceiveDeferredStop(Activity activity, IEnumerable<long> sequenceNumbers, TaskStatus? status, ILis |
| | 291 | | { |
| 0 | 292 | | if (activity != null) |
| | 293 | | { |
| 0 | 294 | | SetRelatedOperations(activity, messageList); |
| 0 | 295 | | SetTags(activity, messageList); |
| | 296 | |
|
| 0 | 297 | | DiagnosticListener.StopActivity(activity, new |
| 0 | 298 | | { |
| 0 | 299 | | SequenceNumbers = sequenceNumbers, |
| 0 | 300 | | Entity = this.entityPath, |
| 0 | 301 | | Endpoint = this.endpoint, |
| 0 | 302 | | Messages = messageList, |
| 0 | 303 | | Status = status ?? TaskStatus.Faulted |
| 0 | 304 | | }); |
| | 305 | | } |
| 0 | 306 | | } |
| | 307 | |
|
| | 308 | | #endregion |
| | 309 | |
|
| | 310 | |
|
| | 311 | | #region Complete |
| | 312 | |
|
| | 313 | | internal Activity CompleteStart(IList<string> lockTokens) |
| | 314 | | { |
| 0 | 315 | | return Start("Complete", () => new |
| 0 | 316 | | { |
| 0 | 317 | | LockTokens = lockTokens, |
| 0 | 318 | | Entity = this.entityPath, |
| 0 | 319 | | Endpoint = this.endpoint |
| 0 | 320 | | }, |
| 0 | 321 | | null); |
| | 322 | | } |
| | 323 | |
|
| | 324 | | internal void CompleteStop(Activity activity, IList<string> lockTokens, TaskStatus? status) |
| | 325 | | { |
| 0 | 326 | | if (activity != null) |
| | 327 | | { |
| 0 | 328 | | DiagnosticListener.StopActivity(activity, new |
| 0 | 329 | | { |
| 0 | 330 | | LockTokens = lockTokens, |
| 0 | 331 | | Entity = this.entityPath, |
| 0 | 332 | | Endpoint = this.endpoint, |
| 0 | 333 | | Status = status ?? TaskStatus.Faulted |
| 0 | 334 | | }); |
| | 335 | | } |
| 0 | 336 | | } |
| | 337 | |
|
| | 338 | | #endregion |
| | 339 | |
|
| | 340 | |
|
| | 341 | | #region Dispose |
| | 342 | |
|
| | 343 | | internal Activity DisposeStart(string operationName, string lockToken) |
| | 344 | | { |
| 0 | 345 | | return Start(operationName, () => new |
| 0 | 346 | | { |
| 0 | 347 | | LockToken = lockToken, |
| 0 | 348 | | Entity = this.entityPath, |
| 0 | 349 | | Endpoint = this.endpoint |
| 0 | 350 | | }, |
| 0 | 351 | | null); |
| | 352 | | } |
| | 353 | |
|
| | 354 | | internal void DisposeStop(Activity activity, string lockToken, TaskStatus? status) |
| | 355 | | { |
| 0 | 356 | | if (activity != null) |
| | 357 | | { |
| 0 | 358 | | DiagnosticListener.StopActivity(activity, new |
| 0 | 359 | | { |
| 0 | 360 | | LockToken = lockToken, |
| 0 | 361 | | Entity = this.entityPath, |
| 0 | 362 | | Endpoint = this.endpoint, |
| 0 | 363 | | Status = status ?? TaskStatus.Faulted |
| 0 | 364 | | }); |
| | 365 | | } |
| 0 | 366 | | } |
| | 367 | |
|
| | 368 | | #endregion |
| | 369 | |
|
| | 370 | |
|
| | 371 | | #region RenewLock |
| | 372 | |
|
| | 373 | | internal Activity RenewLockStart(string lockToken) |
| | 374 | | { |
| 0 | 375 | | return Start("RenewLock", () => new |
| 0 | 376 | | { |
| 0 | 377 | | LockToken = lockToken, |
| 0 | 378 | | Entity = this.entityPath, |
| 0 | 379 | | Endpoint = this.endpoint |
| 0 | 380 | | }, |
| 0 | 381 | | null); |
| | 382 | | } |
| | 383 | |
|
| | 384 | | internal void RenewLockStop(Activity activity, string lockToken, TaskStatus? status, DateTime lockedUntilUtc) |
| | 385 | | { |
| 0 | 386 | | if (activity != null) |
| | 387 | | { |
| 0 | 388 | | DiagnosticListener.StopActivity(activity, new |
| 0 | 389 | | { |
| 0 | 390 | | LockToken = lockToken, |
| 0 | 391 | | Entity = this.entityPath, |
| 0 | 392 | | Endpoint = this.endpoint, |
| 0 | 393 | | Status = status ?? TaskStatus.Faulted, |
| 0 | 394 | | LockedUntilUtc = lockedUntilUtc |
| 0 | 395 | | }); |
| | 396 | | } |
| 0 | 397 | | } |
| | 398 | |
|
| | 399 | | #endregion |
| | 400 | |
|
| | 401 | |
|
| | 402 | | #region AddRule |
| | 403 | |
|
| | 404 | | internal Activity AddRuleStart(RuleDescription description) |
| | 405 | | { |
| 0 | 406 | | return Start("AddRule", () => new |
| 0 | 407 | | { |
| 0 | 408 | | Rule = description, |
| 0 | 409 | | Entity = this.entityPath, |
| 0 | 410 | | Endpoint = this.endpoint |
| 0 | 411 | | }, |
| 0 | 412 | | null); |
| | 413 | | } |
| | 414 | |
|
| | 415 | | internal void AddRuleStop(Activity activity, RuleDescription description, TaskStatus? status) |
| | 416 | | { |
| 0 | 417 | | if (activity != null) |
| | 418 | | { |
| 0 | 419 | | DiagnosticListener.StopActivity(activity, new |
| 0 | 420 | | { |
| 0 | 421 | | Rule = description, |
| 0 | 422 | | Entity = this.entityPath, |
| 0 | 423 | | Endpoint = this.endpoint, |
| 0 | 424 | | Status = status ?? TaskStatus.Faulted |
| 0 | 425 | | }); |
| | 426 | | } |
| 0 | 427 | | } |
| | 428 | |
|
| | 429 | | #endregion |
| | 430 | |
|
| | 431 | |
|
| | 432 | | #region RemoveRule |
| | 433 | |
|
| | 434 | | internal Activity RemoveRuleStart(string ruleName) |
| | 435 | | { |
| 0 | 436 | | return Start("RemoveRule", () => new |
| 0 | 437 | | { |
| 0 | 438 | | RuleName = ruleName, |
| 0 | 439 | | Entity = this.entityPath, |
| 0 | 440 | | Endpoint = this.endpoint |
| 0 | 441 | | }, |
| 0 | 442 | | null); |
| | 443 | | } |
| | 444 | |
|
| | 445 | | internal void RemoveRuleStop(Activity activity, string ruleName, TaskStatus? status) |
| | 446 | | { |
| 0 | 447 | | if (activity != null) |
| | 448 | | { |
| 0 | 449 | | DiagnosticListener.StopActivity(activity, new |
| 0 | 450 | | { |
| 0 | 451 | | RuleName = ruleName, |
| 0 | 452 | | Entity = this.entityPath, |
| 0 | 453 | | Endpoint = this.endpoint, |
| 0 | 454 | | Status = status ?? TaskStatus.Faulted |
| 0 | 455 | | }); |
| | 456 | | } |
| 0 | 457 | | } |
| | 458 | |
|
| | 459 | | #endregion |
| | 460 | |
|
| | 461 | |
|
| | 462 | | #region GetRules |
| | 463 | |
|
| | 464 | | internal Activity GetRulesStart() |
| | 465 | | { |
| 0 | 466 | | return Start("GetRules", () => new |
| 0 | 467 | | { |
| 0 | 468 | | Entity = this.entityPath, |
| 0 | 469 | | Endpoint = this.endpoint |
| 0 | 470 | | }, |
| 0 | 471 | | null); |
| | 472 | | } |
| | 473 | |
|
| | 474 | | internal void GetRulesStop(Activity activity, IEnumerable<RuleDescription> rules, TaskStatus? status) |
| | 475 | | { |
| 0 | 476 | | if (activity != null) |
| | 477 | | { |
| 0 | 478 | | DiagnosticListener.StopActivity(activity, new |
| 0 | 479 | | { |
| 0 | 480 | | Rules = rules, |
| 0 | 481 | | Entity = this.entityPath, |
| 0 | 482 | | Endpoint = this.endpoint, |
| 0 | 483 | | Status = status ?? TaskStatus.Faulted |
| 0 | 484 | | }); |
| | 485 | | } |
| 0 | 486 | | } |
| | 487 | |
|
| | 488 | | #endregion |
| | 489 | |
|
| | 490 | |
|
| | 491 | | #region AcceptMessageSession |
| | 492 | |
|
| | 493 | | internal Activity AcceptMessageSessionStart(string sessionId) |
| | 494 | | { |
| 0 | 495 | | return Start("AcceptMessageSession", () => new |
| 0 | 496 | | { |
| 0 | 497 | | SessionId = sessionId, |
| 0 | 498 | | Entity = this.entityPath, |
| 0 | 499 | | Endpoint = this.endpoint |
| 0 | 500 | | }, |
| 0 | 501 | | a => SetSessionTag(a, sessionId) |
| 0 | 502 | | ); |
| | 503 | | } |
| | 504 | |
|
| | 505 | | internal void AcceptMessageSessionStop(Activity activity, string sessionId, TaskStatus? status) |
| | 506 | | { |
| 0 | 507 | | if (activity != null) |
| | 508 | | { |
| 0 | 509 | | DiagnosticListener.StopActivity(activity, new |
| 0 | 510 | | { |
| 0 | 511 | | SessionId = sessionId, |
| 0 | 512 | | Entity = this.entityPath, |
| 0 | 513 | | Endpoint = this.endpoint, |
| 0 | 514 | | Status = status ?? TaskStatus.Faulted |
| 0 | 515 | | }); |
| | 516 | | } |
| 0 | 517 | | } |
| | 518 | |
|
| | 519 | | #endregion |
| | 520 | |
|
| | 521 | |
|
| | 522 | | #region GetSessionStateAsync |
| | 523 | |
|
| | 524 | | internal Activity GetSessionStateStart(string sessionId) |
| | 525 | | { |
| 0 | 526 | | return Start("GetSessionState", () => new |
| 0 | 527 | | { |
| 0 | 528 | | SessionId = sessionId, |
| 0 | 529 | | Entity = this.entityPath, |
| 0 | 530 | | Endpoint = this.endpoint |
| 0 | 531 | | }, |
| 0 | 532 | | a => SetSessionTag(a, sessionId)); |
| | 533 | | } |
| | 534 | |
|
| | 535 | | internal void GetSessionStateStop(Activity activity, string sessionId, byte[] state, TaskStatus? status) |
| | 536 | | { |
| 0 | 537 | | if (activity != null) |
| | 538 | | { |
| 0 | 539 | | DiagnosticListener.StopActivity(activity, new |
| 0 | 540 | | { |
| 0 | 541 | | SessionId = sessionId, |
| 0 | 542 | | Entity = this.entityPath, |
| 0 | 543 | | Endpoint = this.endpoint, |
| 0 | 544 | | Status = status ?? TaskStatus.Faulted, |
| 0 | 545 | | State = state |
| 0 | 546 | | }); |
| | 547 | | } |
| 0 | 548 | | } |
| | 549 | |
|
| | 550 | | #endregion |
| | 551 | |
|
| | 552 | |
|
| | 553 | | #region SetSessionState |
| | 554 | |
|
| | 555 | | internal Activity SetSessionStateStart(string sessionId, byte[] state) |
| | 556 | | { |
| 0 | 557 | | return Start("SetSessionState", () => new |
| 0 | 558 | | { |
| 0 | 559 | | State = state, |
| 0 | 560 | | SessionId = sessionId, |
| 0 | 561 | | Entity = this.entityPath, |
| 0 | 562 | | Endpoint = this.endpoint |
| 0 | 563 | | }, |
| 0 | 564 | | a => SetSessionTag(a, sessionId)); |
| | 565 | | } |
| | 566 | |
|
| | 567 | | internal void SetSessionStateStop(Activity activity, byte[] state, string sessionId, TaskStatus? status) |
| | 568 | | { |
| 0 | 569 | | if (activity != null) |
| | 570 | | { |
| 0 | 571 | | DiagnosticListener.StopActivity(activity, new |
| 0 | 572 | | { |
| 0 | 573 | | State = state, |
| 0 | 574 | | SessionId = sessionId, |
| 0 | 575 | | Entity = this.entityPath, |
| 0 | 576 | | Endpoint = this.endpoint, |
| 0 | 577 | | Status = status ?? TaskStatus.Faulted |
| 0 | 578 | | }); |
| | 579 | | } |
| 0 | 580 | | } |
| | 581 | |
|
| | 582 | | #endregion |
| | 583 | |
|
| | 584 | |
|
| | 585 | | #region RenewSessionLock |
| | 586 | |
|
| | 587 | | internal Activity RenewSessionLockStart(string sessionId) |
| | 588 | | { |
| 0 | 589 | | return Start("RenewSessionLock", () => new |
| 0 | 590 | | { |
| 0 | 591 | | SessionId = sessionId, |
| 0 | 592 | | Entity = this.entityPath, |
| 0 | 593 | | Endpoint = this.endpoint |
| 0 | 594 | | }, |
| 0 | 595 | | a => SetSessionTag(a, sessionId)); |
| | 596 | | } |
| | 597 | |
|
| | 598 | | internal void RenewSessionLockStop(Activity activity, string sessionId, TaskStatus? status) |
| | 599 | | { |
| 0 | 600 | | if (activity != null) |
| | 601 | | { |
| 0 | 602 | | DiagnosticListener.StopActivity(activity, new |
| 0 | 603 | | { |
| 0 | 604 | | SessionId = sessionId, |
| 0 | 605 | | Entity = this.entityPath, |
| 0 | 606 | | Endpoint = this.endpoint, |
| 0 | 607 | | Status = status ?? TaskStatus.Faulted |
| 0 | 608 | | }); |
| | 609 | | } |
| 0 | 610 | | } |
| | 611 | |
|
| | 612 | | #endregion |
| | 613 | |
|
| | 614 | | internal void ReportException(Exception ex) |
| | 615 | | { |
| 0 | 616 | | if (DiagnosticListener.IsEnabled(ExceptionEventName)) |
| | 617 | | { |
| 0 | 618 | | DiagnosticListener.Write(ExceptionEventName, |
| 0 | 619 | | new |
| 0 | 620 | | { |
| 0 | 621 | | Exception = ex, |
| 0 | 622 | | Entity = this.entityPath, |
| 0 | 623 | | Endpoint = this.endpoint |
| 0 | 624 | | }); |
| | 625 | | } |
| 0 | 626 | | } |
| | 627 | |
|
| | 628 | | private Activity Start(string operationName, Func<object> getPayload, Action<Activity> setTags) |
| | 629 | | { |
| 0 | 630 | | Activity activity = null; |
| 0 | 631 | | string activityName = BaseActivityName + operationName; |
| 0 | 632 | | if (DiagnosticListener.IsEnabled(activityName, this.entityPath)) |
| | 633 | | { |
| 0 | 634 | | activity = new Activity(activityName); |
| 0 | 635 | | setTags?.Invoke(activity); |
| | 636 | |
|
| 0 | 637 | | if (DiagnosticListener.IsEnabled(activityName + ".Start")) |
| | 638 | | { |
| 0 | 639 | | DiagnosticListener.StartActivity(activity, getPayload()); |
| | 640 | | } |
| | 641 | | else |
| | 642 | | { |
| 0 | 643 | | activity.Start(); |
| | 644 | | } |
| | 645 | | } |
| | 646 | |
|
| 0 | 647 | | return activity; |
| | 648 | | } |
| | 649 | |
|
| | 650 | | private void Inject(IList<Message> messageList) |
| | 651 | | { |
| 0 | 652 | | var currentActivity = Activity.Current; |
| 0 | 653 | | if (currentActivity != null && messageList != null) |
| | 654 | | { |
| 0 | 655 | | var correlationContext = SerializeCorrelationContext(currentActivity.Baggage.ToList()); |
| | 656 | |
|
| 0 | 657 | | foreach (var message in messageList) |
| | 658 | | { |
| 0 | 659 | | Inject(message, currentActivity.Id, correlationContext); |
| | 660 | | } |
| | 661 | | } |
| 0 | 662 | | } |
| | 663 | |
|
| | 664 | | private void Inject(Message message) |
| | 665 | | { |
| 0 | 666 | | var currentActivity = Activity.Current; |
| 0 | 667 | | if (currentActivity != null) |
| | 668 | | { |
| 0 | 669 | | Inject(message, currentActivity.Id, SerializeCorrelationContext(currentActivity.Baggage.ToList())); |
| | 670 | | } |
| 0 | 671 | | } |
| | 672 | |
|
| | 673 | | private void Inject(Message message, string id, string correlationContext) |
| | 674 | | { |
| 0 | 675 | | if (message != null && !message.UserProperties.ContainsKey(ActivityIdPropertyName)) |
| | 676 | | { |
| 0 | 677 | | message.UserProperties[ActivityIdPropertyName] = id; |
| 0 | 678 | | if (correlationContext != null) |
| | 679 | | { |
| 0 | 680 | | message.UserProperties[CorrelationContextPropertyName] = correlationContext; |
| | 681 | | } |
| | 682 | | } |
| 0 | 683 | | } |
| | 684 | |
|
| | 685 | | private string SerializeCorrelationContext(IList<KeyValuePair<string,string>> baggage) |
| | 686 | | { |
| 0 | 687 | | if (baggage != null && baggage.Count > 0) |
| | 688 | | { |
| 0 | 689 | | return string.Join(",", baggage.Select(kvp => kvp.Key + "=" + kvp.Value)); |
| | 690 | | } |
| 0 | 691 | | return null; |
| | 692 | | } |
| | 693 | |
|
| | 694 | | private void SetRelatedOperations(Activity activity, IList<Message> messageList) |
| | 695 | | { |
| 0 | 696 | | if (messageList != null && messageList.Count > 0) |
| | 697 | | { |
| 0 | 698 | | var relatedTo = new List<string>(); |
| 0 | 699 | | foreach (var message in messageList) |
| | 700 | | { |
| 0 | 701 | | if (message.TryExtractId(out string id)) |
| | 702 | | { |
| 0 | 703 | | relatedTo.Add(id); |
| | 704 | | } |
| | 705 | | } |
| | 706 | |
|
| 0 | 707 | | if (relatedTo.Count > 0) |
| | 708 | | { |
| 0 | 709 | | activity.AddTag(RelatedToTag, string.Join(",", relatedTo.Distinct())); |
| | 710 | | } |
| | 711 | | } |
| 0 | 712 | | } |
| | 713 | |
|
| | 714 | | private Activity ProcessStart(string operationName, Message message, Func<object> getPayload, Action<Activity> s |
| | 715 | | { |
| 0 | 716 | | Activity activity = null; |
| 0 | 717 | | string activityName = BaseActivityName + operationName; |
| | 718 | |
|
| 0 | 719 | | if (message != null && DiagnosticListener.IsEnabled(activityName, entityPath)) |
| | 720 | | { |
| 0 | 721 | | var tmpActivity = message.ExtractActivity(activityName); |
| 0 | 722 | | setTags?.Invoke(tmpActivity); |
| | 723 | |
|
| 0 | 724 | | if (DiagnosticListener.IsEnabled(activityName, entityPath, tmpActivity)) |
| | 725 | | { |
| 0 | 726 | | activity = tmpActivity; |
| 0 | 727 | | if (DiagnosticListener.IsEnabled(activityName + ".Start")) |
| | 728 | | { |
| 0 | 729 | | DiagnosticListener.StartActivity(activity, getPayload()); |
| | 730 | | } |
| | 731 | | else |
| | 732 | | { |
| 0 | 733 | | activity.Start(); |
| | 734 | | } |
| | 735 | | } |
| | 736 | | } |
| 0 | 737 | | return activity; |
| | 738 | | } |
| | 739 | |
|
| | 740 | | private void SetTags(Activity activity, IList<Message> messageList) |
| | 741 | | { |
| 0 | 742 | | if (messageList == null) |
| | 743 | | { |
| 0 | 744 | | return; |
| | 745 | | } |
| | 746 | |
|
| 0 | 747 | | var messageIds = messageList.Where(m => m.MessageId != null).Select(m => m.MessageId).ToArray(); |
| 0 | 748 | | if (messageIds.Any()) |
| | 749 | | { |
| 0 | 750 | | activity.AddTag(MessageIdTag, string.Join(",", messageIds)); |
| | 751 | | } |
| | 752 | |
|
| 0 | 753 | | var sessionIds = messageList.Where(m => m.SessionId != null).Select(m => m.SessionId).Distinct().ToArray(); |
| 0 | 754 | | if (sessionIds.Any()) |
| | 755 | | { |
| 0 | 756 | | activity.AddTag(SessionIdTag, string.Join(",", sessionIds)); |
| | 757 | | } |
| 0 | 758 | | } |
| | 759 | |
|
| | 760 | | private void SetTags(Activity activity, Message message) |
| | 761 | | { |
| 0 | 762 | | if (message == null) |
| | 763 | | { |
| 0 | 764 | | return; |
| | 765 | | } |
| | 766 | |
|
| 0 | 767 | | if (message.MessageId != null) |
| | 768 | | { |
| 0 | 769 | | activity.AddTag(MessageIdTag, message.MessageId); |
| | 770 | | } |
| | 771 | |
|
| 0 | 772 | | SetSessionTag(activity, message.SessionId); |
| 0 | 773 | | } |
| | 774 | |
|
| | 775 | | private void SetSessionTag(Activity activity, string sessionId) |
| | 776 | | { |
| 0 | 777 | | if (sessionId != null) |
| | 778 | | { |
| 0 | 779 | | activity.AddTag(SessionIdTag, sessionId); |
| | 780 | | } |
| 0 | 781 | | } |
| | 782 | | } |
| | 783 | | } |