Skip to content

Commit

Permalink
Work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
qdraw committed Dec 31, 2023
1 parent 086736f commit e6e3d37
Show file tree
Hide file tree
Showing 13 changed files with 83 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using Microsoft.Extensions.DependencyInjection;
using OpenTelemetry.Exporter;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using starsky.foundation.platform.Models;

[assembly: InternalsVisibleTo("starskytest")]
Expand All @@ -15,28 +22,48 @@ public static class OpenTelemetryExtension
public static void AddOpenTelemetryMonitoring(
this IServiceCollection services, AppSettings appSettings)
{
if ( string.IsNullOrWhiteSpace(appSettings.ApplicationInsightsConnectionString) )
if ( string.IsNullOrWhiteSpace(appSettings.OpenTelemetryEndpoint) )
{
return;
}

const string serviceName = "roll-dice";

services.builder.Logging.AddOpenTelemetry(options =>
{
options
.SetResourceBuilder(
ResourceBuilder.CreateDefault()
.AddService(serviceName))
.AddConsoleExporter();
});
builder.Services.AddOpenTelemetry()
.ConfigureResource(resource => resource.AddService(serviceName))
.WithTracing(tracing => tracing
.AddAspNetCoreInstrumentation()
.AddConsoleExporter())
.WithMetrics(metrics => metrics
.AddAspNetCoreInstrumentation()
.AddConsoleExporter());
services.AddOpenTelemetry()
.ConfigureResource(resource => resource.AddService(
serviceNamespace: appSettings.Name,
serviceName: appSettings.Name,
serviceVersion: Assembly.GetEntryAssembly()?.GetName().Version
?.ToString(),
serviceInstanceId: Environment.MachineName
).AddAttributes(new Dictionary<string, object>
{
{
"deployment.environment",
Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")
!
}
}))
.WithTracing(tracing => tracing.AddAspNetCoreInstrumentation()
.AddConsoleExporter()
.AddOtlpExporter(
o =>
{
o.Endpoint = new Uri(appSettings.OpenTelemetryEndpoint);
o.Protocol = OtlpExportProtocol.HttpProtobuf;
o.Headers = appSettings.OpenTelemetryHeader;
}
)
)
.WithMetrics(metrics =>
metrics.AddAspNetCoreInstrumentation()
.AddRuntimeInstrumentation()
.AddConsoleExporter()
.AddOtlpExporter(
o =>
{
o.Endpoint = new Uri(appSettings.OpenTelemetryEndpoint);
o.Protocol = OtlpExportProtocol.HttpProtobuf;
o.Headers = appSettings.OpenTelemetryHeader;
})
);
}
}
23 changes: 20 additions & 3 deletions starsky/starsky.foundation.webtelemetry/Helpers/SetupLogging.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using System.Diagnostics.CodeAnalysis;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using OpenTelemetry.Exporter;
using OpenTelemetry.Logs;
using starsky.foundation.platform.Interfaces;
using starsky.foundation.platform.Models;
using starsky.foundation.platform.Services;
Expand All @@ -10,14 +13,26 @@ namespace starsky.foundation.webtelemetry.Helpers
public static class SetupLogging
{
[SuppressMessage("Usage", "S4792:Make sure that this logger's configuration is safe.")]
public static void AddApplicationInsightsLogging(this IServiceCollection services, AppSettings appSettings)
public static void AddTelemetryLogging(this IServiceCollection services, AppSettings appSettings)
{
services.AddLogging(logging =>
{
logging.ClearProviders();
logging.AddConsole();
// Skip when is Development
if ( !string.IsNullOrEmpty(appSettings.OpenTelemetryEndpoint) )
{
logging.AddOpenTelemetry(builder => builder.AddOtlpExporter(
"logging",
options =>
{
options.Protocol = OtlpExportProtocol.HttpProtobuf;
options.Headers = appSettings.OpenTelemetryHeader;
options.Endpoint = new Uri(appSettings.OpenTelemetryEndpoint);
}));
}
// Remove when ApplicationInsights is phased out
if (appSettings.ApplicationInsightsLog != true ||
string.IsNullOrWhiteSpace(appSettings.ApplicationInsightsConnectionString)) return;
Expand All @@ -27,6 +42,8 @@ public static void AddApplicationInsightsLogging(this IServiceCollection service
telemetryConfiguration.ConnectionString = appSettings.ApplicationInsightsConnectionString;
},
_ => { });
// End Remove when ApplicationInsights is phased out
});

services.AddScoped<IWebLogger, WebLogger>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="6.0.0" />
<PackageReference Include="OpenTelemetry" Version="1.7.0" />
<PackageReference Include="OpenTelemetry.Api" Version="1.7.0" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.7.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.7.0" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.7.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.7.0" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.6.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.6.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.5.1" />
<PackageReference Include="System.Text.Json" Version="6.0.7" />
</ItemGroup>

Expand Down
4 changes: 3 additions & 1 deletion starsky/starsky/Properties/default-init-launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
"app__DemoUnsafeDeleteStorageFolder": "false",
"app__useSystemTrash": "true",
"[email protected]": "Administrator",
"app__ThumbnailGenerationIntervalInMinutes": "15"
"app__ThumbnailGenerationIntervalInMinutes": "15",
"_app__OpenTelemetryEndpoint": "http://localhost:9411/api/v2/spans",
"app__OpenTelemetryHeader": "api-key=test"
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion starsky/starsky/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,10 @@ public void ConfigureServices(IServiceCollection services)

// Detect Application Insights (used in next SetupDatabaseTypes)
services.AddMonitoring(_appSettings);
services.AddOpenTelemetryMonitoring(_appSettings);

// LoggerFactory
services.AddApplicationInsightsLogging(_appSettings);
services.AddTelemetryLogging(_appSettings);

var foundationDatabaseName = typeof(ApplicationDbContext).Assembly.FullName?.Split(",").FirstOrDefault();
new SetupDatabaseTypes(_appSettings,services).BuilderDb(foundationDatabaseName);
Expand Down
2 changes: 1 addition & 1 deletion starsky/starskyadmincli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ internal static async Task Main(string[] args)
var appSettings = serviceProvider.GetRequiredService<AppSettings>();

services.AddMonitoringWorkerService(appSettings, AppSettings.StarskyAppType.Geo);
services.AddApplicationInsightsLogging(appSettings);
services.AddTelemetryLogging(appSettings);

var webLogger = serviceProvider.GetRequiredService<IWebLogger>();

Expand Down
2 changes: 1 addition & 1 deletion starsky/starskydemoseedcli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static async Task Main(string[] args)
var appSettings = serviceProvider.GetRequiredService<AppSettings>();

services.AddMonitoringWorkerService(appSettings, AppSettings.StarskyAppType.DemoSeed);
services.AddApplicationInsightsLogging(appSettings);
services.AddTelemetryLogging(appSettings);

new SetupDatabaseTypes(appSettings,services).BuilderDb();
serviceProvider = services.BuildServiceProvider();
Expand Down
2 changes: 1 addition & 1 deletion starsky/starskygeocli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static async Task Main(string[] args)
var appSettings = serviceProvider.GetRequiredService<AppSettings>();

services.AddMonitoringWorkerService(appSettings, AppSettings.StarskyAppType.Geo);
services.AddApplicationInsightsLogging(appSettings);
services.AddTelemetryLogging(appSettings);

new SetupDatabaseTypes(appSettings,services).BuilderDb();
serviceProvider = services.BuildServiceProvider();
Expand Down
2 changes: 1 addition & 1 deletion starsky/starskyimportercli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static async Task Main(string[] args)
var appSettings = serviceProvider.GetRequiredService<AppSettings>();

services.AddMonitoringWorkerService(appSettings, AppSettings.StarskyAppType.Importer);
services.AddApplicationInsightsLogging(appSettings);
services.AddTelemetryLogging(appSettings);

new SetupDatabaseTypes(appSettings,services).BuilderDb();
serviceProvider = services.BuildServiceProvider();
Expand Down
2 changes: 1 addition & 1 deletion starsky/starskysynchronizecli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static async Task Main(string[] args)
var appSettings = serviceProvider.GetRequiredService<AppSettings>();

services.AddMonitoringWorkerService(appSettings, AppSettings.StarskyAppType.Sync);
services.AddApplicationInsightsLogging(appSettings);
services.AddTelemetryLogging(appSettings);

new SetupDatabaseTypes(appSettings,services).BuilderDb();
serviceProvider = services.BuildServiceProvider();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public void AddApplicationInsightsLoggingTest()
{
var testGuid = Guid.NewGuid().ToString();
IServiceCollection services = new ServiceCollection();
services.AddApplicationInsightsLogging(new AppSettings
services.AddTelemetryLogging(new AppSettings
{
ApplicationInsightsLog = true,
ApplicationInsightsConnectionString = $"InstrumentationKey={testGuid}"
Expand Down
2 changes: 1 addition & 1 deletion starsky/starskythumbnailcli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static async Task Main(string[] args)
var appSettings = serviceProvider.GetRequiredService<AppSettings>();

services.AddMonitoringWorkerService(appSettings, AppSettings.StarskyAppType.Thumbnail);
services.AddApplicationInsightsLogging(appSettings);
services.AddTelemetryLogging(appSettings);

new SetupDatabaseTypes(appSettings,services).BuilderDb();
serviceProvider = services.BuildServiceProvider();
Expand Down
2 changes: 1 addition & 1 deletion starsky/starskythumbnailmetacli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static async Task Main(string[] args)
var appSettings = serviceProvider.GetRequiredService<AppSettings>();

services.AddMonitoringWorkerService(appSettings, AppSettings.StarskyAppType.MetaThumbnail);
services.AddApplicationInsightsLogging(appSettings);
services.AddTelemetryLogging(appSettings);

new SetupDatabaseTypes(appSettings,services).BuilderDb();
serviceProvider = services.BuildServiceProvider();
Expand Down

0 comments on commit e6e3d37

Please sign in to comment.