From 6029f3176705fdc0cb63849b73bafbc6bc9214fb Mon Sep 17 00:00:00 2001 From: Braydon Kains <93549768+braydonk@users.noreply.github.com> Date: Mon, 16 Sep 2024 05:36:51 -0400 Subject: [PATCH] otelcol: fix windows event log core placement (#11051) #### Description In the CollectorSettings LoggingOptions, when the options are `zap.WrapCore`, the final core wrapped will end up running first. For this reason, the windowsEventLogCore must be the first option in the list of user supplied options, to ensure user supplied options are run before the core that writes to the Windows Event Log. Prior art: I fixed this before in #5298. #### Link to tracking issue #5297 #### Testing Testing was manual by running the Collector as a service after this change. I tried to add a unit test but could not find an effective way to do that due to all the type indirection done by the `zap` package. I was hoping to find an easy way to tell that the `windowsEventLogCore` is the first core wrapped in the options but I could not find a way to pull that off. Open to ideas if anyone can find a way. --- .../fix_windows_service_event_log_core.yaml | 25 +++++++++++++++++++ otelcol/collector_windows.go | 19 +++++++++++--- otelcol/collector_windows_service_test.go | 13 +++++++++- ...-to-file.yaml => otelcol-log-to-file.yaml} | 0 4 files changed, 52 insertions(+), 5 deletions(-) create mode 100644 .chloggen/fix_windows_service_event_log_core.yaml rename otelcol/testdata/{otel-log-to-file.yaml => otelcol-log-to-file.yaml} (100%) diff --git a/.chloggen/fix_windows_service_event_log_core.yaml b/.chloggen/fix_windows_service_event_log_core.yaml new file mode 100644 index 00000000000..7b33776084e --- /dev/null +++ b/.chloggen/fix_windows_service_event_log_core.yaml @@ -0,0 +1,25 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: bug_fix + +# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver) +component: otelcol + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Fix the Windows Event Log configuration when running the Collector as a Windows service. + +# One or more tracking issues or pull requests related to the change +issues: [5297, 11051] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: + +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [] diff --git a/otelcol/collector_windows.go b/otelcol/collector_windows.go index 3df08386bbf..7e9099e02a9 100644 --- a/otelcol/collector_windows.go +++ b/otelcol/collector_windows.go @@ -99,10 +99,7 @@ func (s *windowsService) start(elog *eventlog.Log, colErrorChannel chan error) e // only read at the time of the Run method call. To work around this, we pass the // serviceConfig as a pointer to the logging options, and then read its value // when the zap.Logger is created by the telemetry. - s.col.set.LoggingOptions = append( - s.col.set.LoggingOptions, - zap.WrapCore(withWindowsCore(elog, &s.col.serviceConfig)), - ) + s.col.set.LoggingOptions = loggingOptionsWithEventLogCore(elog, &s.col.serviceConfig, s.col.set.LoggingOptions) // col.Run blocks until receiving a SIGTERM signal, so needs to be started // asynchronously, but it will exit early if an error occurs on startup @@ -141,6 +138,20 @@ func openEventLog(serviceName string) (*eventlog.Log, error) { return elog, nil } +func loggingOptionsWithEventLogCore( + elog *eventlog.Log, + serviceConfig **service.Config, + userOptions []zap.Option, +) []zap.Option { + return append( + // The order below must be preserved - see PR #11051 + // The event log core must run *after* any user provided options, so it + // must be the first option in this list. + []zap.Option{zap.WrapCore(withWindowsCore(elog, serviceConfig))}, + userOptions..., + ) +} + var _ zapcore.Core = (*windowsEventLogCore)(nil) type windowsEventLogCore struct { diff --git a/otelcol/collector_windows_service_test.go b/otelcol/collector_windows_service_test.go index 999fa87bbbc..7b4cdb93b68 100644 --- a/otelcol/collector_windows_service_test.go +++ b/otelcol/collector_windows_service_test.go @@ -25,6 +25,17 @@ const ( // Test the collector as a Windows service. // The test assumes that the service and respective event source are already created. +// +// To test locally: +// * Build the binary: +// - make otelcorecol +// +// * Install the Windows service +// - New-Service -Name "otelcorecol" -StartupType "Manual" -BinaryPathName "${PWD}\bin\otelcorecol_windows_amd64 --config ${PWD}\examples\local\otel-config.yaml" +// +// * Create event log source +// - eventcreate.exe /t information /id 1 /l application /d "Creating event provider for 'otelcorecol'" /so otelcorecol +// // The test also must be executed with administrative privileges. func TestCollectorAsService(t *testing.T) { collector_executable, err := filepath.Abs(filepath.Join("..", "bin", "otelcorecol_windows_amd64")) @@ -58,7 +69,7 @@ func TestCollectorAsService(t *testing.T) { }, { name: "LogToFile", - configFile: filepath.Join(".", "testdata", "otel-log-to-file.yaml"), + configFile: filepath.Join(".", "testdata", "otelcol-log-to-file.yaml"), customSetup: func(t *testing.T) { // Create the folder and clean the log file if it exists programDataPath := os.Getenv("ProgramData") diff --git a/otelcol/testdata/otel-log-to-file.yaml b/otelcol/testdata/otelcol-log-to-file.yaml similarity index 100% rename from otelcol/testdata/otel-log-to-file.yaml rename to otelcol/testdata/otelcol-log-to-file.yaml