-
Notifications
You must be signed in to change notification settings - Fork 10
/
sender.go
98 lines (84 loc) · 2.67 KB
/
sender.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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
package sender
import (
"context"
"github.com/n9e/n9e-agentd/staging/datadog-agent/pkg/logs/client"
"github.com/n9e/n9e-agentd/staging/datadog-agent/pkg/logs/message"
"github.com/n9e/n9e-agentd/staging/datadog-agent/pkg/logs/metrics"
)
// Strategy should contain all logic to send logs to a remote destination
// and forward them the next stage of the pipeline.
type Strategy interface {
Send(inputChan chan *message.Message, outputChan chan *message.Message, send func([]byte) error)
Flush(ctx context.Context)
}
// Sender sends logs to different destinations.
type Sender struct {
inputChan chan *message.Message
outputChan chan *message.Message
destinations *client.Destinations
strategy Strategy
done chan struct{}
}
// NewSender returns a new sender.
func NewSender(inputChan chan *message.Message, outputChan chan *message.Message, destinations *client.Destinations, strategy Strategy) *Sender {
return &Sender{
inputChan: inputChan,
outputChan: outputChan,
destinations: destinations,
strategy: strategy,
done: make(chan struct{}),
}
}
// Start starts the sender.
func (s *Sender) Start() {
go s.run()
}
// Stop stops the sender,
// this call blocks until inputChan is flushed
func (s *Sender) Stop() {
close(s.inputChan)
<-s.done
}
// Flush sends synchronously the messages that this sender has to send.
func (s *Sender) Flush(ctx context.Context) {
s.strategy.Flush(ctx)
}
func (s *Sender) run() {
defer func() {
s.done <- struct{}{}
}()
s.strategy.Send(s.inputChan, s.outputChan, s.send)
}
// send sends a payload to multiple destinations,
// it will forever retry for the main destination unless the error is not retryable
// and only try once for additionnal destinations.
func (s *Sender) send(payload []byte) error {
for {
err := s.destinations.Main.Send(payload)
if err != nil {
metrics.DestinationErrors.Add(1)
metrics.TlmDestinationErrors.Inc()
if _, ok := err.(*client.RetryableError); ok {
// could not send the payload because of a client issue,
// let's retry
continue
}
return err
}
break
}
for _, destination := range s.destinations.Additionals {
// send in the background so that the agent does not fall behind
// for the main destination
destination.SendAsync(payload)
}
return nil
}
// shouldStopSending returns true if a component should stop sending logs.
func shouldStopSending(err error) bool {
return err == context.Canceled
}