| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Text; |
| | 6 | | using Azure.Core.Pipeline; |
| | 7 | | using System.Linq; |
| | 8 | | using System.Collections.Generic; |
| | 9 | |
|
| | 10 | | namespace Azure.Messaging.ServiceBus.Diagnostics |
| | 11 | | { |
| | 12 | | internal static class DiagnosticExtensions |
| | 13 | | { |
| | 14 | | public static string GetAsciiString(this ArraySegment<byte> arraySegment) |
| | 15 | | { |
| 0 | 16 | | return arraySegment.Array == null ? string.Empty : Encoding.ASCII.GetString(arraySegment.Array, arraySegment |
| | 17 | | } |
| | 18 | |
|
| | 19 | | public static void SetMessageTags(this DiagnosticScope scope, IEnumerable<ServiceBusReceivedMessage> messages) |
| | 20 | | { |
| | 21 | | // set the message Ids on the scope |
| 0 | 22 | | var messageIds = messages.Where(m => m.MessageId != null).Select(m => m.MessageId).ToArray(); |
| 0 | 23 | | var sessionIds = messages.Where(m => m.SessionId != null).Select(m => m.SessionId).Distinct().ToArray(); |
| 20 | 24 | | scope.SetMessageTags(messageIds, sessionIds); |
| 20 | 25 | | } |
| | 26 | |
|
| | 27 | | private static void SetMessageTags(this DiagnosticScope scope, IEnumerable<ServiceBusMessage> messages) |
| | 28 | | { |
| 50 | 29 | | var messageIds = messages.Where(m => m.MessageId != null).Select(m => m.MessageId).ToArray(); |
| 0 | 30 | | var sessionIds = messages.Where(m => m.SessionId != null).Select(m => m.SessionId).Distinct().ToArray(); |
| 14 | 31 | | scope.SetMessageTags(messageIds, sessionIds); |
| 14 | 32 | | } |
| | 33 | |
|
| | 34 | | private static void SetMessageTags(this DiagnosticScope scope, string[] messageIds, string[] sessionIds) |
| | 35 | | { |
| | 36 | | // set the message Ids on the scope |
| 34 | 37 | | if (messageIds.Any()) |
| | 38 | | { |
| 10 | 39 | | scope.AddAttribute(DiagnosticProperty.MessageIdAttribute, string.Join(",", messageIds)); |
| | 40 | | } |
| | 41 | |
|
| | 42 | | // set any session Ids on the scope |
| 34 | 43 | | if (sessionIds.Any()) |
| | 44 | | { |
| 0 | 45 | | scope.AddAttribute(DiagnosticProperty.SessionIdAttribute, string.Join(",", sessionIds)); |
| | 46 | | } |
| 34 | 47 | | } |
| | 48 | |
|
| | 49 | | public static void SetMessageData(this DiagnosticScope scope, IEnumerable<ServiceBusReceivedMessage> messages) |
| | 50 | | { |
| 20 | 51 | | scope.AddLinkedDiagnostics(messages); |
| 20 | 52 | | scope.SetMessageTags(messages); |
| 20 | 53 | | } |
| | 54 | |
|
| | 55 | | public static void SetMessageData(this DiagnosticScope scope, IEnumerable<ServiceBusMessage> messages) |
| | 56 | | { |
| 14 | 57 | | scope.AddLinkedDiagnostics(messages); |
| 14 | 58 | | scope.SetMessageTags(messages); |
| 14 | 59 | | } |
| | 60 | |
|
| | 61 | | private static void AddLinkedDiagnostics(this DiagnosticScope scope, IEnumerable<ServiceBusReceivedMessage> mess |
| | 62 | | { |
| 20 | 63 | | if (scope.IsEnabled) |
| | 64 | | { |
| 0 | 65 | | foreach (ServiceBusReceivedMessage message in messages) |
| | 66 | | { |
| 0 | 67 | | AddLinkedDiagnostics(scope, message.SentMessage.Properties); |
| | 68 | | } |
| | 69 | | } |
| 20 | 70 | | } |
| | 71 | |
|
| | 72 | | private static void AddLinkedDiagnostics(this DiagnosticScope scope, IEnumerable<ServiceBusMessage> messages) |
| | 73 | | { |
| 14 | 74 | | if (scope.IsEnabled) |
| | 75 | | { |
| 0 | 76 | | foreach (ServiceBusMessage message in messages) |
| | 77 | | { |
| 0 | 78 | | AddLinkedDiagnostics(scope, message.Properties); |
| | 79 | | } |
| | 80 | | } |
| 14 | 81 | | } |
| | 82 | |
|
| | 83 | | private static void AddLinkedDiagnostics(this DiagnosticScope scope, IDictionary<string, object> properties) |
| | 84 | | { |
| 0 | 85 | | if (EntityScopeFactory.TryExtractDiagnosticId( |
| 0 | 86 | | properties, |
| 0 | 87 | | out string diagnosticId)) |
| | 88 | | { |
| 0 | 89 | | scope.AddLink(diagnosticId); |
| | 90 | | } |
| 0 | 91 | | } |
| | 92 | | } |
| | 93 | | } |