forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prospector.go
78 lines (66 loc) · 1.78 KB
/
prospector.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
package stdin
import (
"fmt"
"github.com/elastic/beats/filebeat/channel"
"github.com/elastic/beats/filebeat/harvester"
"github.com/elastic/beats/filebeat/input/file"
"github.com/elastic/beats/filebeat/prospector/log"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
)
// Prospector is a prospector for stdin
type Prospector struct {
harvester *log.Harvester
started bool
cfg *common.Config
outlet channel.Outleter
registry *harvester.Registry
}
// NewStdin creates a new stdin prospector
// This prospector contains one harvester which is reading from stdin
func NewProspector(cfg *common.Config, outlet channel.OutleterFactory) (*Prospector, error) {
out, err := outlet(cfg)
if err != nil {
return nil, err
}
p := &Prospector{
started: false,
cfg: cfg,
outlet: out,
registry: harvester.NewRegistry(),
}
p.harvester, err = p.createHarvester(file.State{Source: "-"})
if err != nil {
return nil, fmt.Errorf("Error initializing stdin harvester: %v", err)
}
return p, nil
}
// Run runs the prospector
func (p *Prospector) Run() {
// Make sure stdin harvester is only started once
if !p.started {
err := p.harvester.Setup()
if err != nil {
logp.Err("Error setting up stdin harvester: %s", err)
return
}
p.registry.Start(p.harvester)
p.started = true
}
}
// createHarvester creates a new harvester instance from the given state
func (p *Prospector) createHarvester(state file.State) (*log.Harvester, error) {
// Each harvester gets its own copy of the outlet
h, err := log.NewHarvester(
p.cfg,
state, nil, nil,
p.outlet,
)
return h, err
}
// Wait waits for completion of the prospector.
func (p *Prospector) Wait() {}
// Stop stops the prospector.
func (p *Prospector) Stop() {
p.outlet.Close()
}