forked from grafana/loki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromtail.go
88 lines (76 loc) · 1.96 KB
/
promtail.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
package promtail
import (
"sync"
"github.com/cortexproject/cortex/pkg/util"
"github.com/grafana/loki/pkg/promtail/client"
"github.com/grafana/loki/pkg/promtail/config"
"github.com/grafana/loki/pkg/promtail/server"
"github.com/grafana/loki/pkg/promtail/targets"
)
// Promtail is the root struct for Promtail...
type Promtail struct {
client client.Client
targetManagers *targets.TargetManagers
server *server.Server
stopped bool
mtx sync.Mutex
}
// New makes a new Promtail.
func New(cfg config.Config, dryRun bool) (*Promtail, error) {
if cfg.ClientConfig.URL.URL != nil {
// if a single client config is used we add it to the multiple client config for backward compatibility
cfg.ClientConfigs = append(cfg.ClientConfigs, cfg.ClientConfig)
}
var err error
var cl client.Client
if dryRun {
cl, err = client.NewLogger(cfg.ClientConfigs...)
if err != nil {
return nil, err
}
cfg.PositionsConfig.ReadOnly = true
} else {
cl, err = client.NewMulti(util.Logger, cfg.ClientConfigs...)
if err != nil {
return nil, err
}
}
promtail := &Promtail{
client: cl,
}
tms, err := targets.NewTargetManagers(promtail, util.Logger, cfg.PositionsConfig, cl, cfg.ScrapeConfig, &cfg.TargetConfig)
if err != nil {
return nil, err
}
promtail.targetManagers = tms
server, err := server.New(cfg.ServerConfig, tms)
if err != nil {
return nil, err
}
promtail.server = server
return promtail, nil
}
// Run the promtail; will block until a signal is received.
func (p *Promtail) Run() error {
p.mtx.Lock()
// if we stopped promtail before the server even started we can return without starting.
if p.stopped {
p.mtx.Unlock()
return nil
}
p.mtx.Unlock() // unlock before blocking
return p.server.Run()
}
// Shutdown the promtail.
func (p *Promtail) Shutdown() {
p.mtx.Lock()
defer p.mtx.Unlock()
p.stopped = true
if p.server != nil {
p.server.Shutdown()
}
if p.targetManagers != nil {
p.targetManagers.Stop()
}
p.client.Stop()
}