Initialize() public method

Initialize method is called after all configuration properties have been loaded from the configuration.
public Initialize ( Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration configuration ) : void
configuration Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration
return void
        public void DependencyTrackingTelemetryModuleIsNotInitializedTwiceToPreventProfilerAttachFailure()
        {
            var module = new DependencyTrackingTelemetryModule();
            PrivateObject privateObject = new PrivateObject(module);

            module.Initialize(TelemetryConfiguration.CreateDefault());
            object config1 = privateObject.GetField("telemetryConfiguration");

            module.Initialize(TelemetryConfiguration.CreateDefault());
            object config2 = privateObject.GetField("telemetryConfiguration");

            Assert.AreSame(config1, config2);
        }
        public void TestHttpRequestsWithQueryStringAreCollected()
        {
            ITelemetry sentTelemetry = null;
            var channel = new StubTelemetryChannel { OnSend = telemetry => sentTelemetry = telemetry };
            var config = new TelemetryConfiguration
            {
                InstrumentationKey = IKey,
                TelemetryChannel = channel
            };

            using (var module = new DependencyTrackingTelemetryModule())
            {
                module.Initialize(config);
                const string Url = "http://www.bing.com/search?q=1";
                new HttpWebRequestUtils().ExecuteAsyncHttpRequest(Url, HttpMethod.Get);

                while (sentTelemetry == null)
                {
                    Thread.Sleep(100);
                }

                Assert.IsNotNull(sentTelemetry, "Get requests are not monitored with RDD Event Source.");
                var item = (DependencyTelemetry)sentTelemetry;
                Assert.AreEqual(Url, item.Name, "Reported Url must be " + Url);
                Assert.IsTrue(item.Duration > TimeSpan.FromMilliseconds(0), "Duration has to be positive");
                Assert.AreEqual("Http", item.DependencyKind, "HttpAny has to be dependency kind as it includes http and azure calls");
                Assert.IsTrue(
                    DateTime.UtcNow.Subtract(item.Timestamp.UtcDateTime).TotalMilliseconds < TimeSpan.FromMinutes(1).TotalMilliseconds, "timestamp < now");
                Assert.IsTrue(
                    item.Timestamp.Subtract(DateTime.UtcNow).TotalMilliseconds > -TimeSpan.FromMinutes(1).TotalMilliseconds, "now - 1 min < timestamp");
            }
        }
            private static void ConfigureApplicationInsights(IAppBuilder app)
            {
                Channel = new MockTelemetryChannel();

                var telemetryConfig = new TelemetryConfigurationBuilder()
                    .WithChannel(Channel)
                    .WithTelemetryInitializer(new OperationIdTelemetryInitializer())
                    .Build();

                DependencyTrackingTelemetryModule = new DependencyTrackingTelemetryModule();
                DependencyTrackingTelemetryModule.Initialize(telemetryConfig);
                app.UseApplicationInsights(telemetryConfiguration: telemetryConfig);
            }
Ejemplo n.º 4
0
        public static void ConfigureTelemetryModules(this TelemetryConfiguration configuration)
        {
            var performanceCounters = new PerformanceCollectorModule();
            performanceCounters.Counters.Add(new PerformanceCounterCollectionRequest(@"\Process\ID", "ID"));
            performanceCounters.Initialize(configuration);

            telemetryModules.Add(performanceCounters);

            var dependencies = new DependencyTrackingTelemetryModule();
            dependencies.Initialize(configuration);
           
            telemetryModules.Add(dependencies);
        }
            private static void ConfigureApplicationInsights(IAppBuilder app)
            {
                Channel = new MockTelemetryChannel();

                var config = new TelemetryConfigurationBuilder()
                    .WithChannel(Channel)
                    .WithTelemetryInitializer(new OperationIdTelemetryInitializer())
                    .Build();

                DependencyTrackingTelemetryModule = new DependencyTrackingTelemetryModule();
                DependencyTrackingTelemetryModule.Initialize(config);

                app.UseApplicationInsights(
                    middlewareConfiguration:
                        new OperationIdContextMiddlewareConfiguration {ShouldTryGetIdFromHeader = true},
                    telemetryConfiguration: config);
            }
        public void EventHubsSuccessfulReceiveShortNameIsHandled()
        {
            using (var listener = new DiagnosticListener("Microsoft.Azure.EventHubs"))
                using (var module = new DependencyTrackingTelemetryModule())
                {
                    module.IncludeDiagnosticSourceActivities.Add("Microsoft.Azure.EventHubs");
                    module.Initialize(this.configuration);

                    Activity sendActivity   = null;
                    Activity parentActivity = new Activity("parent").AddBaggage("k1", "v1").Start();
                    parentActivity.TraceStateString = "state=some";
                    var telemetry = this.TrackOperation <DependencyTelemetry>(
                        listener,
                        "Receive",
                        TaskStatus.RanToCompletion,
                        null,
                        () => sendActivity = Activity.Current);

                    Assert.IsNotNull(telemetry);
                    Assert.AreEqual("Receive", telemetry.Name);
                    Assert.AreEqual(RemoteDependencyConstants.AzureEventHubs, telemetry.Type);
                    Assert.AreEqual("sb://eventhubname.servicebus.windows.net/ | ehname", telemetry.Target);
                    Assert.IsTrue(telemetry.Success.Value);

                    Assert.AreEqual(sendActivity.ParentSpanId.ToHexString(), telemetry.Context.Operation.ParentId);
                    Assert.AreEqual(sendActivity.TraceId.ToHexString(), telemetry.Context.Operation.Id);
                    Assert.AreEqual(sendActivity.SpanId.ToHexString(), telemetry.Id);

                    Assert.AreEqual("v1", telemetry.Properties["k1"]);
                    Assert.AreEqual("eventhubname.servicebus.windows.net", telemetry.Properties["peer.hostname"]);
                    Assert.AreEqual("ehname", telemetry.Properties["eh.event_hub_name"]);
                    Assert.AreEqual("SomePartitionKeyHere", telemetry.Properties["eh.partition_key"]);
                    Assert.AreEqual("EventHubClient1(ehname)", telemetry.Properties["eh.client_id"]);

                    Assert.IsTrue(telemetry.Properties.TryGetValue("tracestate", out var tracestate));
                    Assert.AreEqual("state=some", tracestate);
                }
        }
Ejemplo n.º 7
0
        public void TestDependencyCollectionCanceledRequestDiagnosticSource()
        {
            using (var module = new DependencyTrackingTelemetryModule())
            {
                module.ProfileQueryEndpoint = FakeProfileApiEndpoint;
                module.Initialize(this.config);

                var            url     = new Uri("https://bing.com/");
                HttpWebRequest request = WebRequest.CreateHttp(url);
                request.Timeout = 5;
                try
                {
                    using (request.GetResponse())
                    {
                    }
                }
                catch (WebException)
                {
                }

                this.ValidateTelemetryForDiagnosticSource(this.sentTelemetry.Single(), url, request, false, string.Empty);
            }
        }
Ejemplo n.º 8
0
        public void ServiceBusProcessHanding()
        {
            using (var module = new DependencyTrackingTelemetryModule())
            {
                module.IncludeDiagnosticSourceActivities.Add("Microsoft.Azure.ServiceBus");
                module.Initialize(this.configuration);

                DiagnosticListener listener = new DiagnosticListener("Microsoft.Azure.ServiceBus");

                Activity parentActivity = new Activity("parent").AddBaggage("k1", "v1").Start();
                var      telemetry      = this.TrackOperation <RequestTelemetry>(listener, "Microsoft.Azure.ServiceBus.Process", TaskStatus.RanToCompletion);

                Assert.IsNotNull(telemetry);
                Assert.AreEqual("Process", telemetry.Name);
                Assert.AreEqual($"type:{RemoteDependencyConstants.AzureServiceBus} | name:queueName | endpoint:sb://queuename.myservicebus.com/", telemetry.Source);
                Assert.IsTrue(telemetry.Success.Value);

                Assert.AreEqual(parentActivity.Id, telemetry.Context.Operation.ParentId);
                Assert.AreEqual(parentActivity.RootId, telemetry.Context.Operation.Id);
                Assert.AreEqual("v1", telemetry.Properties["k1"]);
                Assert.AreEqual("messageId", telemetry.Properties["MessageId"]);
            }
        }
