コード例 #1
0
        public static TelemetryConfiguration CreateAITelemetryConfig(out IList <ITelemetry> telemetrySentToChannel)
        {
            StubApplicationInsightsTelemetryChannel channel = new StubApplicationInsightsTelemetryChannel();
            string iKey = Guid.NewGuid().ToString("D");
            TelemetryConfiguration telemetryConfig = new TelemetryConfiguration(iKey, channel);

            var channelBuilder = new TelemetryProcessorChainBuilder(telemetryConfig);

            channelBuilder.Build();

            foreach (ITelemetryProcessor initializer in telemetryConfig.TelemetryInitializers)
            {
                ITelemetryModule m = initializer as ITelemetryModule;
                if (m != null)
                {
                    m.Initialize(telemetryConfig);
                }
            }

            foreach (ITelemetryProcessor processor in telemetryConfig.TelemetryProcessors)
            {
                ITelemetryModule m = processor as ITelemetryModule;
                if (m != null)
                {
                    m.Initialize(telemetryConfig);
                }
            }

            telemetrySentToChannel = channel.TelemetryItems;
            return(telemetryConfig);
        }
        internal static TelemetryConfiguration CreateTelemetryConfigWithExtractor(IList <ITelemetry> telemetrySentToChannel,
                                                                                  Func <ITelemetryProcessor, AutocollectedMetricsExtractor> extractorFactory)
        {
            ITelemetryChannel channel = new StubTelemetryChannel {
                OnSend = (t) => telemetrySentToChannel.Add(t)
            };
            string iKey = Guid.NewGuid().ToString("D");
            TelemetryConfiguration telemetryConfig = new TelemetryConfiguration(iKey, channel);

            var channelBuilder = new TelemetryProcessorChainBuilder(telemetryConfig);

            channelBuilder.Use(extractorFactory);
            channelBuilder.Build();

            TelemetryProcessorChain processors = telemetryConfig.TelemetryProcessorChain;

            foreach (ITelemetryProcessor processor in processors.TelemetryProcessors)
            {
                ITelemetryModule m = processor as ITelemetryModule;
                if (m != null)
                {
                    m.Initialize(telemetryConfig);
                }
            }


            return(telemetryConfig);
        }
コード例 #3
0
        private static TelemetryProcessorChain CreateTelemetryProcessorChainWithSampling(IList <ITelemetry> sentTelemetry, double samplingPercentage, string excludedTypes = null, string includedTypes = null)
        {
            var tc = new TelemetryConfiguration {
                TelemetryChannel = new StubTelemetryChannel()
            };

            tc.InstrumentationKey = Guid.NewGuid().ToString("D");

            var channelBuilder = new TelemetryProcessorChainBuilder(tc);

            channelBuilder.UseSampling(samplingPercentage, excludedTypes, includedTypes);
            channelBuilder.Use(next => new StubTelemetryProcessor(next)
            {
                OnProcess = t => sentTelemetry.Add(t)
            });

            channelBuilder.Build();

            TelemetryProcessorChain processors = tc.TelemetryProcessorChain;

            foreach (ITelemetryProcessor processor in processors.TelemetryProcessors)
            {
                ITelemetryModule m = processor as ITelemetryModule;
                if (m != null)
                {
                    m.Initialize(tc);
                }
            }

            return(processors);
        }
