public void MultiHost_TwoActiveAndOneIsDisposedStillTracksTelemetry()
        {
            var inclusionList = new[] { "Test.A" }.ToList();

            var dl1 = new TelemetryDiagnosticSourceListener(this.configuration, inclusionList);

            dl1.Subscribe();

            using (var listener = new DiagnosticListener("Test.A"))
                using (var dl2 = new TelemetryDiagnosticSourceListener(this.configuration, inclusionList))
                {
                    dl2.Subscribe();

                    Activity activity = new Activity("Test.A.Client.Monitoring");
                    listener.StartActivity(activity, null);
                    listener.StopActivity(activity, null);

                    Assert.AreEqual(1, this.sentItems.Count(t => t is DependencyTelemetry));

                    dl1.Dispose();

                    activity = new Activity("Test.A.Client.Monitoring");
                    listener.StartActivity(activity, null);
                    listener.StopActivity(activity, null);

                    Assert.AreEqual(2, this.sentItems.Count(t => t is DependencyTelemetry));
                }
        }
        public void MultiHost_OneListnerThenAnotherTracksTelemetry()
        {
            var inclusionList = new[] { "Test.A" }.ToList();

            using (var listener = new DiagnosticListener("Test.A"))
            {
                using (var dl = new TelemetryDiagnosticSourceListener(this.configuration, inclusionList))
                {
                    dl.Subscribe();

                    Activity activity = new Activity("Test.A.Client.Monitoring");
                    listener.StartActivity(activity, null);
                    listener.StopActivity(activity, null);

                    Assert.AreEqual(1, this.sentItems.Count(t => t is DependencyTelemetry));
                }

                using (var dl = new TelemetryDiagnosticSourceListener(this.configuration, inclusionList))
                {
                    dl.Subscribe();

                    Activity activity = new Activity("Test.A.Client.Monitoring");
                    listener.StartActivity(activity, null);
                    listener.StopActivity(activity, null);

                    Assert.AreEqual(2, this.sentItems.Count(t => t is DependencyTelemetry));
                }
            }
        }
        public void TelemetryDiagnosticSourceListenerCollectsTelemetryFromRawActivity()
        {
            var inclusionList = new[] { "Test.A" }.ToList();

            using (var listener = new DiagnosticListener("Test.A"))
                using (var dl = new TelemetryDiagnosticSourceListener(this.configuration, inclusionList))
                {
                    dl.Subscribe();

                    // generic example
                    var tags = new Dictionary <string, string>()
                    {
                        ["error"]         = "true",
                        ["peer.hostname"] = "test.example.com",
                        ["custom.tag"]    = "test"
                    };

                    DependencyTelemetry telemetryItem = this.CollectDependencyTelemetryFromActivity(listener, tags);

                    Assert.AreEqual(telemetryItem.Name, "Test.A.Client.Monitoring"); // Activity name
                    Assert.AreEqual(telemetryItem.Type, listener.Name);
                    Assert.IsTrue(string.IsNullOrEmpty(telemetryItem.Data));
                    Assert.AreEqual(telemetryItem.Target, tags["peer.hostname"]);
                    Assert.AreEqual(telemetryItem.Success, false);
                    Assert.IsTrue(telemetryItem.Properties.ContainsKey("custom.tag"));
                    Assert.AreEqual(telemetryItem.Properties["custom.tag"], tags["custom.tag"]);
                }
        }
        public void TelemetryDiagnosticSourceListenerCallsCustomHandlersWhenEnabled()
        {
            var inclusionList = new[] { "Test.A:Send", "Test.B" }.ToList();

            using (var telemetryListener = new TelemetryDiagnosticSourceListener(this.configuration, inclusionList))
            {
                telemetryListener.Subscribe();
                TestableEventHandler ahandler = new TestableEventHandler();
                TestableEventHandler bhandler = new TestableEventHandler();
                telemetryListener.RegisterHandler("Test.A", ahandler);
                telemetryListener.RegisterHandler("Test.B", bhandler);

                using (var listenerA = new DiagnosticListener("Test.A"))
                    using (var listenerB = new DiagnosticListener("Test.B"))
                    {
                        this.DoOperation(listenerA, "Send");
                        this.DoOperation(listenerA, "Receive");
                        this.DoOperation(listenerB, "Any");
                    }

                Assert.AreEqual(1, ahandler.EventCalls.Count);

                var testASend = ahandler.EventCalls[0];
                Assert.AreEqual("Test.A", testASend.Item1);
                Assert.AreEqual("Send.Stop", testASend.Item2.Key);
                Assert.IsNull(testASend.Item2.Value);

                Assert.AreEqual(1, bhandler.EventCalls.Count);
                var testBAny = bhandler.EventCalls[0];
                Assert.AreEqual("Test.B", testBAny.Item1);
                Assert.AreEqual("Any.Stop", testBAny.Item2.Key);
                Assert.IsNull(testBAny.Item2.Value);
            }
        }
