-
Notifications
You must be signed in to change notification settings - Fork 23
/
ingest_stdin.go
118 lines (102 loc) · 3.03 KB
/
ingest_stdin.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
/*
* Copyright (C) 2023 IBM, 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 ingest
import (
"bufio"
"os"
"github.com/netobserv/flowlogs-pipeline/pkg/api"
"github.com/netobserv/flowlogs-pipeline/pkg/config"
"github.com/netobserv/flowlogs-pipeline/pkg/operational"
"github.com/netobserv/flowlogs-pipeline/pkg/pipeline/decode"
pUtils "github.com/netobserv/flowlogs-pipeline/pkg/pipeline/utils"
"github.com/sirupsen/logrus"
)
const (
stdinChannelSize = 1000
)
var slog = logrus.WithField("component", "ingest.Stdin")
type ingestStdin struct {
in chan string
eof chan struct{}
exitChan <-chan struct{}
metrics *metrics
decoder decode.Decoder
}
// Ingest ingests entries from stdin
func (s *ingestStdin) Ingest(out chan<- config.GenericMap) {
slog.Debugf("entering ingestStdin.Ingest")
s.metrics.createOutQueueLen(out)
go s.getStdinInput()
// process log lines received by stdin
s.processLogLines(out)
}
func (s *ingestStdin) getStdinInput() {
scanner := bufio.NewScanner(os.Stdin)
// Loop to read lines from stdin until an error or EOF is encountered
for scanner.Scan() {
s.in <- scanner.Text()
}
// Check for errors
if err := scanner.Err(); err != nil {
slog.WithError(err).Errorf("Error reading standard input")
}
close(s.eof)
}
func (s *ingestStdin) processLogLines(out chan<- config.GenericMap) {
for {
select {
case <-s.exitChan:
slog.Debugf("exiting ingestStdin because of signal")
return
case <-s.eof:
slog.Debugf("exiting ingestStdin because of EOF")
return
case line := <-s.in:
s.processRecord(out, line)
}
}
}
func (s *ingestStdin) processRecord(out chan<- config.GenericMap, line string) {
slog.Debugf("Decoding %s", line)
decoded, err := s.decoder.Decode([]byte(line))
if err != nil {
slog.WithError(err).Warnf("ignoring line %v", line)
s.metrics.error("Ignoring line")
return
}
s.metrics.flowsProcessed.Inc()
out <- decoded
}
// NewIngestStdin create a new ingester
func NewIngestStdin(opMetrics *operational.Metrics, params config.StageParam) (Ingester, error) {
slog.Debugf("Entering NewIngestStdin")
in := make(chan string, stdinChannelSize)
eof := make(chan struct{})
metrics := newMetrics(opMetrics, params.Name, params.Ingest.Type, func() int { return len(in) })
decoderParams := api.Decoder{Type: api.DecoderName("JSON")}
decoder, err := decode.GetDecoder(decoderParams)
if err != nil {
return nil, err
}
return &ingestStdin{
exitChan: pUtils.ExitChannel(),
in: in,
eof: eof,
metrics: metrics,
decoder: decoder,
}, nil
}