Ejemplo n.º 9
0
        public void ServiceBusProcessHandingExternalParent()
        {
            var tc = new TelemetryClient(this.configuration);

            void TrackTraceDuringProcessing() => tc.TrackTrace("trace");

            using (var listener = new DiagnosticListener("Microsoft.Azure.ServiceBus"))
                using (var module = new DependencyTrackingTelemetryModule())
                {
                    module.IncludeDiagnosticSourceActivities.Add("Microsoft.Azure.ServiceBus");
                    module.Initialize(this.configuration);

                    var requestTelemetry = this.TrackOperation <RequestTelemetry>(
                        listener,
                        "Microsoft.Azure.ServiceBus.Process",
                        TaskStatus.RanToCompletion,
                        "parent",
                        TrackTraceDuringProcessing);

                    Assert.IsNotNull(requestTelemetry);
                    Assert.AreEqual("Process", requestTelemetry.Name);
                    Assert.AreEqual(
                        $"type:{RemoteDependencyConstants.AzureServiceBus} | name:queueName | endpoint:sb://queuename.myservicebus.com/",
                        requestTelemetry.Source);
                    Assert.IsTrue(requestTelemetry.Success.Value);

                    Assert.AreEqual("parent", requestTelemetry.Context.Operation.ParentId);
                    Assert.AreEqual("parent", requestTelemetry.Context.Operation.Id);
                    Assert.AreEqual("messageId", requestTelemetry.Properties["MessageId"]);

                    var traceTelemetry = this.sentItems.OfType <TraceTelemetry>();
                    Assert.AreEqual(1, traceTelemetry.Count());

                    Assert.AreEqual(requestTelemetry.Context.Operation.Id, traceTelemetry.Single().Context.Operation.Id);
                    Assert.AreEqual(requestTelemetry.Id, traceTelemetry.Single().Context.Operation.ParentId);
                }
        }
