|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the MIT License. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Linq; |
| 6 | +using Microsoft.ApplicationInsights.Extensibility; |
| 7 | +using Microsoft.ApplicationInsights.WorkerService; |
| 8 | +using Microsoft.Azure.Functions.Worker.ApplicationInsights; |
| 9 | +using Microsoft.Azure.Functions.Worker.Logging; |
| 10 | +using Microsoft.Extensions.DependencyInjection; |
| 11 | +using Microsoft.Extensions.DependencyInjection.Extensions; |
| 12 | +using Microsoft.Extensions.Logging; |
| 13 | +using Microsoft.Extensions.Logging.ApplicationInsights; |
| 14 | + |
| 15 | +namespace Microsoft.Azure.Functions.Worker |
| 16 | +{ |
| 17 | + public static class FunctionsApplicationInsightsExtensions |
| 18 | + { |
| 19 | + /// <summary> |
| 20 | + /// Adds Application Insights support by internally calling <see cref="ApplicationInsightsExtensions.AddApplicationInsightsTelemetryWorkerService(IServiceCollection)"/>. |
| 21 | + /// </summary> |
| 22 | + /// <param name="builder">The <see cref="IFunctionsWorkerApplicationBuilder"/></param> |
| 23 | + /// <param name="configureOptions">Action to configure ApplicationInsights services.</param> |
| 24 | + /// <returns>The <see cref="IFunctionsWorkerApplicationBuilder"/></returns> |
| 25 | + public static IFunctionsWorkerApplicationBuilder AddApplicationInsights(this IFunctionsWorkerApplicationBuilder builder, Action<ApplicationInsightsServiceOptions>? configureOptions = null) |
| 26 | + { |
| 27 | + builder.AddCommonServices(); |
| 28 | + |
| 29 | + builder.Services.AddApplicationInsightsTelemetryWorkerService(options => |
| 30 | + { |
| 31 | + configureOptions?.Invoke(options); |
| 32 | + }); |
| 33 | + |
| 34 | + return builder; |
| 35 | + } |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Adds the <see cref="ApplicationInsightsLoggerProvider"/> and disables the Functions host passthrough logger. |
| 39 | + /// </summary> |
| 40 | + /// <param name="builder">The <see cref="IFunctionsWorkerApplicationBuilder"/></param> |
| 41 | + /// <param name="configureOptions">Action to configure ApplicationInsights logger.</param> |
| 42 | + /// <returns>The <see cref="IFunctionsWorkerApplicationBuilder"/></returns> |
| 43 | + public static IFunctionsWorkerApplicationBuilder AddApplicationInsightsLogger(this IFunctionsWorkerApplicationBuilder builder, Action<ApplicationInsightsLoggerOptions>? configureOptions = null) |
| 44 | + { |
| 45 | + builder.AddCommonServices(); |
| 46 | + |
| 47 | + builder.Services.AddLogging(logging => |
| 48 | + { |
| 49 | + logging.AddApplicationInsights(options => |
| 50 | + { |
| 51 | + options.IncludeScopes = false; |
| 52 | + configureOptions?.Invoke(options); |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + return builder; |
| 57 | + } |
| 58 | + |
| 59 | + private static IFunctionsWorkerApplicationBuilder AddCommonServices(this IFunctionsWorkerApplicationBuilder builder) |
| 60 | + { |
| 61 | + builder.Services.TryAddEnumerable(new ServiceDescriptor(typeof(ITelemetryInitializer), typeof(FunctionsTelemetryInitializer), ServiceLifetime.Singleton)); |
| 62 | + builder.Services.TryAddEnumerable(new ServiceDescriptor(typeof(ITelemetryModule), typeof(FunctionsTelemetryModule), ServiceLifetime.Singleton)); |
| 63 | + |
| 64 | + // User logs will be written directly to Application Insights; this prevents duplicate logging. |
| 65 | + builder.Services.AddSingleton<IUserLogWriter>(_ => NullUserLogWriter.Instance); |
| 66 | + |
| 67 | + // This middleware is temporary for the preview. Eventually this behavior will move into the |
| 68 | + // core worker assembly. |
| 69 | + if (!builder.Services.Any(p => p.ImplementationType == typeof(FunctionActivitySourceMiddleware))) |
| 70 | + { |
| 71 | + builder.Services.AddSingleton<FunctionActivitySourceMiddleware>(); |
| 72 | + builder.Use(next => |
| 73 | + { |
| 74 | + return async context => |
| 75 | + { |
| 76 | + var middleware = context.InstanceServices.GetRequiredService<FunctionActivitySourceMiddleware>(); |
| 77 | + await middleware.Invoke(context, next); |
| 78 | + }; |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + return builder; |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments