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);
        }
Esempio n. 2
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);
        }
Esempio n. 3
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);
        }
 public void RegisterTelemetryModule(ITelemetryModule module)
 {
     lock (_lock)
     {
         _modules.Add(module);
     }
 }
Esempio n. 5
0
        public override void Configure(IFunctionsHostBuilder builder)
        {
            builder.Services.AddOptions <TelemetryConfiguration>()
            .Configure <IEnumerable <ITelemetryModuleConfigurator>, IEnumerable <ITelemetryModule> >((telemetryConfig, configurators, modules) =>
            {
                // Run through the registered configurators
                foreach (var configurator in configurators)
                {
                    ITelemetryModule telemetryModule = modules.FirstOrDefault((module) => module.GetType() == configurator.TelemetryModuleType);
                    if (telemetryModule != null)
                    {
                        configurator.Configure(telemetryModule);
                    }
                }
            });
            //builder.Services.AddApplicationInsightsTelemetry("2cff0d83-f8cc-f797-9f96-7fcf7c30df0e");
            //builder.Services.AddSingleton<TelemetryClient>(s => new TelemetryClient());
            builder.Services.ConfigureTelemetryModule <QuickPulseTelemetryModule>((module, o) => module.QuickPulseServiceEndpoint = "https://quickpulse.applicationinsights.us/QuickPulseService.svc");
            builder.Services.AddSingleton <IApplicationIdProvider>(_ => new ApplicationInsightsApplicationIdProvider()
            {
                ProfileQueryEndpoint = "https://dc.applicationinsights.us/api/profiles/{0}/appId"
            });
            builder.Services.AddSingleton <ITelemetryChannel>(s =>
            {
                // HACK: Need to force the options factory to run somewhere so it'll run through our Configurators.
                var ignore = s.GetService <IOptions <TelemetryConfiguration> >().Value;

                return(new ServerTelemetryChannel {
                    EndpointAddress = "https://dc.applicationinsights.us/v2/track"
                });
            });
        }
 private static void DisposeIfDisposable(ITelemetryModule module)
 {
     if (module is IDisposable)
     {
         (module as IDisposable).Dispose();
     }
 }
        private void DisposeModule(ITelemetryModule module)
        {
            var disposeMethod = module.GetType().GetMethod("Dispose");

            if (disposeMethod != null)
            {
                disposeMethod.Invoke(module, null);
            }
        }
        public void UnRegisterTelemetryModule(ITelemetryModule module)
        {
            lock (_lock)
            {
                _modules.Remove(module);

                DisposeModule(module);
            }
        }
Esempio n. 9
0
 public UpdateHandler(IMessenger messenger, LiveProgramInfo programInfo, IReceiveUserInput inputReceiver
                      , IInstallUpdates updateInstaller, Locator locator, ITelemetryModule telemetryModule)
 {
     this.messenger       = messenger;
     this.programInfo     = programInfo;
     this.inputReceiver   = inputReceiver;
     this.updateInstaller = updateInstaller;
     this.locator         = locator;
     this.telemetryModule = telemetryModule;
 }
Esempio n. 10
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);
                }
            }
        }
Esempio n. 11
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);
        }
        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
                        {
#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
            }
        }
Esempio n. 15
0
 public void Configure(ITelemetryModule telemetryModule)
 {
     this.configure?.Invoke(telemetryModule, null);
 }
Esempio n. 16
0
 /// <summary>
 /// Configures telemetry module.
 /// </summary>
 public void Configure(ITelemetryModule telemetryModule, ApplicationInsightsServiceOptions options)
 {
     this.configure?.Invoke(telemetryModule, options);
 }
Esempio n. 17
0
 public TelemetryClientBuilder AddModules(ITelemetryModule telemetryModule)
 {
     _telemetryModules.Add(telemetryModule);
     return(this);
 }
        /// <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);
            }
        }
Esempio n. 19
0
        /// <summary>
        /// Configure Function.
        /// </summary>
        /// <param name="builder">Function Host Builder.</param>
        public override void Configure(IFunctionsHostBuilder builder)
        {
            var config = new ConfigurationBuilder()
                         .SetBasePath(Environment.CurrentDirectory)
                         .AddJsonFile($"local.settings.json", optional: true, reloadOnChange: true)
                         .AddUserSecrets <Startup>()
                         .AddEnvironmentVariables()
                         .Build();

            builder.Services.AddSingleton <IConfiguration>(config);

            if (bool.Parse(config["IsRunningInFairfax"]))
            {
                #region Begin App Insights configuration workaround for Azure Gov

                builder.Services.AddOptions <TelemetryConfiguration>()
                .Configure <IEnumerable <ITelemetryModuleConfigurator>, IEnumerable <ITelemetryModule> >(
                    (telemetryConfig, configurators, modules) =>
                {
                    // Run through the registered configurators
                    foreach (var configurator in configurators)
                    {
                        ITelemetryModule telemetryModule = modules.FirstOrDefault((module) =>
                                                                                  module.GetType() == configurator.TelemetryModuleType);
                        if (telemetryModule != null)
                        {
                            // next line of code is giving us a compiler warning (OBSOLETE)
                            configurator.Configure(telemetryModule);
                        }
                    }
                });

                // The ConfigureTelemetryModule() call on the next line results in the following exception:
                // FunctionAppTest: Method Microsoft.Extensions.DependencyInjection.ApplicationInsightsExtensions.ConfigureTelemetryModule:
                // type argument 'Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector.QuickPulse.QuickPulseTelemetryModule' violates the constraint of type parameter 'T'.
                builder.Services.ConfigureTelemetryModule <QuickPulseTelemetryModule>((module, o) =>
                                                                                      module.QuickPulseServiceEndpoint =
                                                                                          "https://quickpulse.applicationinsights.us/QuickPulseService.svc");
                builder.Services.AddSingleton <IApplicationIdProvider>(_ =>
                                                                       new ApplicationInsightsApplicationIdProvider()
                {
                    ProfileQueryEndpoint = "https://dc.applicationinsights.us/api/profiles/{0}/appId"
                });
                builder.Services.AddSingleton <ITelemetryChannel>(s =>
                {
                    // HACK: Need to force the options factory to run somewhere so it'll run through our Configurators.
                    var ignore = s.GetService <IOptions <TelemetryConfiguration> >().Value;

                    return(new ServerTelemetryChannel
                    {
                        EndpointAddress = "https://dc.applicationinsights.us/v2/track", DeveloperMode = true
                    });
                });

                #endregion
            }

            builder.Services.AddLogging(loggingBuilder =>
            {
                loggingBuilder.AddFilter <Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider>("", LogLevel.Information);
                loggingBuilder.AddConsole();
                loggingBuilder.AddFilter <Microsoft.Extensions.Logging.Console.ConsoleLoggerProvider>("", LogLevel.Information);
            });

            builder.Services.AddHttpClient();
            builder.Services.ConfigureCommonServices();
        }