Exemplo n.º 5
0
        public void TelemetryDiagnosticSourceListenerCapturesAllActivitiesByDefault()
        {
            var inclusionList = new[] { "Test.A" }.ToList();
            using (var dl = new TelemetryDiagnosticSourceListener(this.configuration, inclusionList))
            {
                dl.Subscribe();
                DiagnosticListener listener = new DiagnosticListener("Test.A");
                Activity activity = new Activity("Test.A.Client.Monitoring");

                Assert.IsTrue(listener.IsEnabled(), "There is a subscriber for a new diagnostic source");
                Assert.IsTrue(listener.IsEnabled(activity.OperationName), "There is a subscriber for a new activity");
                Assert.IsTrue(
                    listener.IsEnabled(
                        activity.OperationName + TelemetryDiagnosticSourceListener.ActivityStopNameSuffix),
                    "There is a subscriber for new activity Stop event");
                Assert.IsFalse(
                    listener.IsEnabled(activity.OperationName +
                                       TelemetryDiagnosticSourceListener.ActivityStartNameSuffix),
                    "There are no subscribers for new activity Start event");

                int sentCountBefore = this.sentItems.Count;

                listener.StartActivity(activity, null);
                Assert.AreEqual(sentCountBefore, this.sentItems.Count, "No telemetry item should be sent on activity start");

                listener.StopActivity(activity, null);
                Assert.AreEqual(sentCountBefore + 1, this.sentItems.Count, "One new telemetry item should be sent on activity stop");

                DependencyTelemetry telemetryItem = this.sentItems.Last() as DependencyTelemetry;
                Assert.IsNotNull(telemetryItem, "Dependency telemetry item should be sent");
                Assert.AreEqual(activity.OperationName, telemetryItem.Name);
            }
        }
        /// <summary>
        /// Initialize method is called after all configuration properties have been loaded from the configuration.
        /// </summary>
        public void Initialize(TelemetryConfiguration configuration)
        {
            DependencyCollectorEventSource.Log.RemoteDependencyModuleVerbose("Initializing DependencyTrackingModule");

            // Temporary fix to make sure that we initialize module once.
            // It should be removed when configuration reading logic is moved to Web SDK.
            if (!this.isInitialized)
            {
                lock (this.lockObject)
                {
                    if (!this.isInitialized)
                    {
                        try
                        {
                            this.telemetryConfiguration = configuration;

#if !NETCORE
                            // Net40 only supports runtime instrumentation
                            // Net45 supports either but not both to avoid duplication
                            this.InitializeForRuntimeInstrumentationOrFramework();
#endif

                            // NET45 referencing .net core System.Net.Http supports diagnostic listener
                            this.httpCoreDiagnosticSourceListener = new HttpCoreDiagnosticSourceListener(
                                configuration,
                                this.EffectiveProfileQueryEndpoint,
                                this.SetComponentCorrelationHttpHeaders,
                                this.ExcludeComponentCorrelationHttpHeadersOnDomains,
                                null);

                            if (this.IncludeDiagnosticSourceActivities != null && this.IncludeDiagnosticSourceActivities.Count > 0)
                            {
                                this.telemetryDiagnosticSourceListener = new TelemetryDiagnosticSourceListener(configuration, this.IncludeDiagnosticSourceActivities);
                                this.telemetryDiagnosticSourceListener.RegisterHandler(EventHubsDiagnosticsEventHandler.DiagnosticSourceName, new EventHubsDiagnosticsEventHandler(configuration));
                                this.telemetryDiagnosticSourceListener.RegisterHandler(ServiceBusDiagnosticsEventHandler.DiagnosticSourceName, new ServiceBusDiagnosticsEventHandler(configuration));
                            }

                            this.sqlClientDiagnosticSourceListener = new SqlClientDiagnosticSourceListener(configuration);

                            DependencyCollectorEventSource.Log.RemoteDependencyModuleVerbose("Initializing DependencyTrackingModule completed successfully.");
                        }
                        catch (Exception exc)
                        {
                            string clrVersion;
#if NETCORE
                            clrVersion = System.Reflection.Assembly.GetEntryAssembly().GetCustomAttribute <TargetFrameworkAttribute>().FrameworkName;
#else
                            clrVersion = Environment.Version.ToString();
#endif
                            DependencyCollectorEventSource.Log.RemoteDependencyModuleError(exc.ToInvariantString(), clrVersion);
                        }

                        this.PrepareActivity();

                        this.isInitialized = true;
                    }
                }
            }
        }
