public WebJobsTelemetryInitializer(ISdkVersionProvider versionProvider)
        {
            if (versionProvider == null)
            {
                throw new ArgumentNullException(nameof(versionProvider));
            }

            _sdkVersion = versionProvider.GetSdkVersion();
        }
        internal static void ConfigureApplicationInsights(HostBuilderContext context, ILoggingBuilder builder)
        {
            string appInsightsKey = context.Configuration[EnvironmentSettingNames.AppInsightsInstrumentationKey];

            if (!string.IsNullOrEmpty(appInsightsKey))
            {
                builder.AddApplicationInsights(o => o.InstrumentationKey = appInsightsKey);
                builder.Services.ConfigureOptions <ApplicationInsightsLoggerOptionsSetup>();

                builder.Services.AddSingleton <ISdkVersionProvider, ApplicationInsightsSdkVersionProvider>();

                // Override the default SdkVersion with the functions key
                builder.Services.AddSingleton <TelemetryClient>(provider =>
                {
                    TelemetryConfiguration configuration = provider.GetService <TelemetryConfiguration>();
                    TelemetryClient client = new TelemetryClient(configuration);

                    ISdkVersionProvider versionProvider            = provider.GetService <ISdkVersionProvider>();
                    client.Context.GetInternalContext().SdkVersion = versionProvider.GetSdkVersion();

                    return(client);
                });
            }
        }
