-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
DiagnosticExtensions.cs
93 lines (82 loc) · 3.53 KB
/
DiagnosticExtensions.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Text;
using Azure.Core.Pipeline;
using System.Linq;
using System.Collections.Generic;
namespace Azure.Messaging.ServiceBus.Diagnostics
{
internal static class DiagnosticExtensions
{
public static string GetAsciiString(this ArraySegment<byte> arraySegment)
{
return arraySegment.Array == null ? string.Empty : Encoding.ASCII.GetString(arraySegment.Array, arraySegment.Offset, arraySegment.Count);
}
public static void SetMessageTags(this DiagnosticScope scope, IEnumerable<ServiceBusReceivedMessage> messages)
{
// set the message Ids on the scope
var messageIds = messages.Where(m => m.MessageId != null).Select(m => m.MessageId).ToArray();
var sessionIds = messages.Where(m => m.SessionId != null).Select(m => m.SessionId).Distinct().ToArray();
scope.SetMessageTags(messageIds, sessionIds);
}
private static void SetMessageTags(this DiagnosticScope scope, IEnumerable<ServiceBusMessage> messages)
{
var messageIds = messages.Where(m => m.MessageId != null).Select(m => m.MessageId).ToArray();
var sessionIds = messages.Where(m => m.SessionId != null).Select(m => m.SessionId).Distinct().ToArray();
scope.SetMessageTags(messageIds, sessionIds);
}
private static void SetMessageTags(this DiagnosticScope scope, string[] messageIds, string[] sessionIds)
{
// set the message Ids on the scope
if (messageIds.Any())
{
scope.AddAttribute(DiagnosticProperty.MessageIdAttribute, string.Join(",", messageIds));
}
// set any session Ids on the scope
if (sessionIds.Any())
{
scope.AddAttribute(DiagnosticProperty.SessionIdAttribute, string.Join(",", sessionIds));
}
}
public static void SetMessageData(this DiagnosticScope scope, IEnumerable<ServiceBusReceivedMessage> messages)
{
scope.AddLinkedDiagnostics(messages);
scope.SetMessageTags(messages);
}
public static void SetMessageData(this DiagnosticScope scope, IEnumerable<ServiceBusMessage> messages)
{
scope.AddLinkedDiagnostics(messages);
scope.SetMessageTags(messages);
}
private static void AddLinkedDiagnostics(this DiagnosticScope scope, IEnumerable<ServiceBusReceivedMessage> messages)
{
if (scope.IsEnabled)
{
foreach (ServiceBusReceivedMessage message in messages)
{
AddLinkedDiagnostics(scope, message.AmqpMessage.ApplicationProperties);
}
}
}
private static void AddLinkedDiagnostics(this DiagnosticScope scope, IEnumerable<ServiceBusMessage> messages)
{
if (scope.IsEnabled)
{
foreach (ServiceBusMessage message in messages)
{
AddLinkedDiagnostics(scope, message.ApplicationProperties);
}
}
}
private static void AddLinkedDiagnostics(this DiagnosticScope scope, IDictionary<string, object> properties)
{
if (EntityScopeFactory.TryExtractDiagnosticId(
properties,
out string diagnosticId))
{
scope.AddLink(diagnosticId);
}
}
}
}