From bd9500d1422fb8ec64d6b122ffcbfb23371c3541 Mon Sep 17 00:00:00 2001 From: George Pollard Date: Wed, 2 Nov 2022 21:40:11 +0000 Subject: [PATCH] Improve AppInsights setup --- src/ApiService/ApiService/ApiService.csproj | 3 +- src/ApiService/ApiService/Log.cs | 8 +- src/ApiService/ApiService/Program.cs | 19 +- src/ApiService/ApiService/packages.lock.json | 203 +++++++++++++++++-- 4 files changed, 201 insertions(+), 32 deletions(-) diff --git a/src/ApiService/ApiService/ApiService.csproj b/src/ApiService/ApiService/ApiService.csproj index 3e42f47511b..2d27978b2d3 100644 --- a/src/ApiService/ApiService/ApiService.csproj +++ b/src/ApiService/ApiService/ApiService.csproj @@ -8,7 +8,7 @@ - + @@ -20,7 +20,6 @@ - diff --git a/src/ApiService/ApiService/Log.cs b/src/ApiService/ApiService/Log.cs index 04a19f2f5b9..8cb92b0e8d2 100644 --- a/src/ApiService/ApiService/Log.cs +++ b/src/ApiService/ApiService/Log.cs @@ -65,8 +65,8 @@ public interface ILog { class AppInsights : ILog { private readonly TelemetryClient _telemetryClient; - public AppInsights(TelemetryConfiguration config) { - _telemetryClient = new TelemetryClient(config); + public AppInsights(TelemetryClient client) { + _telemetryClient = client; } public void Log(Guid correlationId, LogStringHandler message, SeverityLevel level, IReadOnlyDictionary tags, string? caller) { @@ -366,12 +366,12 @@ public interface ILogSinks { public class LogSinks : ILogSinks { private readonly List _loggers; - public LogSinks(IServiceConfig config, TelemetryConfiguration telemetryConfiguration) { + public LogSinks(IServiceConfig config, TelemetryClient telemetryClient) { _loggers = new List(); foreach (var dest in config.LogDestinations) { _loggers.Add( dest switch { - LogDestination.AppInsights => new AppInsights(telemetryConfiguration), + LogDestination.AppInsights => new AppInsights(telemetryClient), LogDestination.Console => new Console(), _ => throw new Exception($"Unhandled Log Destination type: {dest}"), } diff --git a/src/ApiService/ApiService/Program.cs b/src/ApiService/ApiService/Program.cs index 9e88a0f813b..9f5a8a85881 100644 --- a/src/ApiService/ApiService/Program.cs +++ b/src/ApiService/ApiService/Program.cs @@ -44,6 +44,8 @@ public async Async.Task Invoke(FunctionContext context, FunctionExecutionDelegat //Move out expensive resources into separate class, and add those as Singleton // ArmClient, Table Client(s), Queue Client(s), HttpClient, etc. public static async Async.Task Main() { + var configuration = new ServiceConfiguration(); + using var host = new HostBuilder() .ConfigureFunctionsWorkerDefaults( @@ -110,18 +112,14 @@ public static async Async.Task Main() { .AddScoped() .AddScoped() .AddScoped() - - .AddSingleton(provider => { - var config = provider.GetRequiredService(); - return new() { - ConnectionString = $"InstrumentationKey={config.ApplicationInsightsInstrumentationKey}", - }; + .AddApplicationInsightsTelemetry(options => { + options.ConnectionString = $"InstrumentationKey={configuration.ApplicationInsightsInstrumentationKey}"; }) .AddSingleton(new GraphServiceClient(new DefaultAzureCredential())) .AddSingleton() .AddSingleton() .AddSingleton() - .AddSingleton() + .AddSingleton(configuration) .AddSingleton() .AddSingleton() .AddHttpClient() @@ -129,13 +127,6 @@ public static async Async.Task Main() { }) .Build(); - // Set up Application Insights dependency tracking: - { - var telemetryConfig = host.Services.GetRequiredService(); - var module = host.Services.GetRequiredService(); - module.Initialize(telemetryConfig); - } - // Initialize expected Storage tables: await SetupStorage( host.Services.GetRequiredService(), diff --git a/src/ApiService/ApiService/packages.lock.json b/src/ApiService/ApiService/packages.lock.json index 8ff9fc8b0ac..7e8a76503d3 100644 --- a/src/ApiService/ApiService/packages.lock.json +++ b/src/ApiService/ApiService/packages.lock.json @@ -157,6 +157,25 @@ "resolved": "0.12.2", "contentHash": "JgMAGj8ekeAzKkagubXqf1UqgfHq89GyA1UQYWbkAe441uRr2Rh2rktkx5Z0LPwmD/aOqu9cxjekD2GZjP8rbw==" }, + "Microsoft.ApplicationInsights.AspNetCore": { + "type": "Direct", + "requested": "[2.21.0, )", + "resolved": "2.21.0", + "contentHash": "d+7MB4YdUMc9Mtq2u6C7TritzC0eKfHkhGmlnNckDDQiOQuk9IHMPxUmPBRMm/tn+db8fI/BYno9jGxLhI+SZw==", + "dependencies": { + "Microsoft.ApplicationInsights": "2.21.0", + "Microsoft.ApplicationInsights.DependencyCollector": "2.21.0", + "Microsoft.ApplicationInsights.EventCounterCollector": "2.21.0", + "Microsoft.ApplicationInsights.PerfCounterCollector": "2.21.0", + "Microsoft.ApplicationInsights.WindowsServer": "2.21.0", + "Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel": "2.21.0", + "Microsoft.AspNetCore.Hosting": "2.1.1", + "Microsoft.AspNetCore.Http": "2.1.22", + "Microsoft.Extensions.Configuration.Json": "3.1.0", + "Microsoft.Extensions.Logging.ApplicationInsights": "2.21.0", + "System.Text.Encodings.Web": "4.7.2" + } + }, "Microsoft.ApplicationInsights.DependencyCollector": { "type": "Direct", "requested": "[2.21.0, )", @@ -424,6 +443,45 @@ "System.Diagnostics.DiagnosticSource": "5.0.0" } }, + "Microsoft.ApplicationInsights.EventCounterCollector": { + "type": "Transitive", + "resolved": "2.21.0", + "contentHash": "MfF9IKxx9UhaYHVFQ1VKw0LYvBhkjZtPNUmCTYlGws0N7D2EaupmeIj/EWalqP47sQRedR9+VzARsONcwH8OCA==", + "dependencies": { + "Microsoft.ApplicationInsights": "2.21.0" + } + }, + "Microsoft.ApplicationInsights.PerfCounterCollector": { + "type": "Transitive", + "resolved": "2.21.0", + "contentHash": "RcckSVkfu+NkDie6/HyM6AVLHmTMVZrUrYnDeJdvRByOc2a+DqTM6KXMtsTHW/5+K7DT9QK5ZrZdi0YbBW8PVA==", + "dependencies": { + "Microsoft.ApplicationInsights": "2.21.0", + "Microsoft.Extensions.Caching.Memory": "1.0.0", + "System.Diagnostics.PerformanceCounter": "4.7.0" + } + }, + "Microsoft.ApplicationInsights.WindowsServer": { + "type": "Transitive", + "resolved": "2.21.0", + "contentHash": "Xbhss7dqbKyE5PENm1lRA9oxzhKEouKGMzgNqJ9xTHPZiogDwKVMK02qdbVhvaXKf9zG8RvvIpM5tnGR5o+Onw==", + "dependencies": { + "Microsoft.ApplicationInsights": "2.21.0", + "Microsoft.ApplicationInsights.DependencyCollector": "2.21.0", + "Microsoft.ApplicationInsights.PerfCounterCollector": "2.21.0", + "Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel": "2.21.0", + "System.Diagnostics.DiagnosticSource": "5.0.0" + } + }, + "Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel": { + "type": "Transitive", + "resolved": "2.21.0", + "contentHash": "7D4oq+9YyagEPx+0kNNOXdG6c7IDM/2d+637nAYKFqdWhNN0IqHZEed0DuG28waj7hBSLM9lBO+B8qQqgfE4rw==", + "dependencies": { + "Microsoft.ApplicationInsights": "2.21.0", + "System.IO.FileSystem.AccessControl": "4.7.0" + } + }, "Microsoft.AspNet.WebApi.Client": { "type": "Transitive", "resolved": "5.2.7", @@ -458,6 +516,94 @@ "resolved": "5.0.8", "contentHash": "ZI9S2NGjuOKXN3PxJcF8EKVwd1cqpWyUSqiVoH8gqq5tlHaXULwPmoR0DBOFON4sEFETRWI69f5RQ3tJWw205A==" }, + "Microsoft.AspNetCore.Hosting": { + "type": "Transitive", + "resolved": "2.1.1", + "contentHash": "MqYc0DUxrhAPnb5b4HFspxsoJT+gJlLsliSxIgovf4BsbmpaXQId0/pDiVzLuEbmks2w1/lRfY8w0lQOuK1jQQ==", + "dependencies": { + "Microsoft.AspNetCore.Hosting.Abstractions": "2.1.1", + "Microsoft.AspNetCore.Http": "2.1.1", + "Microsoft.AspNetCore.Http.Extensions": "2.1.1", + "Microsoft.Extensions.Configuration": "2.1.1", + "Microsoft.Extensions.Configuration.EnvironmentVariables": "2.1.1", + "Microsoft.Extensions.Configuration.FileExtensions": "2.1.1", + "Microsoft.Extensions.DependencyInjection": "2.1.1", + "Microsoft.Extensions.FileProviders.Physical": "2.1.1", + "Microsoft.Extensions.Hosting.Abstractions": "2.1.1", + "Microsoft.Extensions.Logging": "2.1.1", + "Microsoft.Extensions.Options": "2.1.1", + "System.Diagnostics.DiagnosticSource": "4.5.0", + "System.Reflection.Metadata": "1.6.0" + } + }, + "Microsoft.AspNetCore.Hosting.Abstractions": { + "type": "Transitive", + "resolved": "2.1.1", + "contentHash": "76cKcp2pWhvdV2TXTqMg/DyW7N6cDzTEhtL8vVWFShQN+Ylwv3eO/vUQr2BS3Hz4IZHEpL+FOo2T+MtymHDqDQ==", + "dependencies": { + "Microsoft.AspNetCore.Hosting.Server.Abstractions": "2.1.1", + "Microsoft.AspNetCore.Http.Abstractions": "2.1.1", + "Microsoft.Extensions.Hosting.Abstractions": "2.1.1" + } + }, + "Microsoft.AspNetCore.Hosting.Server.Abstractions": { + "type": "Transitive", + "resolved": "2.1.1", + "contentHash": "+vD7HJYzAXNq17t+NgRkpS38cxuAyOBu8ixruOiA3nWsybozolUdALWiZ5QFtGRzajSLPFA2YsbO3NPcqoUwcw==", + "dependencies": { + "Microsoft.AspNetCore.Http.Features": "2.1.1", + "Microsoft.Extensions.Configuration.Abstractions": "2.1.1" + } + }, + "Microsoft.AspNetCore.Http": { + "type": "Transitive", + "resolved": "2.1.22", + "contentHash": "+Blk++1JWqghbl8+3azQmKhiNZA5wAepL9dY2I6KVmu2Ri07MAcvAVC888qUvO7yd7xgRgZOMfihezKg14O/2A==", + "dependencies": { + "Microsoft.AspNetCore.Http.Abstractions": "2.1.1", + "Microsoft.AspNetCore.WebUtilities": "2.1.1", + "Microsoft.Extensions.ObjectPool": "2.1.1", + "Microsoft.Extensions.Options": "2.1.1", + "Microsoft.Net.Http.Headers": "2.1.1" + } + }, + "Microsoft.AspNetCore.Http.Abstractions": { + "type": "Transitive", + "resolved": "2.1.1", + "contentHash": "kQUEVOU4loc8CPSb2WoHFTESqwIa8Ik7ysCBfTwzHAd0moWovc9JQLmhDIHlYLjHbyexqZAlkq/FPRUZqokebw==", + "dependencies": { + "Microsoft.AspNetCore.Http.Features": "2.1.1", + "System.Text.Encodings.Web": "4.5.0" + } + }, + "Microsoft.AspNetCore.Http.Extensions": { + "type": "Transitive", + "resolved": "2.1.1", + "contentHash": "ncAgV+cqsWSqjLXFUTyObGh4Tr7ShYYs3uW8Q/YpRwZn7eLV7dux5Z6GLY+rsdzmIHiia3Q2NWbLULQi7aziHw==", + "dependencies": { + "Microsoft.AspNetCore.Http.Abstractions": "2.1.1", + "Microsoft.Extensions.FileProviders.Abstractions": "2.1.1", + "Microsoft.Net.Http.Headers": "2.1.1", + "System.Buffers": "4.5.0" + } + }, + "Microsoft.AspNetCore.Http.Features": { + "type": "Transitive", + "resolved": "2.1.1", + "contentHash": "VklZ7hWgSvHBcDtwYYkdMdI/adlf7ebxTZ9kdzAhX+gUs5jSHE9mZlTamdgf9miSsxc1QjNazHXTDJdVPZKKTw==", + "dependencies": { + "Microsoft.Extensions.Primitives": "2.1.1" + } + }, + "Microsoft.AspNetCore.WebUtilities": { + "type": "Transitive", + "resolved": "2.1.1", + "contentHash": "PGKIZt4+412Z/XPoSjvYu/QIbTxcAQuEFNoA1Pw8a9mgmO0ZhNBmfaNyhgXFf7Rq62kP0tT/2WXpxdcQhkFUPA==", + "dependencies": { + "Microsoft.Net.Http.Headers": "2.1.1", + "System.Text.Encodings.Web": "4.5.0" + } + }, "Microsoft.Azure.Functions.Worker.Core": { "type": "Transitive", "resolved": "1.4.0", @@ -778,6 +924,11 @@ "Microsoft.Extensions.Primitives": "5.0.0" } }, + "Microsoft.Extensions.ObjectPool": { + "type": "Transitive", + "resolved": "2.1.1", + "contentHash": "SErON45qh4ogDp6lr6UvVmFYW0FERihW+IQ+2JyFv1PUyWktcJytFaWH5zarufJvZwhci7Rf1IyGXr9pVEadTw==" + }, "Microsoft.Extensions.Options": { "type": "Transitive", "resolved": "5.0.0", @@ -876,6 +1027,15 @@ "System.Security.Cryptography.Cng": "4.5.0" } }, + "Microsoft.Net.Http.Headers": { + "type": "Transitive", + "resolved": "2.1.1", + "contentHash": "lPNIphl8b2EuhOE9dMH6EZDmu7pS882O+HMi5BJNsigxHaWlBrYxZHFZgE18cyaPp6SSZcTkKkuzfjV/RRQKlA==", + "dependencies": { + "Microsoft.Extensions.Primitives": "2.1.1", + "System.Buffers": "4.5.0" + } + }, "Microsoft.NETCore.Platforms": { "type": "Transitive", "resolved": "5.0.0", @@ -1161,15 +1321,8 @@ }, "System.Buffers": { "type": "Transitive", - "resolved": "4.3.0", - "contentHash": "ratu44uTIHgeBeI0dE8DWvmXVBSo4u7ozRZZHOMmK/JPpYyo0dAfgSiHlpiObMQ5lEtEyIXA40sKRYg5J6A8uQ==", - "dependencies": { - "System.Diagnostics.Debug": "4.3.0", - "System.Diagnostics.Tracing": "4.3.0", - "System.Resources.ResourceManager": "4.3.0", - "System.Runtime": "4.3.0", - "System.Threading": "4.3.0" - } + "resolved": "4.5.0", + "contentHash": "pL2ChpaRRWI/p4LXyy4RgeWlYF2sgfj/pnVMvBqwNFr5cXg7CXNnWZWxrOONLg8VGdFB8oB+EG2Qw4MLgTOe+A==" }, "System.Collections": { "type": "Transitive", @@ -1205,10 +1358,11 @@ }, "System.Configuration.ConfigurationManager": { "type": "Transitive", - "resolved": "4.4.1", - "contentHash": "jz3TWKMAeuDEyrPCK5Jyt4bzQcmzUIMcY9Ud6PkElFxTfnsihV+9N/UCqvxe1z5gc7jMYAnj7V1COMS9QKIuHQ==", + "resolved": "4.7.0", + "contentHash": "/anOTeSZCNNI2zDilogWrZ8pNqCmYbzGNexUnNhjW8k0sHqEZ2nHJBp147jBV3hGYswu5lINpNg1vxR7bnqvVA==", "dependencies": { - "System.Security.Cryptography.ProtectedData": "4.4.0" + "System.Security.Cryptography.ProtectedData": "4.7.0", + "System.Security.Permissions": "4.7.0" } }, "System.Console": { @@ -1259,6 +1413,17 @@ "System.Security.Principal.Windows": "5.0.0" } }, + "System.Diagnostics.PerformanceCounter": { + "type": "Transitive", + "resolved": "4.7.0", + "contentHash": "kE9szT4i3TYT9bDE/BPfzg9/BL6enMiZlcUmnUEBrhRtxWvurKoa8qhXkLTRhrxMzBqaDleWlRfIPE02tulU+w==", + "dependencies": { + "Microsoft.NETCore.Platforms": "3.1.0", + "Microsoft.Win32.Registry": "4.7.0", + "System.Configuration.ConfigurationManager": "4.7.0", + "System.Security.Principal.Windows": "4.7.0" + } + }, "System.Diagnostics.Tools": { "type": "Transitive", "resolved": "4.3.0", @@ -1391,6 +1556,15 @@ "System.Threading.Tasks": "4.3.0" } }, + "System.IO.FileSystem.AccessControl": { + "type": "Transitive", + "resolved": "4.7.0", + "contentHash": "vMToiarpU81LR1/KZtnT7VDPvqAZfw9oOS5nY6pPP78nGYz3COLsQH3OfzbR+SjTgltd31R6KmKklz/zDpTmzw==", + "dependencies": { + "System.Security.AccessControl": "4.7.0", + "System.Security.Principal.Windows": "4.7.0" + } + }, "System.IO.FileSystem.Primitives": { "type": "Transitive", "resolved": "4.3.0", @@ -1584,6 +1758,11 @@ "System.Runtime": "4.3.0" } }, + "System.Reflection.Metadata": { + "type": "Transitive", + "resolved": "1.6.0", + "contentHash": "COC1aiAJjCoA5GBF+QKL2uLqEBew4JsCkQmoHKbN3TlOZKa2fKLz5CpiRQKDz0RsAOEGsVKqOD5bomsXq/4STQ==" + }, "System.Reflection.Primitives": { "type": "Transitive", "resolved": "4.3.0",