-
Notifications
You must be signed in to change notification settings - Fork 24
/
receiver.go
66 lines (55 loc) · 1.47 KB
/
receiver.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
package alertsreceiver
import (
"context"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer"
"github.com/fluxninja/aperture/v2/pkg/log"
)
type alertsReceiver struct {
cfg *Config
logsConsumer consumer.Logs
// shutdown kills the long running context. Should be set in Start()
shutdown func()
}
func newReceiver(cfg *Config) (*alertsReceiver, error) {
p := &alertsReceiver{
cfg: cfg,
}
return p, nil
}
// Start creates long running context, saves shutdown function and starts run() goroutine.
func (p *alertsReceiver) Start(_ context.Context, _ component.Host) error {
ctx, cancel := context.WithCancel(context.Background())
p.shutdown = cancel
go p.run(ctx)
return nil
}
// Shutdown calls shutdown function which kills run() goroutine.
func (p *alertsReceiver) Shutdown(_ context.Context) error {
if p.shutdown != nil {
p.shutdown()
}
return nil
}
func (p *alertsReceiver) registerLogsConsumer(lc consumer.Logs) error {
if lc == nil {
return component.ErrNilNextConsumer
}
p.logsConsumer = lc
return nil
}
func (p *alertsReceiver) run(ctx context.Context) {
for {
select {
case alert := <-p.cfg.alerter.AlertsChan():
err := p.logsConsumer.ConsumeLogs(ctx, alert.AsLogs())
if err != nil {
// We do not care much about those errors. Alerts can be dropped sometimes,
// they are sent all the time anyway.
log.Autosample().Debug().Err(err).Msg("ConsumeLogs failed")
}
case <-ctx.Done():
return
}
}
}