< Summary

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

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
get_ScopeFactory()-100%100%
InstrumentEvent(...)-100%100%
TryExtractDiagnosticId(...)-100%100%
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        ///
 303007026        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        {
 5242            if (!eventData.Properties.ContainsKey(DiagnosticProperty.DiagnosticIdAttribute))
 43            {
 4244                using DiagnosticScope messageScope = ScopeFactory.CreateScope(DiagnosticProperty.EventActivityName);
 4245                messageScope.AddAttribute(DiagnosticProperty.KindAttribute, DiagnosticProperty.ProducerKind);
 4246                messageScope.AddAttribute(DiagnosticProperty.EventHubAttribute, eventHubName);
 4247                messageScope.AddAttribute(DiagnosticProperty.EndpointAttribute, fullyQualifiedNamespace);
 4248                messageScope.Start();
 49
 4250                Activity activity = Activity.Current;
 4251                if (activity != null)
 52                {
 1453                    eventData.Properties[DiagnosticProperty.DiagnosticIdAttribute] = activity.Id;
 1454                    return true;
 55                }
 56            }
 57
 3858            return false;
 1459        }
 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        {
 5472            id = null;
 73
 5474            if (eventData.Properties.TryGetValue(DiagnosticProperty.DiagnosticIdAttribute, out var objectId) && objectId
 75            {
 2676                id = stringId;
 2677                return true;
 78            }
 79
 2880            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}