コード例 #4
0
        /// <inheritdoc />
        public void Configure(TelemetryConfiguration configuration)
        {
            if (this.applicationInsightsServiceOptions.InstrumentationKey != null)
            {
                configuration.InstrumentationKey = this.applicationInsightsServiceOptions.InstrumentationKey;
            }

            if (this.telemetryProcessorFactories.Any())
            {
                foreach (ITelemetryProcessorFactory processorFactory in this.telemetryProcessorFactories)
                {
                    configuration.TelemetryProcessorChainBuilder.Use(processorFactory.Create);
                }
                configuration.TelemetryProcessorChainBuilder.Build();
            }

            this.AddTelemetryChannelAndProcessors(configuration);
            (configuration.TelemetryChannel as ITelemetryModule)?.Initialize(configuration);

            configuration.TelemetryProcessorChainBuilder.Build();


            configuration.TelemetryChannel = this.telemetryChannel ?? configuration.TelemetryChannel;

            if (this.applicationInsightsServiceOptions.DeveloperMode != null)
            {
                configuration.TelemetryChannel.DeveloperMode = this.applicationInsightsServiceOptions.DeveloperMode;
            }

            if (this.applicationInsightsServiceOptions.EndpointAddress != null)
            {
                configuration.TelemetryChannel.EndpointAddress = this.applicationInsightsServiceOptions.EndpointAddress;
            }

            foreach (ITelemetryInitializer initializer in this.initializers)
            {
                configuration.TelemetryInitializers.Add(initializer);
            }

            foreach (ITelemetryModule module in this.modules)
            {
                module.Initialize(configuration);
            }

            foreach (ITelemetryProcessor processor in configuration.TelemetryProcessors)
            {
                ITelemetryModule module = processor as ITelemetryModule;
                if (module != null)
                {
                    module.Initialize(configuration);
                }
            }
        }
コード例 #5
0
        private static void RegisterApplicationInsightsTelemetryModule(ITelemetryModule telemetryModule, TelemetryConfiguration configuration)
        {
            var existingModule = TelemetryModules.Instance.Modules.SingleOrDefault(m => m.GetType().Equals(telemetryModule.GetType()));

            if (existingModule != null)
            {
                TelemetryModules.Instance.Modules.Remove(existingModule);
            }

            telemetryModule.Initialize(configuration);

            TelemetryModules.Instance.Modules.Add(telemetryModule);
        }
