Skip to content
This repository has been archived by the owner on Nov 1, 2023. It is now read-only.

Commit

Permalink
Improve AppInsights setup
Browse files Browse the repository at this point in the history
  • Loading branch information
Porges authored Nov 2, 2022
1 parent e6dd1ac commit bd9500d
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 32 deletions.
3 changes: 1 addition & 2 deletions src/ApiService/ApiService/ApiService.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<ItemGroup>
<PackageReference Include="Azure.ResourceManager.Monitor" Version="1.0.0-beta.2" />
<PackageReference Include="Faithlife.Utility" Version="0.12.2" />
<PackageReference Include="Microsoft.ApplicationInsights.DependencyCollector" Version="2.21.0" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.21.0" />
<PackageReference Include="Semver" Version="2.1.0" />
<PackageReference Include="Azure.Security.KeyVault.Secrets" Version="4.3.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Storage" Version="5.0.0" />
Expand All @@ -20,7 +20,6 @@
<PackageReference Include="Microsoft.Azure.Management.OperationalInsights" Version="0.24.0-preview" />
<PackageReference Include="Microsoft.Azure.Management.Monitor" Version="0.28.0-preview" />

<PackageReference Include="Microsoft.Extensions.Logging.ApplicationInsights" Version="2.21.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.3.0" OutputItemType="Analyzer" />
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.6.0" />
<PackageReference Include="Azure.Data.Tables" Version="12.5.0" />
Expand Down
8 changes: 4 additions & 4 deletions src/ApiService/ApiService/Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> tags, string? caller) {
Expand Down Expand Up @@ -366,12 +366,12 @@ public interface ILogSinks {
public class LogSinks : ILogSinks {
private readonly List<ILog> _loggers;

public LogSinks(IServiceConfig config, TelemetryConfiguration telemetryConfiguration) {
public LogSinks(IServiceConfig config, TelemetryClient telemetryClient) {
_loggers = new List<ILog>();
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}"),
}
Expand Down
19 changes: 5 additions & 14 deletions src/ApiService/ApiService/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -110,32 +112,21 @@ public static async Async.Task Main() {
.AddScoped<INodeMessageOperations, NodeMessageOperations>()
.AddScoped<ISubnet, Subnet>()
.AddScoped<IAutoScaleOperations, AutoScaleOperations>()

.AddSingleton<TelemetryConfiguration>(provider => {
var config = provider.GetRequiredService<IServiceConfig>();
return new() {
ConnectionString = $"InstrumentationKey={config.ApplicationInsightsInstrumentationKey}",
};
.AddApplicationInsightsTelemetry(options => {
options.ConnectionString = $"InstrumentationKey={configuration.ApplicationInsightsInstrumentationKey}";
})
.AddSingleton<GraphServiceClient>(new GraphServiceClient(new DefaultAzureCredential()))
.AddSingleton<DependencyTrackingTelemetryModule>()
.AddSingleton<ICreds, Creds>()
.AddSingleton<EntityConverter>()
.AddSingleton<IServiceConfig, ServiceConfiguration>()
.AddSingleton<IServiceConfig>(configuration)
.AddSingleton<IStorage, Storage>()
.AddSingleton<ILogSinks, LogSinks>()
.AddHttpClient()
.AddMemoryCache();
})
.Build();

// Set up Application Insights dependency tracking:
{
var telemetryConfig = host.Services.GetRequiredService<TelemetryConfiguration>();
var module = host.Services.GetRequiredService<DependencyTrackingTelemetryModule>();
module.Initialize(telemetryConfig);
}

// Initialize expected Storage tables:
await SetupStorage(
host.Services.GetRequiredService<IStorage>(),
Expand Down
203 changes: 191 additions & 12 deletions src/ApiService/ApiService/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -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, )",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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": {
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit bd9500d

Please sign in to comment.