< Summary

Class:Azure.Messaging.EventHubs.Diagnostics.EventDataInstrumentation
Assembly:Azure.Messaging.EventHubs.Processor
File(s):C:\Git\azure-sdk-for-net\sdk\eventhub\Azure.Messaging.EventHubs.Shared\src\Diagnostics\EventDataInstrumentation.cs
Covered lines:1
Uncovered lines:18
Coverable lines:19
Total lines:92
Line coverage:5.2% (1 of 19)
Covered branches:0
Total branches:8
Branch coverage:0% (0 of 8)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
get_ScopeFactory()-100%100%
InstrumentEvent(...)-0%0%
TryExtractDiagnosticId(...)-0%0%
ResetEvent(...)-0%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\eventhub\Azure.Messaging.EventHubs.Shared\src\Diagnostics\EventDataInstrumentation.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System.Diagnostics;
 5using Azure.Core.Pipeline;
 6
 7namespace 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        ///
 1426        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        {
 042            if (!eventData.Properties.ContainsKey(DiagnosticProperty.DiagnosticIdAttribute))
 43            {
 044                using DiagnosticScope messageScope = ScopeFactory.CreateScope(DiagnosticProperty.EventActivityName);
 045                messageScope.AddAttribute(DiagnosticProperty.KindAttribute, DiagnosticProperty.ProducerKind);
 046                messageScope.AddAttribute(DiagnosticProperty.EventHubAttribute, eventHubName);
 047                messageScope.AddAttribute(DiagnosticProperty.EndpointAttribute, fullyQualifiedNamespace);
 048                messageScope.Start();
 49
 050                Activity activity = Activity.Current;
 051                if (activity != null)
 52                {
 053                    eventData.Properties[DiagnosticProperty.DiagnosticIdAttribute] = activity.Id;
 054                    return true;
 55                }
 56            }
 57
 058            return false;
 059        }
 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        {
 072            id = null;
 73
 074            if (eventData.Properties.TryGetValue(DiagnosticProperty.DiagnosticIdAttribute, out var objectId) && objectId
 75            {
 076                id = stringId;
 077                return true;
 78            }
 79
 080            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) =>
 090            eventData.Properties.Remove(DiagnosticProperty.DiagnosticIdAttribute);
 91    }
 92}