Skip to content

Commit

Permalink
[chore][receiver/windowseventlog] Expose windowseventlogreceiver conf…
Browse files Browse the repository at this point in the history
…ig on all platforms (open-telemetry#29553)

**Description:**
Discovered via
open-telemetry#29532 (comment).
The receiver configuration is not the same between Windows and the other
OSes. This is not the practice for other components and make hard to
write utilities dealing with the configuration on multiple platforms.

The change moves some types to sources that are built for all OSes while
preserving the behavior of the receiver.

**Link to tracking Issue:**
N/A

**Testing:**
Local Windows and Linux, plus CI on my fork.

**Documentation:**
N/A
  • Loading branch information
pjanotti committed Nov 30, 2023
1 parent a7ef224 commit e1af9da
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 60 deletions.
38 changes: 38 additions & 0 deletions pkg/stanza/operator/input/windows/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package windows // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/input/windows"

import (
"time"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/helper"
)

const operatorType = "windows_eventlog_input"

// NewConfig will return an event log config with default values.
func NewConfig() *Config {
return NewConfigWithID(operatorType)
}

// NewConfig will return an event log config with default values.
func NewConfigWithID(operatorID string) *Config {
return &Config{
InputConfig: helper.NewInputConfig(operatorID, operatorType),
MaxReads: 100,
StartAt: "end",
PollInterval: 1 * time.Second,
}
}

// Config is the configuration of a windows event log operator.
type Config struct {
helper.InputConfig `mapstructure:",squash"`
Channel string `mapstructure:"channel"`
MaxReads int `mapstructure:"max_reads,omitempty"`
StartAt string `mapstructure:"start_at,omitempty"`
PollInterval time.Duration `mapstructure:"poll_interval,omitempty"`
Raw bool `mapstructure:"raw,omitempty"`
ExcludeProviders []string `mapstructure:"exclude_providers,omitempty"`
}
31 changes: 1 addition & 30 deletions pkg/stanza/operator/input/windows/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,15 @@ import (
"sync"
"time"

"go.uber.org/zap"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/helper"
"go.uber.org/zap"
)

const operatorType = "windows_eventlog_input"

func init() {
operator.Register(operatorType, func() operator.Builder { return NewConfig() })
}

// NewConfig will return an event log config with default values.
func NewConfig() *Config {
return NewConfigWithID(operatorType)
}

// NewConfig will return an event log config with default values.
func NewConfigWithID(operatorID string) *Config {
return &Config{
InputConfig: helper.NewInputConfig(operatorID, operatorType),
MaxReads: 100,
StartAt: "end",
PollInterval: 1 * time.Second,
}
}

// Config is the configuration of a windows event log operator.
type Config struct {
helper.InputConfig `mapstructure:",squash"`
Channel string `mapstructure:"channel"`
MaxReads int `mapstructure:"max_reads,omitempty"`
StartAt string `mapstructure:"start_at,omitempty"`
PollInterval time.Duration `mapstructure:"poll_interval,omitempty"`
Raw bool `mapstructure:"raw,omitempty"`
ExcludeProviders []string `mapstructure:"exclude_providers,omitempty"`
}

// Build will build a windows event log operator.
func (c *Config) Build(logger *zap.SugaredLogger) (operator.Operator, error) {
inputOperator, err := c.InputConfig.Build(logger)
Expand Down
34 changes: 34 additions & 0 deletions receiver/windowseventlogreceiver/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@
package windowseventlogreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/windowseventlogreceiver"

import (
"context"
"runtime"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/receiver/receivertest"

"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/windowseventlogreceiver/internal/metadata"
)
Expand All @@ -17,3 +23,31 @@ func TestNewFactory(t *testing.T) {
require.EqualValues(t, metadata.Type, factory.Type())
})
}

func TestCreateDefaultConfig(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
require.NotNil(t, cfg, "failed to create default config")
}