Exemplo n.º 7
0
 public void TelemetryDiagnosticSourceListenerOnCreatedListener()
 {
     DiagnosticListener listener = new DiagnosticListener("Test.A");
     var inclusionList = new[] { "Test.A" }.ToList();
     using (var dl = new TelemetryDiagnosticSourceListener(this.configuration, inclusionList))
     {
         dl.Subscribe();
         Assert.IsTrue(listener.IsEnabled(), "There is a subscriber for a new diagnostic source");
     }
 }
        public void TelemetryDiagnosticSourceListenerIgnoresNotIncludedActivities()
        {
            var inclusionList = new[] { "Test.A:Test.A.Client.Monitoring" }.ToList();

            using (var listener = new DiagnosticListener("Test.A"))
                using (var dl = new TelemetryDiagnosticSourceListener(this.configuration, inclusionList))
                {
                    dl.Subscribe();

                    // Diagnostic Source is not ignored
                    Assert.IsTrue(listener.IsEnabled(), "There is a subscriber for diagnostic source");

                    // Activity1 is ignored per exclusion
                    Activity activity1 = new Activity("Test.A.Activity1");
                    Assert.IsFalse(listener.IsEnabled(activity1.OperationName), "There are no subscribers for activity 1");

                    int sentCountBefore = this.sentItems.Count;

                    listener.StartActivity(activity1, null);
                    Assert.AreEqual(sentCountBefore, this.sentItems.Count, "No telemetry item should be sent on activity 1 start");

                    listener.StopActivity(activity1, null);
                    Assert.AreEqual(sentCountBefore, this.sentItems.Count, "No telemetry item should be sent on activity 1 stop");

                    // Activity2 is ignored per exclusion
                    Activity activity2 = new Activity("Test.A.Activity2");
                    Assert.IsFalse(listener.IsEnabled(activity2.OperationName), "There are no subscribers for activity 2");

                    listener.StartActivity(activity2, null);
                    Assert.AreEqual(sentCountBefore, this.sentItems.Count, "No telemetry item should be sent on activity 2 start");

                    listener.StopActivity(activity2, null);
                    Assert.AreEqual(sentCountBefore, this.sentItems.Count, "No telemetry item should be sent on activity 2 stop");

                    // non-excluded activity from same diagnostic source is captured
                    Activity activity = new Activity("Test.A.Client.Monitoring");
                    Assert.IsTrue(listener.IsEnabled(activity.OperationName), "There is a subscriber for activity");

                    listener.StartActivity(activity, null);
                    Assert.AreEqual(sentCountBefore, this.sentItems.Count, "No telemetry item should be sent on activity start");

                    listener.StopActivity(activity, null);
                    Assert.AreEqual(sentCountBefore + 1, this.sentItems.Count, "One new telemetry item should be sent on activity stop");

                    DependencyTelemetry telemetryItem = this.sentItems.Last() as DependencyTelemetry;
                    Assert.IsNotNull(telemetryItem, "Dependency telemetry item should be sent");
                    Assert.AreEqual(telemetryItem.Name, activity.OperationName);
                }
        }
        public void TelemetryDiagnosticSourceListenerIgnoresNotIncludedSources()
        {
            var inclusionList = new[] { "Test.B" }.ToList();

            using (var listenerA = new DiagnosticListener("Test.A"))
                using (var dl = new TelemetryDiagnosticSourceListener(this.configuration, inclusionList))
                {
                    dl.Subscribe();
                    // Diagnostic Source A is ignored
                    Activity activityA = new Activity("Test.A.Client.Monitoring");

                    Assert.IsFalse(listenerA.IsEnabled(), "There are no subscribers for excluded diagnostic source A");
                    Assert.IsFalse(listenerA.IsEnabled(activityA.OperationName), "There are no subscribers for activity A");

                    int sentCountBefore = this.sentItems.Count;

                    listenerA.StartActivity(activityA, null);
                    Assert.AreEqual(sentCountBefore, this.sentItems.Count, "No telemetry item should be sent on activity start");

                    listenerA.StopActivity(activityA, null);
                    Assert.AreEqual(sentCountBefore, this.sentItems.Count, "No telemetry item should be sent on activity stop");

                    // Diagnostic Source B is still captured
                    using (var listenerB = new DiagnosticListener("Test.B"))
                    {
                        Activity activityB = new Activity("Test.B.Client.Monitoring");

                        Assert.IsTrue(listenerB.IsEnabled(), "There is a subscriber for diagnostic source B");
                        Assert.IsTrue(listenerB.IsEnabled(activityB.OperationName), "There is a subscriber for activity B");

                        listenerB.StartActivity(activityB, null);
                        Assert.AreEqual(sentCountBefore, this.sentItems.Count,
                                        "No telemetry item should be sent on activity start");

                        listenerB.StopActivity(activityB, null);
                        Assert.AreEqual(sentCountBefore + 1, this.sentItems.Count,
                                        "One new telemetry item should be sent on activity B stop");

                        DependencyTelemetry telemetryItem = this.sentItems.Last() as DependencyTelemetry;
                        Assert.IsNotNull(telemetryItem, "Dependency telemetry item should be sent");
                        Assert.AreEqual(telemetryItem.Name, activityB.OperationName);
                    }
                }
        }
        public void TelemetryDiagnosticSourceListenerCollectsTelemetryFromRawActivityWithoutParent()
        {
            var inclusionList = new List <string> {
                "Test.A"
            };

            using (var listener = new DiagnosticListener("Test.A"))
                using (var dl = new TelemetryDiagnosticSourceListener(this.configuration, inclusionList))
                {
                    dl.Subscribe();

                    var activity = new Activity("Test.A.Activity1");

                    listener.StartActivity(activity, null);
                    listener.StopActivity(activity, null);

                    var telemetryItem = this.sentItems.Last() as DependencyTelemetry;

                    Assert.AreEqual(activity.SpanId.ToHexString(), telemetryItem.Id);
                    Assert.AreEqual(activity.TraceId.ToHexString(), telemetryItem.Context.Operation.Id);
                    Assert.AreEqual(null, telemetryItem.Context.Operation.ParentId);
                }
        }