Beispiel #3
0
        public static IServiceCollection AddApplicationInsights(this IServiceCollection services)
        {
            services.TryAddSingleton <ISdkVersionProvider, WebJobsSdkVersionProvider>();

            // Bind to the configuration section registered with
            services.AddOptions <ApplicationInsightsLoggerOptions>()
            .Configure <ILoggerProviderConfiguration <ApplicationInsightsLoggerProvider> >((options, config) =>
            {
                config.Configuration?.Bind(options);
            });

            services.AddSingleton <ITelemetryInitializer, HttpDependenciesParsingTelemetryInitializer>();
            services.AddSingleton <ITelemetryInitializer, WebJobsRoleEnvironmentTelemetryInitializer>();
            services.AddSingleton <ITelemetryInitializer, WebJobsTelemetryInitializer>();
            services.AddSingleton <ITelemetryInitializer, WebJobsSanitizingInitializer>();
            services.AddSingleton <ITelemetryModule, QuickPulseTelemetryModule>();

            services.AddSingleton <IApplicationIdProvider, ApplicationInsightsApplicationIdProvider>();

            services.AddSingleton <ITelemetryModule, DependencyTrackingTelemetryModule>(provider =>
            {
                var dependencyCollector = new DependencyTrackingTelemetryModule();
                var excludedDomains     = dependencyCollector.ExcludeComponentCorrelationHttpHeadersOnDomains;
                excludedDomains.Add("core.windows.net");
                excludedDomains.Add("core.chinacloudapi.cn");
                excludedDomains.Add("core.cloudapi.de");
                excludedDomains.Add("core.usgovcloudapi.net");
                excludedDomains.Add("localhost");
                excludedDomains.Add("127.0.0.1");

                var includedActivities = dependencyCollector.IncludeDiagnosticSourceActivities;
                includedActivities.Add("Microsoft.Azure.ServiceBus");

                return(dependencyCollector);
            });
            services.AddSingleton <ITelemetryModule, AppServicesHeartbeatTelemetryModule>();

            services.AddSingleton <ITelemetryChannel, ServerTelemetryChannel>();
            services.AddSingleton <TelemetryConfiguration>(provider =>
            {
                ApplicationInsightsLoggerOptions options = provider.GetService <IOptions <ApplicationInsightsLoggerOptions> >().Value;
                LoggerFilterOptions filterOptions        = CreateFilterOptions(provider.GetService <IOptions <LoggerFilterOptions> >().Value);

                ITelemetryChannel channel     = provider.GetService <ITelemetryChannel>();
                TelemetryConfiguration config = TelemetryConfiguration.CreateDefault();

                IApplicationIdProvider appIdProvider = provider.GetService <IApplicationIdProvider>();

                // Because of https://github.com/Microsoft/ApplicationInsights-dotnet-server/issues/943
                // we have to touch (and create) Active configuration before initializing telemetry modules
                // Active configuration is used to report AppInsights heartbeats
                // role environment telemetry initializer is needed to correlate heartbeats to particular host

                var activeConfig = TelemetryConfiguration.Active;
                if (!string.IsNullOrEmpty(options.InstrumentationKey) &&
                    string.IsNullOrEmpty(activeConfig.InstrumentationKey))
                {
                    activeConfig.InstrumentationKey = options.InstrumentationKey;
                }

                if (!activeConfig.TelemetryInitializers.OfType <WebJobsRoleEnvironmentTelemetryInitializer>().Any())
                {
                    activeConfig.TelemetryInitializers.Add(
                        new WebJobsRoleEnvironmentTelemetryInitializer());
                }

                SetupTelemetryConfiguration(
                    config,
                    options,
                    channel,
                    provider.GetServices <ITelemetryInitializer>(),
                    provider.GetServices <ITelemetryModule>(),
                    appIdProvider,
                    filterOptions);

                return(config);
            });

            services.AddSingleton <TelemetryClient>(provider =>
            {
                TelemetryConfiguration configuration = provider.GetService <TelemetryConfiguration>();
                TelemetryClient client = new TelemetryClient(configuration);

                ISdkVersionProvider versionProvider            = provider.GetService <ISdkVersionProvider>();
                client.Context.GetInternalContext().SdkVersion = versionProvider?.GetSdkVersion();

                return(client);
            });

            services.AddSingleton <ILoggerProvider, ApplicationInsightsLoggerProvider>();

            return(services);
        }
        public static IServiceCollection AddApplicationInsights(this IServiceCollection services)
        {
            services.TryAddSingleton <ISdkVersionProvider, WebJobsSdkVersionProvider>();

            // Bind to the configuration section registered with
            services.AddOptions <ApplicationInsightsLoggerOptions>()
            .Configure <ILoggerProviderConfiguration <ApplicationInsightsLoggerProvider> >((options, config) =>
            {
                config.Configuration?.Bind(options);
            });

            services.AddSingleton <ITelemetryInitializer, HttpDependenciesParsingTelemetryInitializer>();
            services.AddSingleton <ITelemetryInitializer, WebJobsRoleEnvironmentTelemetryInitializer>();
            services.AddSingleton <ITelemetryInitializer, WebJobsSanitizingInitializer>();
            services.AddSingleton <ITelemetryInitializer, WebJobsTelemetryInitializer>();

            services.AddSingleton <ITelemetryModule, QuickPulseTelemetryModule>();

            services.AddSingleton <ITelemetryModule>(provider =>
            {
                ApplicationInsightsLoggerOptions options = provider.GetService <IOptions <ApplicationInsightsLoggerOptions> >().Value;
                if (options.EnablePerformanceCountersCollection)
                {
                    return(new PerformanceCollectorModule());
                }

                return(NullTelemetryModule.Instance);
            });

            services.AddSingleton <IApplicationIdProvider, ApplicationInsightsApplicationIdProvider>();

            services.AddSingleton <ITelemetryModule, DependencyTrackingTelemetryModule>(provider =>
            {
                var options = provider.GetService <IOptions <ApplicationInsightsLoggerOptions> >().Value;

                var dependencyCollector = new DependencyTrackingTelemetryModule();
                var excludedDomains     = dependencyCollector.ExcludeComponentCorrelationHttpHeadersOnDomains;
                excludedDomains.Add("core.windows.net");
                excludedDomains.Add("core.chinacloudapi.cn");
                excludedDomains.Add("core.cloudapi.de");
                excludedDomains.Add("core.usgovcloudapi.net");
                excludedDomains.Add("localhost");
                excludedDomains.Add("127.0.0.1");

                var includedActivities = dependencyCollector.IncludeDiagnosticSourceActivities;
                includedActivities.Add("Microsoft.Azure.ServiceBus");

                dependencyCollector.EnableW3CHeadersInjection = options.HttpAutoCollectionOptions.EnableW3CDistributedTracing;
                return(dependencyCollector);
            });

            services.AddSingleton <ITelemetryModule>(provider =>
            {
                var options = provider.GetService <IOptions <ApplicationInsightsLoggerOptions> >().Value;
                if (options.HttpAutoCollectionOptions.EnableHttpTriggerExtendedInfoCollection)
                {
                    var appIdProvider = provider.GetService <IApplicationIdProvider>();

                    return(new RequestTrackingTelemetryModule(appIdProvider)
                    {
                        CollectionOptions = new RequestCollectionOptions
                        {
                            TrackExceptions = false, // webjobs/functions track exceptions themselves
                            EnableW3CDistributedTracing = options.HttpAutoCollectionOptions.EnableW3CDistributedTracing,
                            InjectResponseHeaders = options.HttpAutoCollectionOptions.EnableResponseHeaderInjection
                        }
                    });
                }

                return(NullTelemetryModule.Instance);
            });

            services.AddSingleton <ITelemetryModule, AppServicesHeartbeatTelemetryModule>();

            services.AddSingleton <ITelemetryChannel, ServerTelemetryChannel>();
            services.AddSingleton <TelemetryConfiguration>(provider =>
            {
                ApplicationInsightsLoggerOptions options = provider.GetService <IOptions <ApplicationInsightsLoggerOptions> >().Value;
                LoggerFilterOptions filterOptions        = CreateFilterOptions(provider.GetService <IOptions <LoggerFilterOptions> >().Value);

                ITelemetryChannel channel     = provider.GetService <ITelemetryChannel>();
                TelemetryConfiguration config = TelemetryConfiguration.CreateDefault();

                IApplicationIdProvider appIdProvider   = provider.GetService <IApplicationIdProvider>();
                ISdkVersionProvider sdkVersionProvider = provider.GetService <ISdkVersionProvider>();
                // Because of https://github.com/Microsoft/ApplicationInsights-dotnet-server/issues/943
                // we have to touch (and create) Active configuration before initializing telemetry modules
                // Active configuration is used to report AppInsights heartbeats
                // role environment telemetry initializer is needed to correlate heartbeats to particular host

                var activeConfig = TelemetryConfiguration.Active;
                if (!string.IsNullOrEmpty(options.InstrumentationKey) &&
                    string.IsNullOrEmpty(activeConfig.InstrumentationKey))
                {
                    activeConfig.InstrumentationKey = options.InstrumentationKey;
                }

                if (!activeConfig.TelemetryInitializers.OfType <WebJobsRoleEnvironmentTelemetryInitializer>().Any())
                {
                    activeConfig.TelemetryInitializers.Add(new WebJobsRoleEnvironmentTelemetryInitializer());
                    activeConfig.TelemetryInitializers.Add(new WebJobsTelemetryInitializer(sdkVersionProvider));
                    if (options.HttpAutoCollectionOptions.EnableW3CDistributedTracing)
                    {
                        // W3C distributed tracing is enabled by the feature flag inside ApplicationInsights SDK
                        // W3COperationCorrelationTelemetryInitializer will go away once W3C is implemented
                        // in the DiagnosticSource (.NET)

                        TelemetryConfiguration.Active.TelemetryInitializers.Add(new W3COperationCorrelationTelemetryInitializer());
                    }
                }

                SetupTelemetryConfiguration(
                    config,
                    options,
                    channel,
                    provider.GetServices <ITelemetryInitializer>(),
                    provider.GetServices <ITelemetryModule>(),
                    appIdProvider,
                    filterOptions);

                return(config);
            });

            services.AddSingleton <TelemetryClient>(provider =>
            {
                TelemetryConfiguration configuration = provider.GetService <TelemetryConfiguration>();
                TelemetryClient client = new TelemetryClient(configuration);

                ISdkVersionProvider versionProvider            = provider.GetService <ISdkVersionProvider>();
                client.Context.GetInternalContext().SdkVersion = versionProvider?.GetSdkVersion();

                return(client);
            }
                                                    );

            services.AddSingleton <ILoggerProvider, ApplicationInsightsLoggerProvider>();

            return(services);
        }