forked from open-telemetry/opentelemetry-collector-contrib
/
procpipe.go
281 lines (234 loc) · 6.87 KB
/
procpipe.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Copyright The OpenTelemetry Authors
//
// 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 procpipe // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/scriptreceiver/procpipe"
import (
"bufio"
"context"
"fmt"
"io"
"strconv"
"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"
)
const (
operatorType = "procpipe_input"
// minMaxLogSize is the minimal size which can be used for buffering
// TCP input
minMaxLogSize = 64 * 1024
// DefaultMaxLogSize is the max buffer sized used
// if MaxLogSize is not set
DefaultMaxLogSize = 1024 * 1024
DefaultIntervalSeconds = 60
)
func init() {
operator.Register(operatorType, func() operator.Builder { return NewConfig() })
}
// NewConfig creates a new stdin input config with default values
func NewConfig() *Config {
return &Config{
InputConfig: helper.NewInputConfig(operatorType, operatorType),
BaseConfig: BaseConfig{
Multiline: helper.NewMultilineConfig(),
Encoding: helper.NewEncodingConfig(),
},
}
}
// Config is the configuration of a stdin input operator.
type Config struct {
helper.InputConfig `mapstructure:",squash"`
BaseConfig `mapstructure:",squash"`
}
// BaseConfig is the detailed configuration of a tcp input operator.
type BaseConfig struct {
ExecFile string `mapstructure:"exec_file,omitempty"`
MaxLogSize helper.ByteSize `mapstructure:"max_log_size,omitempty"`
AddAttributes bool `mapstructure:"add_attributes,omitempty"`
Encoding helper.EncodingConfig `mapstructure:",squash,omitempty"`
Multiline helper.MultilineConfig `mapstructure:"multiline,omitempty"`
Source string `mapstructure:"source"`
SourceType string `mapstructure:"sourcetype"`
Interval string `mapstructure:"interval"`
}
// Build will build a stdin input operator.
func (c *Config) Build(logger *zap.SugaredLogger) (operator.Operator, error) {
if c.ExecFile == "" {
return nil, fmt.Errorf("'exec_file' must be specified")
}
inputOperator, err := c.InputConfig.Build(logger)
if err != nil {
return nil, err
}
// If MaxLogSize not set, set sane default
if c.MaxLogSize == 0 {
c.MaxLogSize = DefaultMaxLogSize
}
if c.Interval == "" {
c.Interval = strconv.Itoa(DefaultIntervalSeconds)
}
if c.MaxLogSize < minMaxLogSize {
return nil, fmt.Errorf(
"invalid value for parameter 'max_log_size', must be equal to or greater than %d bytes", minMaxLogSize,
)
}
encoding, err := c.Encoding.Build()
if err != nil {
return nil, err
}
// Build multiline
var splitFunc bufio.SplitFunc
if c.Multiline.LineStartPattern == "" && c.Multiline.LineEndPattern == "" {
splitFunc = helper.SplitNone(int(c.MaxLogSize))
} else {
splitFunc, err = c.Multiline.Build(encoding.Encoding, true, false, false, nil, int(c.MaxLogSize))
if err != nil {
return nil, err
}
}
return &Input{
baseConfig: c.BaseConfig,
InputOperator: inputOperator,
logger: logger,
MaxLogSize: int(c.MaxLogSize),
encoding: encoding,
splitFunc: splitFunc,
}, nil
}
// Input is an operator that reads input from stdin
type Input struct {
baseConfig BaseConfig
helper.InputOperator
MaxLogSize int
logger *zap.SugaredLogger
wg sync.WaitGroup
cancelAll context.CancelFunc
encoding helper.Encoding
splitFunc bufio.SplitFunc
}
/*
type PipeWriter struct {
ctx context.Context
input *Input
}
*/
func createTicker(intervalStr string) (*time.Ticker, error) {
interval, err := time.ParseDuration(intervalStr)
if err != nil {
return nil, err
}
return time.NewTicker(interval), nil
}
// Start will start generating log entries.
func (g *Input) Start(_ operator.Persister) error {
ctx, cancelAll := context.WithCancel(context.Background())
g.cancelAll = cancelAll
ticker, err := createTicker(g.baseConfig.Interval)
if err != nil {
// TODO: move ticker verification to Start() so that we cannot error here.
return err
}
go func() {
for {
_, cancelCycle := context.WithCancel(ctx)
err := g.beginCycle(ctx)
if err != nil {
g.logger.Errorf("Error running script: %v", err)
}
select {
case <-ctx.Done():
cancelCycle()
return
case <-ticker.C:
cancelCycle()
g.wg.Wait()
}
}
}()
return nil
}
func (g *Input) beginCycle(ctx context.Context) error {
stdOutReader, stdOutWriter := io.Pipe()
commander, err := NewCommander(g.logger.Desugar(), g.baseConfig.ExecFile, stdOutWriter)
if err != nil {
return err
}
if err := commander.Start(ctx); err != nil {
return err
}
g.wg.Add(2)
readerCtx, cancelReader := context.WithCancel(ctx)
go func() {
defer g.wg.Done()
select {
case <-commander.Done():
g.logger.Debug("Script finished", zap.String("exec_file", g.baseConfig.ExecFile))
// Close the write pipe. This will result in subsequent read by scanner to return EOF and finish
// the goroutine that processes the script output.
err := stdOutWriter.Close()
if err != nil {
return
}
case <-ctx.Done():
g.logger.Info("Script run too long. Stopping.", zap.String("exec_file", g.baseConfig.ExecFile))
err := commander.Stop(context.Background())
if err != nil {
return
}
err2 := stdOutWriter.Close()
if err2 != nil {
return
}
}
cancelReader()
}()
go g.readOutput(readerCtx, stdOutReader)
return nil
}
func (g *Input) readOutput(ctx context.Context, r io.Reader) {
defer g.wg.Done()
buf := make([]byte, 0, g.MaxLogSize)
scanner := bufio.NewScanner(r)
scanner.Buffer(buf, g.MaxLogSize)
scanner.Split(g.splitFunc)
for scanner.Scan() {
decoded, err := g.encoding.Decode(scanner.Bytes())
if err != nil {
g.Errorw("Failed to decode data", zap.Error(err))
continue
}
entry, err := g.NewEntry(string(decoded))
if err != nil {
g.Errorw("Failed to create entry", zap.Error(err))
continue
}
if g.baseConfig.Source != "" {
entry.AddAttribute("com.splunk.source", g.baseConfig.Source)
}
if g.baseConfig.SourceType != "" {
entry.AddAttribute("com.splunk.sourcetype", g.baseConfig.SourceType)
}
g.Write(ctx, entry)
}
if err := scanner.Err(); err != nil {
g.Errorw("Scanner error", zap.Error(err))
}
}
// Stop will stop generating logs.
func (g *Input) Stop() error {
g.cancelAll()
g.wg.Wait()
return nil
}