Ejemplo n.º 10
0
        public void TestHttpPostRequestsAreCollected()
        {
            ITelemetry sentTelemetry = null;
            var        channel       = new StubTelemetryChannel {
                OnSend = telemetry => sentTelemetry = telemetry
            };

            var config = new TelemetryConfiguration
            {
                InstrumentationKey = IKey,
                TelemetryChannel   = channel
            };

            using (var module = new DependencyTrackingTelemetryModule())
            {
                module.Initialize(config);
                Uri url = new Uri("http://www.bing.com");
                new HttpWebRequestUtils().ExecuteAsyncHttpRequest(url.ToString(), HttpMethod.Post);

                while (sentTelemetry == null)
                {
                    Thread.Sleep(100);
                }

                Assert.IsNotNull(sentTelemetry, "Get requests are not monitored with RDD Event Source.");
                var item = (DependencyTelemetry)sentTelemetry;
                Assert.AreEqual(url, item.Data);
                Assert.AreEqual(url.Host, item.Target);
                Assert.AreEqual(url.AbsolutePath, item.Name);
                Assert.IsTrue(item.Duration > TimeSpan.FromMilliseconds(0), "Duration has to be positive");
                Assert.AreEqual(RemoteDependencyConstants.HTTP, item.Type, "HttpAny has to be dependency kind as it includes http and azure calls");
                Assert.IsTrue(
                    DateTime.UtcNow.Subtract(item.Timestamp.UtcDateTime).TotalMilliseconds < TimeSpan.FromMinutes(1).TotalMilliseconds, "timestamp < now");
                Assert.IsTrue(
                    item.Timestamp.Subtract(DateTime.UtcNow).TotalMilliseconds > -TimeSpan.FromMinutes(1).TotalMilliseconds, "now - 1 min < timestamp");
            }
        }
        public void AzureClientSpansAreCollectedForHttp()
        {
            using (var listener = new DiagnosticListener("Azure.SomeClient"))
                using (var module = new DependencyTrackingTelemetryModule())
                {
                    module.Initialize(this.configuration);

                    Activity httpActivity = new Activity("Azure.SomeClient.Http.Request")
                                            .AddTag("http.method", "PATCH")
                                            .AddTag("http.url", "http://host:8080/path?query#fragment")
                                            .AddTag("requestId", "client-request-id");

                    var payload = new HttpRequestMessage();
                    listener.StartActivity(httpActivity, payload);
                    httpActivity
                    .AddTag("http.status_code", "206")
                    .AddTag("serviceRequestId", "service-request-id");

                    listener.StopActivity(httpActivity, payload);

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

                    Assert.IsNotNull(telemetry);
                    Assert.AreEqual("PATCH /path", telemetry.Name);
                    Assert.AreEqual("host:8080", telemetry.Target);
                    Assert.AreEqual("http://host:8080/path?query#fragment", telemetry.Data);
                    Assert.AreEqual("206", telemetry.ResultCode);
                    Assert.AreEqual("Http", telemetry.Type);
                    Assert.IsTrue(telemetry.Success.Value);
                    Assert.AreEqual("client-request-id", telemetry.Properties["ClientRequestId"]);
                    Assert.AreEqual("service-request-id", telemetry.Properties["ServerRequestId"]);

                    Assert.IsNull(telemetry.Context.Operation.ParentId);
                    Assert.AreEqual(httpActivity.TraceId.ToHexString(), telemetry.Context.Operation.Id);
                    Assert.AreEqual(httpActivity.SpanId.ToHexString(), telemetry.Id);
                }
        }
        public void EventHubsSuccessfulSendIsHandledW3COff()
        {
            Activity.DefaultIdFormat      = ActivityIdFormat.Hierarchical;
            Activity.ForceDefaultIdFormat = true;
            using (var listener = new DiagnosticListener("Microsoft.Azure.EventHubs"))
                using (var module = new DependencyTrackingTelemetryModule())
                {
                    module.IncludeDiagnosticSourceActivities.Add("Microsoft.Azure.EventHubs");
                    module.Initialize(this.configuration);

                    Activity sendActivity   = null;
                    Activity parentActivity = new Activity("parent").AddBaggage("k1", "v1").Start();
                    var      telemetry      = this.TrackOperation <DependencyTelemetry>(
                        listener,
                        "Microsoft.Azure.EventHubs.Send",
                        TaskStatus.RanToCompletion,
                        null,
                        () => sendActivity = Activity.Current);

                    Assert.IsNotNull(telemetry);
                    Assert.AreEqual("Send", telemetry.Name);
                    Assert.AreEqual(RemoteDependencyConstants.AzureEventHubs, telemetry.Type);
                    Assert.AreEqual("sb://eventhubname.servicebus.windows.net/ | ehname", telemetry.Target);
                    Assert.IsTrue(telemetry.Success.Value);

                    Assert.AreEqual(parentActivity.Id, telemetry.Context.Operation.ParentId);
                    Assert.AreEqual(parentActivity.RootId, telemetry.Context.Operation.Id);
                    Assert.AreEqual(sendActivity.Id, telemetry.Id);

                    Assert.AreEqual("v1", telemetry.Properties["k1"]);
                    Assert.AreEqual("eventhubname.servicebus.windows.net", telemetry.Properties["peer.hostname"]);
                    Assert.AreEqual("ehname", telemetry.Properties["eh.event_hub_name"]);
                    Assert.AreEqual("SomePartitionKeyHere", telemetry.Properties["eh.partition_key"]);
                    Assert.AreEqual("EventHubClient1(ehname)", telemetry.Properties["eh.client_id"]);
                }
        }