コード例 #6
0
        internal TelemetryConfiguration InitializeConfiguration()
        {
            TelemetryConfiguration config = new TelemetryConfiguration()
            {
                InstrumentationKey = _instrumentationKey
            };

            AddInitializers(config);

            // Plug in Live stream and adaptive sampling
            QuickPulseTelemetryProcessor   processor = null;
            TelemetryProcessorChainBuilder builder   = config.TelemetryProcessorChainBuilder
                                                       .Use((next) =>
            {
                processor = new QuickPulseTelemetryProcessor(next);
                return(processor);
            });

            if (_samplingSettings != null)
            {
                builder.Use((next) =>
                {
                    return(new AdaptiveSamplingTelemetryProcessor(_samplingSettings, null, next));
                });
            }

            builder.Build();

            _quickPulseModule = new QuickPulseTelemetryModule();
            _quickPulseModule.Initialize(config);
            _quickPulseModule.RegisterTelemetryProcessor(processor);

            // Plug in perf counters
            _perfModule = new PerformanceCollectorModule();
            _perfModule.Initialize(config);

            // Configure the TelemetryChannel
            ITelemetryChannel channel = CreateTelemetryChannel();

            // call Initialize if available
            ITelemetryModule module = channel as ITelemetryModule;

            if (module != null)
            {
                module.Initialize(config);
            }

            config.TelemetryChannel = channel;

            return(config);
        }
        /// <inheritdoc />
        public void Configure(TelemetryConfiguration configuration)
        {
            try
            {
                if (this.applicationInsightsServiceOptions.InstrumentationKey != null)
                {
                    configuration.InstrumentationKey = this.applicationInsightsServiceOptions.InstrumentationKey;
                }

                if (this.telemetryModuleConfigurators.Any())
                {
                    foreach (ITelemetryModuleConfigurator telemetryModuleConfigurator in this.telemetryModuleConfigurators)
                    {
                        ITelemetryModule telemetryModule = this.modules.FirstOrDefault(((module) => module.GetType() == telemetryModuleConfigurator.TelemetryModuleType));
                        if (telemetryModule != null)
                        {
                            telemetryModuleConfigurator.Configure(telemetryModule, this.applicationInsightsServiceOptions);
                        }
                        else
                        {
                            AspNetCoreEventSource.Instance.UnableToFindModuleToConfigure(telemetryModuleConfigurator.TelemetryModuleType.ToString());
                        }
                    }
                }

                if (this.telemetryProcessorFactories.Any())
                {
                    foreach (ITelemetryProcessorFactory processorFactory in this.telemetryProcessorFactories)
                    {
                        configuration.DefaultTelemetrySink.TelemetryProcessorChainBuilder.Use(processorFactory.Create);
                    }
                }

                // Fallback to default channel (InMemoryChannel) created by base sdk if no channel is found in DI
                configuration.TelemetryChannel = this.telemetryChannel ?? configuration.TelemetryChannel;
                (configuration.TelemetryChannel as ITelemetryModule)?.Initialize(configuration);

                this.AddAutoCollectedMetricExtractor(configuration);
                this.AddQuickPulse(configuration);
                this.AddSampling(configuration);
                this.DisableHeartBeatIfConfigured();

                if (applicationInsightsServiceOptions.RequestCollectionOptions.EnableW3CDistributedTracing)
                {
                    this.EnableW3CHeaders(configuration);
                }

                configuration.DefaultTelemetrySink.TelemetryProcessorChainBuilder.Build();
                configuration.TelemetryProcessorChainBuilder.Build();

                if (this.applicationInsightsServiceOptions.DeveloperMode != null)
                {
                    configuration.TelemetryChannel.DeveloperMode = this.applicationInsightsServiceOptions.DeveloperMode;
                }

                if (this.applicationInsightsServiceOptions.EndpointAddress != null)
                {
                    configuration.TelemetryChannel.EndpointAddress = this.applicationInsightsServiceOptions.EndpointAddress;
                }

                foreach (ITelemetryInitializer initializer in this.initializers)
                {
                    configuration.TelemetryInitializers.Add(initializer);
                }

                foreach (ITelemetryModule module in this.modules)
                {
                    module.Initialize(configuration);
                }

                foreach (ITelemetryProcessor processor in configuration.TelemetryProcessors)
                {
                    ITelemetryModule module = processor as ITelemetryModule;
                    if (module != null)
                    {
                        module.Initialize(configuration);
                    }
                }

                // Microsoft.ApplicationInsights.DependencyCollector.DependencyTrackingTelemetryModule depends on this nullable configuration to support Correlation.
                configuration.ApplicationIdProvider = this.applicationIdProvider;
            }
            catch (Exception ex)
            {
                AspNetCoreEventSource.Instance.TelemetryConfigurationSetupFailure(ex.Message);
            }
        }
        /// <inheritdoc />
        public void Configure(TelemetryConfiguration configuration)
        {
            try
            {
                if (this.applicationInsightsServiceOptions.InstrumentationKey != null)
                {
                    configuration.InstrumentationKey = this.applicationInsightsServiceOptions.InstrumentationKey;
                }

                if (this.telemetryModuleConfigurators.Any())
                {
                    foreach (ITelemetryModuleConfigurator telemetryModuleConfigurator in this.telemetryModuleConfigurators)
                    {
                        ITelemetryModule telemetryModule = this.modules.FirstOrDefault((module) => module.GetType() == telemetryModuleConfigurator.TelemetryModuleType);
                        if (telemetryModule != null)
                        {
                            telemetryModuleConfigurator.Configure(telemetryModule, this.applicationInsightsServiceOptions);
                        }
                        else
                        {
#if AI_ASPNETCORE_WEB
                            AspNetCoreEventSource.Instance.UnableToFindModuleToConfigure(telemetryModuleConfigurator.TelemetryModuleType.ToString());
#else
                            WorkerServiceEventSource.Instance.UnableToFindModuleToConfigure(telemetryModuleConfigurator.TelemetryModuleType.ToString());
#endif
                        }
                    }
                }

                if (this.telemetryProcessorFactories.Any())
                {
                    foreach (ITelemetryProcessorFactory processorFactory in this.telemetryProcessorFactories)
                    {
                        configuration.DefaultTelemetrySink.TelemetryProcessorChainBuilder.Use(processorFactory.Create);
                    }
                }

                // Fallback to default channel (InMemoryChannel) created by base sdk if no channel is found in DI
                configuration.TelemetryChannel = this.telemetryChannel ?? configuration.TelemetryChannel;
                (configuration.TelemetryChannel as ITelemetryModule)?.Initialize(configuration);

                this.AddAutoCollectedMetricExtractor(configuration);
                this.AddQuickPulse(configuration);
                this.AddSampling(configuration);
                this.DisableHeartBeatIfConfigured();

                configuration.DefaultTelemetrySink.TelemetryProcessorChainBuilder.Build();
                configuration.TelemetryProcessorChainBuilder.Build();

                if (this.applicationInsightsServiceOptions.DeveloperMode != null)
                {
                    configuration.TelemetryChannel.DeveloperMode = this.applicationInsightsServiceOptions.DeveloperMode;
                }

                if (this.applicationInsightsServiceOptions.EndpointAddress != null)
                {
                    configuration.TelemetryChannel.EndpointAddress = this.applicationInsightsServiceOptions.EndpointAddress;
                }

                // Need to set connection string before calling Initialize() on the Modules and Processors.
                if (this.applicationInsightsServiceOptions.ConnectionString != null)
                {
                    configuration.ConnectionString = this.applicationInsightsServiceOptions.ConnectionString;
                }

                foreach (ITelemetryInitializer initializer in this.initializers)
                {
                    configuration.TelemetryInitializers.Add(initializer);
                }

                // Find the DiagnosticsTelemetryModule, this is needed to initialize AzureInstanceMetadataTelemetryModule and AppServicesHeartbeatTelemetryModule.
                DiagnosticsTelemetryModule diagModule =
                    (this.applicationInsightsServiceOptions.EnableDiagnosticsTelemetryModule && this.applicationInsightsServiceOptions.EnableHeartbeat)
                    ? this.modules.OfType <DiagnosticsTelemetryModule>().FirstOrDefault()
                    : null;

                // Checks ApplicationInsightsServiceOptions and disable TelemetryModules if explicitly disabled.
                foreach (ITelemetryModule module in this.modules)
                {
                    // If any of the modules are disabled explicitly using aioptions,
                    // do not initialize them and dispose if disposable.
                    // The option of not adding a module to DI if disabled
                    // cannot be done to maintain backward compatibility.
                    // So this approach of adding all modules to DI, but selectively
                    // disable those modules which user has disabled is chosen.

                    if (module is DiagnosticsTelemetryModule)
                    {
                        if (!this.applicationInsightsServiceOptions.EnableDiagnosticsTelemetryModule)
                        {
                            DisposeIfDisposable(module);
                            continue;
                        }
                    }

                    // DependencyTrackingTelemetryModule
                    if (module is DependencyTrackingTelemetryModule)
                    {
                        if (!this.applicationInsightsServiceOptions.EnableDependencyTrackingTelemetryModule)
                        {
                            DisposeIfDisposable(module);
                            continue;
                        }
                    }

#if AI_ASPNETCORE_WEB
                    // RequestTrackingTelemetryModule
                    if (module is RequestTrackingTelemetryModule)
                    {
                        if (!this.applicationInsightsServiceOptions.EnableRequestTrackingTelemetryModule)
                        {
                            DisposeIfDisposable(module);
                            continue;
                        }
                    }
#endif

#if NETSTANDARD2_0
                    // EventCounterCollectionModule
                    if (module is EventCounterCollectionModule)
                    {
                        if (!this.applicationInsightsServiceOptions.EnableEventCounterCollectionModule)
                        {
                            DisposeIfDisposable(module);
                            continue;
                        }
                    }
#endif

                    // PerformanceCollectorModule
                    if (module is PerformanceCollectorModule)
                    {
                        if (!this.applicationInsightsServiceOptions.EnablePerformanceCounterCollectionModule)
                        {
                            DisposeIfDisposable(module);
                            continue;
                        }
                    }

                    // AppServicesHeartbeatTelemetryModule
                    if (module is AppServicesHeartbeatTelemetryModule appServicesHeartbeatTelemetryModule)
                    {
                        if (!this.applicationInsightsServiceOptions.EnableAppServicesHeartbeatTelemetryModule)
                        {
                            DisposeIfDisposable(module);
                            continue;
                        }
                        else if (diagModule != null)
                        {
                            // diagModule is set to Null above if (applicationInsightsServiceOptions.EnableDiagnosticsTelemetryModule || this.applicationInsightsServiceOptions.EnableHeartbeat) == false.
                            appServicesHeartbeatTelemetryModule.HeartbeatPropertyManager = diagModule;
                        }
                    }

                    // AzureInstanceMetadataTelemetryModule
                    if (module is AzureInstanceMetadataTelemetryModule azureInstanceMetadataTelemetryModule)
                    {
                        if (!this.applicationInsightsServiceOptions.EnableAzureInstanceMetadataTelemetryModule)
                        {
                            DisposeIfDisposable(module);
                            continue;
                        }
                        else if (diagModule != null)
                        {
                            // diagModule is set to Null above if (applicationInsightsServiceOptions.EnableDiagnosticsTelemetryModule || this.applicationInsightsServiceOptions.EnableHeartbeat) == false.
                            azureInstanceMetadataTelemetryModule.HeartbeatPropertyManager = diagModule;
                        }
                    }

                    // QuickPulseTelemetryModule
                    if (module is QuickPulseTelemetryModule)
                    {
                        if (!this.applicationInsightsServiceOptions.EnableQuickPulseMetricStream)
                        {
                            DisposeIfDisposable(module);
                            continue;
                        }
                    }

                    try
                    {
                        module.Initialize(configuration);
                    }
                    catch (Exception ex)
                    {
#if AI_ASPNETCORE_WEB
                        AspNetCoreEventSource.Instance.TelemetryModuleInitialziationSetupFailure(module.GetType().FullName, ex.ToInvariantString());
#else
                        WorkerServiceEventSource.Instance.TelemetryModuleInitialziationSetupFailure(module.GetType().FullName, ex.ToInvariantString());
#endif
                    }
                }

                foreach (ITelemetryProcessor processor in configuration.TelemetryProcessors)
                {
                    ITelemetryModule module = processor as ITelemetryModule;
                    if (module != null)
                    {
                        module.Initialize(configuration);
                    }
                }

                // Microsoft.ApplicationInsights.DependencyCollector.DependencyTrackingTelemetryModule depends on this nullable configuration to support Correlation.
                configuration.ApplicationIdProvider = this.applicationIdProvider;

#if AI_ASPNETCORE_WEB
                AspNetCoreEventSource.Instance.LogInformational("Successfully configured TelemetryConfiguration.");
#else
                WorkerServiceEventSource.Instance.LogInformational("Successfully configured TelemetryConfiguration.");
#endif
            }
            catch (Exception ex)
            {
#if AI_ASPNETCORE_WEB
                AspNetCoreEventSource.Instance.TelemetryConfigurationSetupFailure(ex.ToInvariantString());
#else
                WorkerServiceEventSource.Instance.TelemetryConfigurationSetupFailure(ex.ToInvariantString());
#endif
            }
        }
        /// <inheritdoc />
        public void Configure(TelemetryConfiguration configuration)
        {
            try
            {
                if (this.applicationInsightsServiceOptions.InstrumentationKey != null)
                {
                    configuration.InstrumentationKey = this.applicationInsightsServiceOptions.InstrumentationKey;
                }

                if (this.telemetryModuleConfigurators.Any())
                {
                    foreach (ITelemetryModuleConfigurator telemetryModuleConfigurator in this.telemetryModuleConfigurators)
                    {
                        ITelemetryModule telemetryModule = this.modules.FirstOrDefault((module) => module.GetType() == telemetryModuleConfigurator.TelemetryModuleType);
                        if (telemetryModule != null)
                        {
                            telemetryModuleConfigurator.Configure(telemetryModule, this.applicationInsightsServiceOptions);
                        }
                        else
                        {
#if AI_ASPNETCORE_WEB
                            AspNetCoreEventSource.Instance.UnableToFindModuleToConfigure(telemetryModuleConfigurator.TelemetryModuleType.ToString());
#else
                            WorkerServiceEventSource.Instance.UnableToFindModuleToConfigure(telemetryModuleConfigurator.TelemetryModuleType.ToString());
#endif
                        }
                    }
                }

                if (this.telemetryProcessorFactories.Any())
                {
                    foreach (ITelemetryProcessorFactory processorFactory in this.telemetryProcessorFactories)
                    {
                        configuration.DefaultTelemetrySink.TelemetryProcessorChainBuilder.Use(processorFactory.Create);
                    }
                }

                // Fallback to default channel (InMemoryChannel) created by base sdk if no channel is found in DI
                configuration.TelemetryChannel = this.telemetryChannel ?? configuration.TelemetryChannel;
                (configuration.TelemetryChannel as ITelemetryModule)?.Initialize(configuration);

                this.AddAutoCollectedMetricExtractor(configuration);
                this.AddQuickPulse(configuration);
                this.AddSampling(configuration);
                this.DisableHeartBeatIfConfigured();

                configuration.DefaultTelemetrySink.TelemetryProcessorChainBuilder.Build();
                configuration.TelemetryProcessorChainBuilder.Build();

                if (this.applicationInsightsServiceOptions.DeveloperMode != null)
                {
                    configuration.TelemetryChannel.DeveloperMode = this.applicationInsightsServiceOptions.DeveloperMode;
                }

                if (this.applicationInsightsServiceOptions.EndpointAddress != null)
                {
                    configuration.TelemetryChannel.EndpointAddress = this.applicationInsightsServiceOptions.EndpointAddress;
                }

                // Need to set connection string before calling Initialize() on the Modules and Processors.
                if (this.applicationInsightsServiceOptions.ConnectionString != null)
                {
                    configuration.ConnectionString = this.applicationInsightsServiceOptions.ConnectionString;
                }

                foreach (ITelemetryInitializer initializer in this.initializers)
                {
                    configuration.TelemetryInitializers.Add(initializer);
                }

                foreach (ITelemetryModule module in this.modules)
                {
                    module.Initialize(configuration);
                }

                foreach (ITelemetryProcessor processor in configuration.TelemetryProcessors)
                {
                    ITelemetryModule module = processor as ITelemetryModule;
                    if (module != null)
                    {
                        module.Initialize(configuration);
                    }
                }

                // Microsoft.ApplicationInsights.DependencyCollector.DependencyTrackingTelemetryModule depends on this nullable configuration to support Correlation.
                configuration.ApplicationIdProvider = this.applicationIdProvider;

#if AI_ASPNETCORE_WEB
                AspNetCoreEventSource.Instance.LogInformational("Successfully configured TelemetryConfiguration.");
#else
                WorkerServiceEventSource.Instance.LogInformational("Successfully configured TelemetryConfiguration.");
#endif
            }
            catch (Exception ex)
            {
#if AI_ASPNETCORE_WEB
                AspNetCoreEventSource.Instance.TelemetryConfigurationSetupFailure(ex.ToInvariantString());
#else
                WorkerServiceEventSource.Instance.TelemetryConfigurationSetupFailure(ex.ToInvariantString());
#endif
            }
        }