| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.Threading.Tasks; |
| | 6 | | using Azure.Core.Pipeline; |
| | 7 | |
|
| | 8 | | namespace Azure.Messaging.ServiceBus.Diagnostics |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// Enables diagnostics instrumentation to be applied to <see cref="ServiceBusMessage" /> |
| | 12 | | /// instances. |
| | 13 | | /// </summary> |
| | 14 | | /// |
| | 15 | | internal class EntityScopeFactory |
| | 16 | | { |
| | 17 | | /// <summary>The namespace used for the Service Bus diagnostic scope.</summary> |
| | 18 | | public const string DiagnosticNamespace = "Azure.Messaging.ServiceBus"; |
| | 19 | |
|
| | 20 | | /// <summary>The namespace used for the Azure Resource Manager provider namespace.</summary> |
| | 21 | | private const string _resourceProviderNamespace = "Microsoft.ServiceBus"; |
| | 22 | | private readonly string _entityPath; |
| | 23 | | private readonly string _fullyQualifiedNamespace; |
| | 24 | |
|
| | 25 | | /// <summary> |
| | 26 | | /// The client diagnostics instance responsible for managing scope. |
| | 27 | | /// </summary> |
| | 28 | | /// |
| 94 | 29 | | private static DiagnosticScopeFactory _scopeFactory { get; } = new DiagnosticScopeFactory(DiagnosticNamespace, _ |
| | 30 | |
|
| 122 | 31 | | public EntityScopeFactory( |
| 122 | 32 | | string entityPath, |
| 122 | 33 | | string fullyQualifiedNamespace) |
| | 34 | | { |
| 122 | 35 | | _entityPath = entityPath; |
| 122 | 36 | | _fullyQualifiedNamespace = fullyQualifiedNamespace; |
| 122 | 37 | | } |
| | 38 | |
|
| | 39 | | /// <summary> |
| | 40 | | /// Extracts a diagnostic id from a message's properties. |
| | 41 | | /// </summary> |
| | 42 | | /// |
| | 43 | | /// <param name="properties">The properties holding the diagnostic id.</param> |
| | 44 | | /// <param name="id">The value of the diagnostics identifier assigned to the event. </param> |
| | 45 | | /// |
| | 46 | | /// <returns><c>true</c> if the event was contained the diagnostic id; otherwise, <c>false</c>.</returns> |
| | 47 | | /// |
| | 48 | | public static bool TryExtractDiagnosticId(IDictionary<string, object> properties, out string id) |
| | 49 | | { |
| 0 | 50 | | id = null; |
| | 51 | |
|
| 0 | 52 | | if (properties.TryGetValue(DiagnosticProperty.DiagnosticIdAttribute, out var objectId) && objectId is string |
| | 53 | | { |
| 0 | 54 | | id = stringId; |
| 0 | 55 | | return true; |
| | 56 | | } |
| | 57 | |
|
| 0 | 58 | | return false; |
| | 59 | | } |
| | 60 | |
|
| | 61 | | public DiagnosticScope CreateScope( |
| | 62 | | string activityName, |
| | 63 | | string kindAttribute = default, |
| | 64 | | string lockToken = default, |
| | 65 | | string sessionId = default, |
| | 66 | | int? requestedMessageCount = default) |
| | 67 | | { |
| 92 | 68 | | DiagnosticScope scope = _scopeFactory.CreateScope(activityName); |
| 92 | 69 | | scope.AddAttribute(DiagnosticProperty.KindAttribute, kindAttribute ?? DiagnosticProperty.ClientKind); |
| 92 | 70 | | scope.AddAttribute( |
| 92 | 71 | | DiagnosticProperty.ServiceContextAttribute, |
| 92 | 72 | | DiagnosticProperty.ServiceBusServiceContext); |
| 92 | 73 | | scope.AddAttribute(DiagnosticProperty.EntityAttribute, _entityPath); |
| 92 | 74 | | scope.AddAttribute(DiagnosticProperty.EndpointAttribute, _fullyQualifiedNamespace); |
| 92 | 75 | | if (lockToken != null) |
| | 76 | | { |
| 20 | 77 | | scope.AddAttribute(DiagnosticProperty.LockTokensAttribute, lockToken); |
| | 78 | | } |
| 92 | 79 | | if (sessionId != null) |
| | 80 | | { |
| 0 | 81 | | scope.AddAttribute(DiagnosticProperty.SessionIdAttribute, sessionId); |
| | 82 | | } |
| 92 | 83 | | if (requestedMessageCount != null) |
| | 84 | | { |
| 20 | 85 | | scope.AddAttribute(DiagnosticProperty.RequestedMessageCountAttribute, requestedMessageCount); |
| | 86 | | } |
| 92 | 87 | | return scope; |
| | 88 | | } |
| | 89 | | } |
| | 90 | | } |