func TestCreateAndShutdown(t *testing.T) {
factory := NewFactory()
defaultConfig := factory.CreateDefaultConfig()
cfg := defaultConfig.(*WindowsLogConfig) // This cast should work on all platforms.
cfg.InputConfig.Channel = "Application" // Must be explicitly set to a valid channel.

ctx := context.Background()
settings := receivertest.NewNopCreateSettings()
sink := new(consumertest.LogsSink)
receiver, err := factory.CreateLogsReceiver(ctx, settings, cfg, sink)

if runtime.GOOS != "windows" {
assert.Error(t, err)
assert.IsType(t, component.ErrDataTypeIsNotSupported, err)
assert.Nil(t, receiver)
} else {
assert.NoError(t, err)
require.NotNil(t, receiver)
require.NoError(t, receiver.Shutdown(ctx))
}
}
30 changes: 30 additions & 0 deletions receiver/windowseventlogreceiver/receiver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package windowseventlogreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/windowseventlogreceiver"

import (
"go.opentelemetry.io/collector/component"

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/consumerretry"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/adapter"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/input/windows"
)

// createDefaultConfig creates a config with type and version
func createDefaultConfig() component.Config {
return &WindowsLogConfig{
BaseConfig: adapter.BaseConfig{
Operators: []operator.Config{},
RetryOnFailure: consumerretry.NewDefaultConfig(),
},
InputConfig: *windows.NewConfig(),
}
}

// WindowsLogConfig defines configuration for the windowseventlog receiver
type WindowsLogConfig struct {
InputConfig windows.Config `mapstructure:",squash"`
adapter.BaseConfig `mapstructure:",squash"`
}
15 changes: 0 additions & 15 deletions receiver/windowseventlogreceiver/receiver_others.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import (
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/receiver"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/adapter"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/windowseventlogreceiver/internal/metadata"
)

Expand All @@ -27,14 +25,6 @@ func newFactoryAdapter() receiver.Factory {
receiver.WithLogs(createLogsReceiver, metadata.LogsStability))
}

func createDefaultConfig() component.Config {
return &WindowsLogConfig{
BaseConfig: adapter.BaseConfig{
Operators: []operator.Config{},
},
}
}

func createLogsReceiver(
_ context.Context,
_ receiver.CreateSettings,
Expand All @@ -43,8 +33,3 @@ func createLogsReceiver(
) (receiver.Logs, error) {
return nil, fmt.Errorf("windows eventlog receiver is only supported on Windows")
}

// WindowsLogConfig defines configuration for the windowseventlog receiver
type WindowsLogConfig struct {
adapter.BaseConfig `mapstructure:",squash"`
}
16 changes: 1 addition & 15 deletions receiver/windowseventlogreceiver/receiver_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ import (
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/receiver"

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/consumerretry"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/adapter"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/input/windows"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/windowseventlogreceiver/internal/metadata"
)

Expand All @@ -33,13 +31,7 @@ func (f ReceiverType) Type() component.Type {

// CreateDefaultConfig creates a config with type and version
func (f ReceiverType) CreateDefaultConfig() component.Config {
return &WindowsLogConfig{
BaseConfig: adapter.BaseConfig{
Operators: []operator.Config{},
RetryOnFailure: consumerretry.NewDefaultConfig(),
},
InputConfig: *windows.NewConfig(),
}
return createDefaultConfig()
}

// BaseConfig gets the base config from config, for now
Expand All @@ -51,9 +43,3 @@ func (f ReceiverType) BaseConfig(cfg component.Config) adapter.BaseConfig {
func (f ReceiverType) InputConfig(cfg component.Config) operator.Config {
return operator.NewConfig(&cfg.(*WindowsLogConfig).InputConfig)
}

// WindowsLogConfig defines configuration for the windowseventlog receiver
type WindowsLogConfig struct {
InputConfig windows.Config `mapstructure:",squash"`
adapter.BaseConfig `mapstructure:",squash"`
}

0 comments on commit e1af9da

Please sign in to comment.