-
Notifications
You must be signed in to change notification settings - Fork 36
/
config.go
84 lines (70 loc) · 2.33 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package output
import (
"fmt"
"github.com/SumoLogic/sumologic-otel-collector/pkg/receiver/jobreceiver/output/consumer"
"github.com/SumoLogic/sumologic-otel-collector/pkg/receiver/jobreceiver/output/event"
"github.com/SumoLogic/sumologic-otel-collector/pkg/receiver/jobreceiver/output/logentries"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/adapter"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/helper"
"go.opentelemetry.io/collector/confmap"
)
const (
inputTypeEvent = "event"
inputTypeLogEntries = "log_entries"
)
var factories map[string]builderConfigFactory = map[string]builderConfigFactory{
inputTypeEvent: event.EventConfigFactory{},
inputTypeLogEntries: logentries.LogEntriesConfigFactory{},
}
func getOutputFormatFactory(format string) (builderConfigFactory, bool) {
factory, ok := factories[format]
return factory, ok
}
type builderConfigFactory interface {
CreateDefaultConfig() consumer.Builder
}
// Config for the output stage
// Dynamic configuration uses key 'type' to establish the configuration type
type Config struct {
BaseConfig adapter.BaseConfig `mapstructure:",squash"`
InputConfig helper.InputConfig `mapstructure:",squash"`
Builder consumer.Builder
}
// NewDefaultConfig builds the default output configuration
func NewDefaultConfig() Config {
return Config{
InputConfig: helper.NewInputConfig("", inputTypeEvent),
Builder: event.EventConfigFactory{}.CreateDefaultConfig(),
}
}
// Unmarshal dynamic Builder underlying Config
func (c *Config) Unmarshal(component *confmap.Conf) error {
if component == nil {
return nil
}
// Load non-dynamic parts like normal
if err := component.Unmarshal(c); err != nil {
return err
}
if !component.IsSet("type") {
return fmt.Errorf("missing required field 'type'")
}
formatInterface := component.Get("type")
format, ok := formatInterface.(string)
if !ok {
return fmt.Errorf("invalid type %T for field 'type'", formatInterface)
}
factory, ok := getOutputFormatFactory(format)
if !ok {
return fmt.Errorf("invalid value %s for field 'type'", format)
}
subComponent, err := component.Sub(format)
if err != nil {
return err
}
c.Builder = factory.CreateDefaultConfig()
if err := subComponent.Unmarshal(c.Builder, confmap.WithErrorUnused()); err != nil {
return err
}
return nil
}