Ejemplo n.º 13
0
        public void ServiceBusBadStatusHanding()
        {
            using (var module = new DependencyTrackingTelemetryModule())
            {
                module.IncludeDiagnosticSourceActivities.Add("Microsoft.Azure.ServiceBus");
                module.Initialize(this.configuration);

                DiagnosticListener listener = new DiagnosticListener("Microsoft.Azure.ServiceBus");

                Activity parentActivity = new Activity("parent").AddBaggage("k1", "v1").Start();
                var      telemetry      = this.TrackOperation <DependencyTelemetry>(listener, "Microsoft.Azure.ServiceBus.Send", TaskStatus.Faulted);

                Assert.IsNotNull(telemetry);
                Assert.AreEqual("Send", telemetry.Name);
                Assert.AreEqual(RemoteDependencyConstants.AzureServiceBus, telemetry.Type);
                Assert.AreEqual("sb://queuename.myservicebus.com/ | queueName", telemetry.Target);
                Assert.IsFalse(telemetry.Success.Value);

                Assert.AreEqual(parentActivity.Id, telemetry.Context.Operation.ParentId);
                Assert.AreEqual(parentActivity.RootId, telemetry.Context.Operation.Id);
                Assert.AreEqual("v1", telemetry.Properties["k1"]);
                Assert.AreEqual("messageId", telemetry.Properties["MessageId"]);
            }
        }
        public void AzureClientSpansAreCollectedServerKind()
        {
            using (var listener = new DiagnosticListener("Azure.SomeClient"))
                using (var module = new DependencyTrackingTelemetryModule())
                {
                    module.Initialize(this.configuration);

                    Activity sendActivity = new Activity("Azure.SomeClient.Send");
                    sendActivity.AddTag("kind", "server");

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

                    var telemetry = this.sentItems.Last() as RequestTelemetry;

                    Assert.IsNotNull(telemetry);
                    Assert.AreEqual("SomeClient.Send", telemetry.Name);
                    Assert.IsTrue(telemetry.Success.Value);

                    Assert.IsNull(telemetry.Context.Operation.ParentId);
                    Assert.AreEqual(sendActivity.TraceId.ToHexString(), telemetry.Context.Operation.Id);
                    Assert.AreEqual(sendActivity.SpanId.ToHexString(), telemetry.Id);
                }
        }
Ejemplo n.º 15
0
        public void ServiceBusProcessHandingWithoutParent()
        {
            using (var module = new DependencyTrackingTelemetryModule())
            {
                module.IncludeDiagnosticSourceActivities.Add("Microsoft.Azure.ServiceBus");
                module.Initialize(this.configuration);

                DiagnosticListener listener = new DiagnosticListener("Microsoft.Azure.ServiceBus");

                var telemetry = this.TrackOperation <RequestTelemetry>(listener, "Microsoft.Azure.ServiceBus.Process", TaskStatus.RanToCompletion);

                Assert.IsNotNull(telemetry);
                Assert.AreEqual("Process", telemetry.Name);
                Assert.AreEqual($"type:{RemoteDependencyConstants.AzureServiceBus} | name:queueName | endpoint:sb://queuename.myservicebus.com/", telemetry.Source);
                Assert.IsTrue(telemetry.Success.Value);

                // W3C compatible-Id ( should go away when W3C is implemented in .NET https://github.com/dotnet/corefx/issues/30331 TODO)
                Assert.AreEqual(32, telemetry.Context.Operation.Id.Length);
                Assert.IsTrue(Regex.Match(telemetry.Context.Operation.Id, @"[a-z][0-9]").Success);
                // end of workaround test

                Assert.AreEqual("messageId", telemetry.Properties["MessageId"]);
            }
        }
