Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static void AddServiceControlAudit(this IHostApplicationBuilder builder,
RecordStartup(settings, configuration, persistenceConfiguration);

builder.Logging.ClearProviders();
builder.Logging.ConfigureLogging(settings.LoggingSettings.LogLevel);
builder.Logging.ConfigureLogging(settings.LoggingSettings.LogLevel, settings.InstanceName, InstanceVersion);

var services = builder.Services;
var transportSettings = settings.ToTransportSettings();
Expand Down
31 changes: 28 additions & 3 deletions src/ServiceControl.Infrastructure/LoggerUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;
using OpenTelemetry.Logs;
using OpenTelemetry.Resources;
using ServiceControl.Infrastructure.TestLogger;

[Flags]
Expand All @@ -29,7 +30,24 @@ public static bool IsLoggingTo(Loggers logger)
return (logger & ActiveLoggers) == logger;
}

public static void ConfigureLogging(this ILoggingBuilder loggingBuilder, LogLevel level)
// Configures the host logging pipeline. serviceName/serviceVersion identify this instance and are
// attached to exported OTLP telemetry as resource attributes (service.name/service.version) so logs
// are attributable instead of arriving as "unknown_service". They are mandatory: the host always knows
// its instance name and version, and the alternative (defaulting them) is what produced unattributable logs.
public static void ConfigureLogging(this ILoggingBuilder loggingBuilder, LogLevel level, string serviceName, string serviceVersion)
{
ArgumentException.ThrowIfNullOrEmpty(serviceName);
ArgumentException.ThrowIfNullOrEmpty(serviceVersion);

// CreateDefault() also reads OTEL_SERVICE_NAME/OTEL_RESOURCE_ATTRIBUTES, so operators can still enrich
// the resource with deployment-specific attributes via those environment variables.
var resourceBuilder = ResourceBuilder.CreateDefault()
.AddService(serviceName, serviceVersion: serviceVersion, autoGenerateServiceInstanceId: true);

loggingBuilder.AddProviders(level, resourceBuilder);
}

static void AddProviders(this ILoggingBuilder loggingBuilder, LogLevel level, ResourceBuilder resourceBuilder)
{
loggingBuilder.SetMinimumLevel(level);

Expand All @@ -54,7 +72,11 @@ public static void ConfigureLogging(this ILoggingBuilder loggingBuilder, LogLeve
}
if (IsLoggingTo(Loggers.Otlp))
{
loggingBuilder.AddOpenTelemetry(configure => configure.AddOtlpExporter());
loggingBuilder.AddOpenTelemetry(configure =>
{
configure.SetResourceBuilder(resourceBuilder);
configure.AddOtlpExporter();
});
}
}

Expand All @@ -64,7 +86,10 @@ static ILoggerFactory GetOrCreateLoggerFactory(LogLevel level)
{
if (!_factories.TryGetValue(level, out var factory))
{
factory = LoggerFactory.Create(configure => configure.ConfigureLogging(level));
// Static/bootstrap loggers are created from scattered call sites that have no instance identity
// (and many run before it is known). They use the default resource, which still honors
// OTEL_SERVICE_NAME/OTEL_RESOURCE_ATTRIBUTES if the operator sets them.
factory = LoggerFactory.Create(configure => configure.AddProviders(level, ResourceBuilder.CreateDefault()));
_factories[level] = factory;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ async Task InitializeServiceControl(ScenarioContext context)
EnvironmentName = Environments.Development
});
hostBuilder.Logging.ClearProviders();
hostBuilder.Logging.ConfigureLogging(LogLevel.Information);
hostBuilder.Logging.ConfigureLogging(LogLevel.Information, settings.InstanceName, "0.0.0");
hostBuilder.Logging.AddContextAppender(context);

hostBuilder.Services.AddScenarioContext(context);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace ServiceControl.Monitoring;

using System;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -26,11 +27,13 @@ namespace ServiceControl.Monitoring;

public static class HostApplicationBuilderExtensions
{
static readonly string InstanceVersion = FileVersionInfo.GetVersionInfo(typeof(HostApplicationBuilderExtensions).Assembly.Location).ProductVersion;

public static void AddServiceControlMonitoring(this IHostApplicationBuilder hostBuilder,
Func<ICriticalErrorContext, CancellationToken, Task> onCriticalError, Settings settings,
EndpointConfiguration endpointConfiguration)
{
hostBuilder.Logging.ConfigureLogging(settings.LoggingSettings.LogLevel);
hostBuilder.Logging.ConfigureLogging(settings.LoggingSettings.LogLevel, settings.InstanceName, InstanceVersion);

var services = hostBuilder.Services;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public async Task SetUp()
var hostBuilder = Host.CreateApplicationBuilder();

LoggerUtil.ActiveLoggers = Loggers.Test;
hostBuilder.Logging.ConfigureLogging(LogLevel.Information);
hostBuilder.Logging.ConfigureLogging(LogLevel.Information, "ServiceControl.Persistence.Tests", "0.0.0");

await PersistenceTestsContext.Setup(hostBuilder);

Expand Down
8 changes: 4 additions & 4 deletions src/ServiceControl/HostApplicationBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ namespace Particular.ServiceControl

static class HostApplicationBuilderExtensions
{
static readonly string InstanceVersion = FileVersionInfo.GetVersionInfo(typeof(HostApplicationBuilderExtensions).Assembly.Location).ProductVersion;

public static void AddServiceControl(this IHostApplicationBuilder hostBuilder, Settings settings, EndpointConfiguration configuration, params ReadOnlySpan<ServiceControlComponent> components)
{
ArgumentNullException.ThrowIfNull(configuration);
Expand All @@ -42,7 +44,7 @@ public static void AddServiceControl(this IHostApplicationBuilder hostBuilder, S
}

hostBuilder.Logging.ClearProviders();
hostBuilder.Logging.ConfigureLogging(settings.LoggingSettings.LogLevel);
hostBuilder.Logging.ConfigureLogging(settings.LoggingSettings.LogLevel, settings.InstanceName, InstanceVersion);

var componentSetupContext = new ComponentInstallationContext();
var serviceControlComponents = components is { Length: 0 } ? ServiceControlMainInstance.Components : components;
Expand Down Expand Up @@ -112,11 +114,9 @@ public static void AddServiceControlInstallers(this IHostApplicationBuilder host

static void RecordStartup(Settings settings, EndpointConfiguration endpointConfiguration)
{
var version = FileVersionInfo.GetVersionInfo(typeof(HostApplicationBuilderExtensions).Assembly.Location).ProductVersion;

var startupMessage = $@"
-------------------------------------------------------------
ServiceControl Version: {version}
ServiceControl Version: {InstanceVersion}
Audit Retention Period (optional): {settings.AuditRetentionPeriod}
Error Retention Period: {settings.ErrorRetentionPeriod}
Ingest Error Messages: {settings.IngestErrorMessages}
Expand Down
Loading