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 97c8647
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 45 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
Loading

0 comments on commit 97c8647

Please sign in to comment.