Skip to content

Commit

Permalink
otelcol: fix windows event log core placement (#11051)
Browse files Browse the repository at this point in the history
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->
#### 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.

<!-- Issue number if applicable -->
#### Link to tracking issue
#5297 

<!--Describe what testing was performed and which tests were added.-->
#### 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.
  • Loading branch information
braydonk authored Sep 16, 2024
1 parent 3b50b38 commit 6029f31
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 5 deletions.
25 changes: 25 additions & 0 deletions .chloggen/fix_windows_service_event_log_core.yaml
Original file line number Diff line number Diff line change
@@ -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: []
19 changes: 15 additions & 4 deletions otelcol/collector_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
13 changes: 12 additions & 1 deletion otelcol/collector_windows_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down Expand Up @@ -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")
Expand Down
File renamed without changes.

0 comments on commit 6029f31

Please sign in to comment.