Skip to content
Open
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
8 changes: 8 additions & 0 deletions src/ServiceControl.Audit/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.Reflection;
using Microsoft.Extensions.Logging;
using ServiceControl.Audit.Infrastructure.Hosting;
Expand All @@ -13,6 +14,13 @@
{
ExeConfiguration.PopulateAppSettings(Assembly.GetExecutingAssembly());

// Establish the telemetry identity once, before any logger is created, so every logger — including the
// static bootstrap loggers — attributes exported OTLP logs to this instance. Mirrors the InstanceName
// resolution in Settings (InternalQueueName is the legacy fallback for the instance name).
var instanceName = SettingsReader.Read(Settings.SettingsRootNamespace, "InstanceName",

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@WilliamBZA @dvdstelt this is really needed to ensure OTel related attributes are set.

The version could be extracted in the LoggerUtil.Initialize as that version is the same for all SC assemblies in the build.

However, the name is not, unfortnately although very similar for all 3 instances SettingsReader can be used in LoggerUtil but not Settings which is instance specific.

Any refactorings you can think of to maybe this a bit less fugly?

SettingsReader.Read(Settings.SettingsRootNamespace, "InternalQueueName", Settings.DEFAULT_INSTANCE_NAME));
LoggerUtil.Initialize(instanceName, FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).ProductVersion);

var loggingSettings = new LoggingSettings(Settings.SettingsRootNamespace);
LoggingConfigurator.ConfigureLogging(loggingSettings);
logger = LoggerUtil.CreateStaticLogger(typeof(Program));
Expand Down
25 changes: 24 additions & 1 deletion 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 @@ -24,6 +25,24 @@ public static class LoggerUtil

public static string SeqAddress { private get; set; }

// Telemetry resource attached to exported OTLP logs (service.name/service.version/service.instance.id).
// Set once at process startup via Initialize() — before any logger is created — so both the host pipeline
// and the static bootstrap loggers (CreateStaticLogger) share a single instance identity. Defaults to
// CreateDefault() (which still honors OTEL_SERVICE_NAME/OTEL_RESOURCE_ATTRIBUTES) for the rare logger
// created before Initialize runs.
static ResourceBuilder serviceResourceBuilder = ResourceBuilder.CreateDefault();

public static void Initialize(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.
serviceResourceBuilder = ResourceBuilder.CreateDefault()
.AddService(serviceName, serviceVersion: serviceVersion, autoGenerateServiceInstanceId: true);
}

public static bool IsLoggingTo(Loggers logger)
{
return (logger & ActiveLoggers) == logger;
Expand Down Expand Up @@ -54,7 +73,11 @@ public static void ConfigureLogging(this ILoggingBuilder loggingBuilder, LogLeve
}
if (IsLoggingTo(Loggers.Otlp))
{
loggingBuilder.AddOpenTelemetry(configure => configure.AddOtlpExporter());
loggingBuilder.AddOpenTelemetry(configure =>
{
configure.SetResourceBuilder(serviceResourceBuilder);
configure.AddOtlpExporter();
});
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/ServiceControl.Monitoring/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.Reflection;
using Microsoft.Extensions.Logging;
using ServiceControl.Configuration;
Expand All @@ -11,6 +12,11 @@
{
ExeConfiguration.PopulateAppSettings(Assembly.GetExecutingAssembly());

// Establish the telemetry identity once, before any logger is created, so every logger — including the
// static bootstrap loggers — attributes exported OTLP logs to this instance.
var instanceName = SettingsReader.Read(Settings.SettingsRootNamespace, "InstanceName", Settings.DEFAULT_INSTANCE_NAME);
LoggerUtil.Initialize(instanceName, FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).ProductVersion);

var loggingSettings = new LoggingSettings(Settings.SettingsRootNamespace);
LoggingConfigurator.ConfigureLogging(loggingSettings);
logger = LoggerUtil.CreateStaticLogger(typeof(Program));
Expand Down
8 changes: 8 additions & 0 deletions src/ServiceControl/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.Reflection;
using Microsoft.Extensions.Logging;
using Particular.ServiceControl.Hosting;
Expand All @@ -13,6 +14,13 @@
{
ExeConfiguration.PopulateAppSettings(Assembly.GetExecutingAssembly());

// Establish the telemetry identity once, before any logger is created, so every logger — including the
// static bootstrap loggers — attributes exported OTLP logs to this instance. Mirrors the InstanceName
// resolution in Settings (InternalQueueName is the legacy fallback for the instance name).
var instanceName = SettingsReader.Read(Settings.SettingsRootNamespace, "InstanceName",
SettingsReader.Read(Settings.SettingsRootNamespace, "InternalQueueName", Settings.DEFAULT_INSTANCE_NAME));
LoggerUtil.Initialize(instanceName, FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).ProductVersion);

var loggingSettings = new LoggingSettings(Settings.SettingsRootNamespace);
LoggingConfigurator.ConfigureLogging(loggingSettings);
logger = LoggerUtil.CreateStaticLogger(typeof(Program));
Expand Down
Loading