forked from signalfx/splunk-otel-collector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
153 lines (133 loc) · 4.53 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright Splunk, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package scriptedinputsreceiver
import (
"bufio"
"errors"
"fmt"
"math"
"os"
"sort"
"time"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/decode"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/helper"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/split"
"go.opentelemetry.io/collector/component"
)
const (
defaultCollectionInterval = "60s"
// minMaxLogSize is the minimal size which can be used for buffering TCP input
minMaxLogSize = 64 * 1024
)
var availableScripts = func() []string {
var s []string
for sn := range scripts {
s = append(s, sn)
}
sort.Strings(s)
return s
}()
type Config struct {
Multiline split.Config `mapstructure:"multiline,omitempty"`
ScriptName string `mapstructure:"script_name,omitempty"`
Encoding string `mapstructure:"encoding,omitempty"`
Source string `mapstructure:"source"`
SourceType string `mapstructure:"sourcetype"`
CollectionInterval string `mapstructure:"collection_interval"`
helper.InputConfig `mapstructure:",squash"`
MaxLogSize helper.ByteSize `mapstructure:"max_log_size,omitempty"`
interval time.Duration
AddAttributes bool `mapstructure:"add_attributes,omitempty"`
}
func createDefaultConfig() *Config {
return &Config{
Encoding: "utf-8",
InputConfig: helper.NewInputConfig(typeStr, typeStr),
Multiline: split.Config{},
CollectionInterval: defaultCollectionInterval,
MaxLogSize: defaultMaxLogSize,
}
}
func (c *Config) Validate() error {
if c.ScriptName == "" {
return errors.New("'script_name' must be specified")
}
_, ok := scripts[c.ScriptName]
if !ok {
return fmt.Errorf("unsupported 'script_name' %q. must be one of %v", c.ScriptName, availableScripts)
}
if c.MaxLogSize != 0 && c.MaxLogSize < minMaxLogSize {
return fmt.Errorf("invalid value for parameter 'max_log_size', must be equal to or greater than %d bytes", minMaxLogSize)
}
if c.MaxLogSize > math.MaxInt {
return fmt.Errorf("invalid value for parameter 'max_log_size', must be less than or equal to %d bytes", math.MaxInt)
}
var err error
c.interval, err = time.ParseDuration(c.CollectionInterval)
if err != nil {
return fmt.Errorf("invalid 'collection_interval': %w", err)
}
return nil
}
// Build will build a stdoutOperator.
func (c *Config) Build(set component.TelemetrySettings) (operator.Operator, error) {
if isContainer() {
return nil, fmt.Errorf("scriped inputs receiver must be run directly on host and is not supported in container")
}
inputOperator, err := c.InputConfig.Build(set)
if err != nil {
return nil, err
}
enc, err := decode.LookupEncoding(c.Encoding)
if err != nil {
return nil, err
}
// Build multiline
var splitFunc bufio.SplitFunc
if c.Multiline.LineStartPattern == "" && c.Multiline.LineEndPattern == "" {
splitFunc = split.NoSplitFunc(int(c.MaxLogSize))
} else {
splitFunc, err = c.Multiline.Func(enc, true, int(c.MaxLogSize))
if err != nil {
return nil, err
}
}
scriptContent, ok := scripts[c.ScriptName]
if !ok {
// should have already been detected
return nil, fmt.Errorf("missing script %q", c.ScriptName)
}
return &stdoutOperator{
cfg: c,
InputOperator: inputOperator,
logger: set.Logger.Sugar(),
decoder: decode.New(enc),
splitFunc: splitFunc,
scriptContent: scriptContent,
}, nil
}
func isContainer() bool {
inContainer := os.Getpid() == 1
for _, p := range []string{
"/.dockerenv", // Mounted by dockerd when starting a container by default
"/run/.containerenv", // Mounted by podman as described here: https://github.com/containers/podman/blob/ecbb52cb478309cfd59cc061f082702b69f0f4b7/docs/source/markdown/podman-run.1.md.in#L31
} {
if _, err := os.Stat(p); err == nil {
inContainer = true
break
}
}
return inContainer
}