| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System.Diagnostics; |
| | 5 | | using Azure.Core.Pipeline; |
| | 6 | |
|
| | 7 | | namespace Azure.Messaging.EventHubs.Diagnostics |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// Enables diagnostics instrumentation to be applied to <see cref="EventData" /> |
| | 11 | | /// instances. |
| | 12 | | /// </summary> |
| | 13 | | /// |
| | 14 | | internal static class EventDataInstrumentation |
| | 15 | | { |
| | 16 | | /// <summary>The namespace used for the Event Hubs diagnostic scope.</summary> |
| | 17 | | public const string DiagnosticNamespace = "Azure.Messaging.EventHubs"; |
| | 18 | |
|
| | 19 | | /// <summary>The namespace used for the Azure Resource Manager provider namespace.</summary> |
| | 20 | | public const string ResourceProviderNamespace = "Microsoft.EventHub"; |
| | 21 | |
|
| | 22 | | /// <summary> |
| | 23 | | /// The client diagnostics instance responsible for managing scope. |
| | 24 | | /// </summary> |
| | 25 | | /// |
| 3030070 | 26 | | public static DiagnosticScopeFactory ScopeFactory { get; } = new DiagnosticScopeFactory(DiagnosticNamespace, Res |
| | 27 | |
|
| | 28 | | /// <summary> |
| | 29 | | /// Applies diagnostics instrumentation to a given event. |
| | 30 | | /// </summary> |
| | 31 | | /// |
| | 32 | | /// <param name="eventData">The event to instrument.</param> |
| | 33 | | /// <param name="fullyQualifiedNamespace">The fully qualified Event Hubs namespace to use for instrumentation.</ |
| | 34 | | /// <param name="eventHubName">The name of the specific Event Hub to associate the event with.</param> |
| | 35 | | /// |
| | 36 | | /// <returns><c>true</c> if the event was instrumented in response to this request; otherwise, <c>false</c>.</re |
| | 37 | | /// |
| | 38 | | public static bool InstrumentEvent(EventData eventData, |
| | 39 | | string fullyQualifiedNamespace, |
| | 40 | | string eventHubName) |
| | 41 | | { |
| 52 | 42 | | if (!eventData.Properties.ContainsKey(DiagnosticProperty.DiagnosticIdAttribute)) |
| | 43 | | { |
| 42 | 44 | | using DiagnosticScope messageScope = ScopeFactory.CreateScope(DiagnosticProperty.EventActivityName); |
| 42 | 45 | | messageScope.AddAttribute(DiagnosticProperty.KindAttribute, DiagnosticProperty.ProducerKind); |
| 42 | 46 | | messageScope.AddAttribute(DiagnosticProperty.EventHubAttribute, eventHubName); |
| 42 | 47 | | messageScope.AddAttribute(DiagnosticProperty.EndpointAttribute, fullyQualifiedNamespace); |
| 42 | 48 | | messageScope.Start(); |
| | 49 | |
|
| 42 | 50 | | Activity activity = Activity.Current; |
| 42 | 51 | | if (activity != null) |
| | 52 | | { |
| 14 | 53 | | eventData.Properties[DiagnosticProperty.DiagnosticIdAttribute] = activity.Id; |
| 14 | 54 | | return true; |
| | 55 | | } |
| | 56 | | } |
| | 57 | |
|
| 38 | 58 | | return false; |
| 14 | 59 | | } |
| | 60 | |
|
| | 61 | | /// <summary> |
| | 62 | | /// Extracts a diagnostic id from the given event. |
| | 63 | | /// </summary> |
| | 64 | | /// |
| | 65 | | /// <param name="eventData">The event to instrument.</param> |
| | 66 | | /// <param name="id">The value of the diagnostics identifier assigned to the event. </param> |
| | 67 | | /// |
| | 68 | | /// <returns><c>true</c> if the event was contained the diagnostic id; otherwise, <c>false</c>.</returns> |
| | 69 | | /// |
| | 70 | | public static bool TryExtractDiagnosticId(EventData eventData, out string id) |
| | 71 | | { |
| 54 | 72 | | id = null; |
| | 73 | |
|
| 54 | 74 | | if (eventData.Properties.TryGetValue(DiagnosticProperty.DiagnosticIdAttribute, out var objectId) && objectId |
| | 75 | | { |
| 26 | 76 | | id = stringId; |
| 26 | 77 | | return true; |
| | 78 | | } |
| | 79 | |
|
| 28 | 80 | | return false; |
| | 81 | | } |
| | 82 | |
|
| | 83 | | /// <summary> |
| | 84 | | /// Resets the instrumentation associated with a given event. |
| | 85 | | /// </summary> |
| | 86 | | /// |
| | 87 | | /// <param name="eventData">The event to reset.</param> |
| | 88 | | /// |
| | 89 | | public static void ResetEvent(EventData eventData) => |
| 0 | 90 | | eventData.Properties.Remove(DiagnosticProperty.DiagnosticIdAttribute); |
| | 91 | | } |
| | 92 | | } |