Ejemplo n.º 16
0
        public void TestDependencyCollectionNoParentActivity()
        {
            ITelemetry sentTelemetry = null;

            var channel = new StubTelemetryChannel
            {
                OnSend = telemetry =>
                {
                    // The correlation id lookup service also makes http call, just make sure we skip that
                    DependencyTelemetry depTelemetry = telemetry as DependencyTelemetry;
                    if (depTelemetry != null &&
                        !depTelemetry.Data.StartsWith(FakeProfileApiEndpoint, StringComparison.OrdinalIgnoreCase))
                    {
                        Assert.IsNull(sentTelemetry);
                        sentTelemetry = telemetry;
                    }
                }
            };

            var config = new TelemetryConfiguration
            {
                InstrumentationKey = IKey,
                TelemetryChannel   = channel
            };

            config.TelemetryInitializers.Add(new OperationCorrelationTelemetryInitializer());

            using (var module = new DependencyTrackingTelemetryModule())
            {
                module.ProfileQueryEndpoint = FakeProfileApiEndpoint;
                module.Initialize(config);

                var            url     = new Uri("http://bing.com");
                HttpWebRequest request = WebRequest.CreateHttp(url);
                request.GetResponse();

                Assert.IsNotNull(sentTelemetry);
                var item = (DependencyTelemetry)sentTelemetry;
                Assert.AreEqual(url, item.Data);
                Assert.AreEqual(url.Host, item.Target);
                Assert.AreEqual("GET " + url.AbsolutePath, item.Name);
                Assert.IsTrue(item.Duration > TimeSpan.FromMilliseconds(0), "Duration has to be positive");
                Assert.AreEqual(RemoteDependencyConstants.HTTP, item.Type, "HttpAny has to be dependency kind as it includes http and azure calls");
                Assert.IsTrue(
                    DateTime.UtcNow.Subtract(item.Timestamp.UtcDateTime).TotalMilliseconds <
                    TimeSpan.FromMinutes(1).TotalMilliseconds,
                    "timestamp < now");
                Assert.IsTrue(
                    item.Timestamp.Subtract(DateTime.UtcNow).TotalMilliseconds >
                    -TimeSpan.FromMinutes(1).TotalMilliseconds,
                    "now - 1 min < timestamp");
                Assert.AreEqual("200", item.ResultCode);

                var requestId = item.Id;
                Assert.AreEqual(requestId, request.Headers["Request-Id"]);
                Assert.AreEqual(requestId, request.Headers["x-ms-request-id"]);
                Assert.IsTrue(requestId.StartsWith('|' + item.Context.Operation.Id + '.'));

                Assert.IsNull(request.Headers["Correlation-Context"]);
            }
        }
        public void TestHttpGetRequestsAreCollected()
        {
            ITelemetry sentTelemetry = null;
            var channel = new StubTelemetryChannel { OnSend = telemetry => sentTelemetry = telemetry };
            var config = new TelemetryConfiguration
            {
                InstrumentationKey = IKey,
                TelemetryChannel = channel
            };

            using (var module = new DependencyTrackingTelemetryModule())
            {
                module.Initialize(config);
                Uri url = new Uri("http://www.bing.com/maps");
                new HttpWebRequestUtils().ExecuteAsyncHttpRequest(url.ToString(), HttpMethod.Get);

                while (sentTelemetry == null)
                {
                    Thread.Sleep(100);
                }

                Assert.IsNotNull(sentTelemetry, "Get requests are not monitored with RDD Event Source.");
                var item = (DependencyTelemetry)sentTelemetry;
                Assert.AreEqual(url, item.Data);
                Assert.AreEqual(url.Host, item.Target);
                Assert.AreEqual(url.AbsolutePath, item.Name);
                Assert.IsTrue(item.Duration > TimeSpan.FromMilliseconds(0), "Duration has to be positive");
                Assert.AreEqual(RemoteDependencyConstants.HTTP, item.Type, "HttpAny has to be dependency kind as it includes http and azure calls");
                Assert.IsTrue(
                    DateTime.UtcNow.Subtract(item.Timestamp.UtcDateTime).TotalMilliseconds < TimeSpan.FromMinutes(1).TotalMilliseconds, "timestamp < now");
                Assert.IsTrue(
                    item.Timestamp.Subtract(DateTime.UtcNow).TotalMilliseconds > -TimeSpan.FromMinutes(1).TotalMilliseconds, "now - 1 min < timestamp");
            }
        }