-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
syslogtarget.go
238 lines (206 loc) · 5.75 KB
/
syslogtarget.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package syslog
import (
"errors"
"fmt"
"net"
"strings"
"time"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/influxdata/go-syslog/v3"
"github.com/influxdata/go-syslog/v3/rfc5424"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/model/relabel"
"github.com/grafana/loki/v3/clients/pkg/promtail/api"
"github.com/grafana/loki/v3/clients/pkg/promtail/scrapeconfig"
"github.com/grafana/loki/v3/clients/pkg/promtail/targets/target"
"github.com/grafana/loki/v3/pkg/logproto"
)
var (
defaultIdleTimeout = 120 * time.Second
defaultMaxMessageLength = 8192
defaultProtocol = protocolTCP
)
// SyslogTarget listens to syslog messages.
// nolint:revive
type SyslogTarget struct {
metrics *Metrics
logger log.Logger
handler api.EntryHandler
config *scrapeconfig.SyslogTargetConfig
relabelConfig []*relabel.Config
transport Transport
messages chan message
messagesDone chan struct{}
}
type message struct {
labels model.LabelSet
message string
timestamp time.Time
}
// NewSyslogTarget configures a new SyslogTarget.
func NewSyslogTarget(
metrics *Metrics,
logger log.Logger,
handler api.EntryHandler,
relabel []*relabel.Config,
config *scrapeconfig.SyslogTargetConfig,
) (*SyslogTarget, error) {
t := &SyslogTarget{
metrics: metrics,
logger: logger,
handler: handler,
config: config,
relabelConfig: relabel,
messagesDone: make(chan struct{}),
}
switch t.transportProtocol() {
case protocolTCP:
t.transport = NewSyslogTCPTransport(
config,
t.handleMessage,
t.handleMessageError,
logger,
)
case protocolUDP:
t.transport = NewSyslogUDPTransport(
config,
t.handleMessage,
t.handleMessageError,
logger,
)
default:
return nil, fmt.Errorf("invalid transport protocol. expected 'tcp' or 'udp', got '%s'", t.transportProtocol())
}
t.messages = make(chan message)
go t.messageSender(handler.Chan())
err := t.transport.Run()
if err != nil {
return nil, err
}
return t, nil
}
func (t *SyslogTarget) handleMessageError(err error) {
var ne net.Error
if errors.As(err, &ne) && ne.Timeout() {
level.Debug(t.logger).Log("msg", "connection timed out", "err", ne)
return
}
level.Warn(t.logger).Log("msg", "error parsing syslog stream", "err", err)
t.metrics.syslogParsingErrors.Inc()
}
func (t *SyslogTarget) handleMessage(connLabels labels.Labels, msg syslog.Message) {
rfc5424Msg := msg.(*rfc5424.SyslogMessage)
if rfc5424Msg.Message == nil {
t.metrics.syslogEmptyMessages.Inc()
return
}
lb := labels.NewBuilder(connLabels)
if v := rfc5424Msg.SeverityLevel(); v != nil {
lb.Set("__syslog_message_severity", *v)
}
if v := rfc5424Msg.FacilityLevel(); v != nil {
lb.Set("__syslog_message_facility", *v)
}
if v := rfc5424Msg.Hostname; v != nil {
lb.Set("__syslog_message_hostname", *v)
}
if v := rfc5424Msg.Appname; v != nil {
lb.Set("__syslog_message_app_name", *v)
}
if v := rfc5424Msg.ProcID; v != nil {
lb.Set("__syslog_message_proc_id", *v)
}
if v := rfc5424Msg.MsgID; v != nil {
lb.Set("__syslog_message_msg_id", *v)
}
if t.config.LabelStructuredData && rfc5424Msg.StructuredData != nil {
for id, params := range *rfc5424Msg.StructuredData {
id = strings.ReplaceAll(id, "@", "_")
for name, value := range params {
key := "__syslog_message_sd_" + id + "_" + name
lb.Set(key, value)
}
}
}
processed, _ := relabel.Process(lb.Labels(), t.relabelConfig...)
filtered := make(model.LabelSet)
for _, lbl := range processed {
if strings.HasPrefix(lbl.Name, "__") {
continue
}
filtered[model.LabelName(lbl.Name)] = model.LabelValue(lbl.Value)
}
var timestamp time.Time
if t.config.UseIncomingTimestamp && rfc5424Msg.Timestamp != nil {
timestamp = *rfc5424Msg.Timestamp
} else {
timestamp = time.Now()
}
m := *rfc5424Msg.Message
if t.config.UseRFC5424Message {
fullMsg, err := rfc5424Msg.String()
if err != nil {
level.Debug(t.logger).Log("msg", "failed to convert rfc5424 message to string; using message field instead", "err", err)
} else {
m = fullMsg
}
}
t.messages <- message{filtered, m, timestamp}
}
func (t *SyslogTarget) messageSender(entries chan<- api.Entry) {
for msg := range t.messages {
entries <- api.Entry{
Labels: msg.labels,
Entry: logproto.Entry{
Timestamp: msg.timestamp,
Line: msg.message,
},
}
t.metrics.syslogEntries.Inc()
}
t.messagesDone <- struct{}{}
}
// Type returns SyslogTargetType.
func (t *SyslogTarget) Type() target.TargetType {
return target.SyslogTargetType
}
// Ready indicates whether or not the syslog target is ready to be read from.
func (t *SyslogTarget) Ready() bool {
return t.transport.Ready()
}
// DiscoveredLabels returns the set of labels discovered by the syslog target, which
// is always nil. Implements Target.
func (t *SyslogTarget) DiscoveredLabels() model.LabelSet {
return nil
}
// Labels returns the set of labels that statically apply to all log entries
// produced by the SyslogTarget.
func (t *SyslogTarget) Labels() model.LabelSet {
return t.config.Labels
}
// Details returns target-specific details.
func (t *SyslogTarget) Details() interface{} {
return map[string]string{}
}
// Stop shuts down the SyslogTarget.
func (t *SyslogTarget) Stop() error {
err := t.transport.Close()
t.transport.Wait()
close(t.messages)
// wait for all pending messages to be processed and sent to handler
<-t.messagesDone
t.handler.Stop()
return err
}
// ListenAddress returns the address SyslogTarget is listening on.
func (t *SyslogTarget) ListenAddress() net.Addr {
return t.transport.Addr()
}
func (t *SyslogTarget) transportProtocol() string {
if t.config.ListenProtocol != "" {
return t.config.ListenProtocol
}
return defaultProtocol
}