forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logs.go
320 lines (276 loc) · 8.77 KB
/
logs.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package cloudflarereceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/cloudflarereceiver"
import (
"bytes"
"compress/gzip"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net"
"net/http"
"strconv"
"sync"
"time"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/plog"
rcvr "go.opentelemetry.io/collector/receiver"
"go.uber.org/zap"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/cloudflarereceiver/internal/metadata"
)
type logsReceiver struct {
logger *zap.Logger
cfg *LogsConfig
server *http.Server
consumer consumer.Logs
wg *sync.WaitGroup
id component.ID // ID of the receiver component
telemetrySettings component.TelemetrySettings
}
const secretHeaderName = "X-CF-Secret"
var receiverScopeName = "otelcol/" + metadata.Type.String()
func newLogsReceiver(params rcvr.CreateSettings, cfg *Config, consumer consumer.Logs) (*logsReceiver, error) {
recv := &logsReceiver{
cfg: &cfg.Logs,
consumer: consumer,
logger: params.Logger,
wg: &sync.WaitGroup{},
telemetrySettings: params.TelemetrySettings,
id: params.ID,
}
recv.server = &http.Server{
Handler: http.HandlerFunc(recv.handleRequest),
ReadHeaderTimeout: 20 * time.Second,
}
if recv.cfg.TLS != nil {
tlsConfig, err := recv.cfg.TLS.LoadTLSConfig(context.Background())
if err != nil {
return nil, err
}
recv.server.TLSConfig = tlsConfig
}
return recv, nil
}
func (l *logsReceiver) Start(ctx context.Context, host component.Host) error {
return l.startListening(ctx, host)
}
func (l *logsReceiver) Shutdown(ctx context.Context) error {
l.logger.Debug("Shutting down server")
err := l.server.Shutdown(ctx)
if err != nil {
return err
}
l.logger.Debug("Waiting for shutdown to complete.")
l.wg.Wait()
return nil
}
func (l *logsReceiver) startListening(ctx context.Context, _ component.Host) error {
l.logger.Debug("starting receiver HTTP server")
// We use l.server.Serve* over l.server.ListenAndServe*
// So that we can catch and return errors relating to binding to network interface on start.
var lc net.ListenConfig
listener, err := lc.Listen(ctx, "tcp", l.cfg.Endpoint)
if err != nil {
return err
}
l.wg.Add(1)
go func() {
defer l.wg.Done()
if l.cfg.TLS != nil {
l.logger.Debug("Starting ServeTLS",
zap.String("address", l.cfg.Endpoint),
zap.String("certfile", l.cfg.TLS.CertFile),
zap.String("keyfile", l.cfg.TLS.KeyFile))
err := l.server.ServeTLS(listener, l.cfg.TLS.CertFile, l.cfg.TLS.KeyFile)
l.logger.Debug("ServeTLS done")
if !errors.Is(err, http.ErrServerClosed) {
l.logger.Error("ServeTLS failed", zap.Error(err))
l.telemetrySettings.ReportStatus(component.NewFatalErrorEvent(err))
}
} else {
l.logger.Debug("Starting Serve",
zap.String("address", l.cfg.Endpoint))
err := l.server.Serve(listener)
l.logger.Debug("Serve done")
if !errors.Is(err, http.ErrServerClosed) {
l.logger.Error("Serve failed", zap.Error(err))
l.telemetrySettings.ReportStatus(component.NewFatalErrorEvent(err))
}
}
}()
return nil
}
func (l *logsReceiver) handleRequest(rw http.ResponseWriter, req *http.Request) {
if l.cfg.Secret != "" {
secretHeader := req.Header.Get(secretHeaderName)
if secretHeader == "" {
rw.WriteHeader(http.StatusUnauthorized)
l.logger.Debug("Got payload with no Secret when it was specified in config, dropping...")
return
} else if secretHeader != l.cfg.Secret {
rw.WriteHeader(http.StatusUnauthorized)
l.logger.Debug("Got payload with invalid Secret, dropping...")
return
}
}
var payload []byte
if req.Header.Get("Content-Encoding") == "gzip" {
reader, err := gzip.NewReader(req.Body)
if err != nil {
rw.WriteHeader(http.StatusUnprocessableEntity)
l.logger.Debug("Got payload with gzip, but failed to read", zap.Error(err))
return
}
defer reader.Close()
// Read the decompressed response body
payload, err = io.ReadAll(reader)
if err != nil {
rw.WriteHeader(http.StatusUnprocessableEntity)
l.logger.Debug("Got payload with gzip, but failed to read", zap.Error(err))
return
}
} else {
var err error
payload, err = io.ReadAll(req.Body)
if err != nil {
rw.WriteHeader(http.StatusUnprocessableEntity)
l.logger.Debug("Failed to read alerts payload", zap.Error(err), zap.String("remote", req.RemoteAddr))
return
}
}
if string(payload) == "test" {
l.logger.Info("Received test request from Cloudflare")
rw.WriteHeader(http.StatusOK)
return
}
logs, err := parsePayload(payload)
if err != nil {
rw.WriteHeader(http.StatusUnprocessableEntity)
l.logger.Error("Failed to convert cloudflare request payload to maps", zap.Error(err))
return
}
if err := l.consumer.ConsumeLogs(req.Context(), l.processLogs(pcommon.NewTimestampFromTime(time.Now()), logs)); err != nil {
rw.WriteHeader(http.StatusInternalServerError)
l.logger.Error("Failed to consumer alert as log", zap.Error(err))
return
}
rw.WriteHeader(http.StatusOK)
}
func parsePayload(payload []byte) ([]map[string]any, error) {
lines := bytes.Split(payload, []byte("\n"))
logs := make([]map[string]any, 0, len(lines))
for _, line := range lines {
if len(line) == 0 {
continue
}
var log map[string]any
err := json.Unmarshal(line, &log)
if err != nil {
return logs, err
}
logs = append(logs, log)
}
return logs, nil
}
func (l *logsReceiver) processLogs(now pcommon.Timestamp, logs []map[string]any) plog.Logs {
pLogs := plog.NewLogs()
// Group logs by ZoneName field if it was configured so it can be used as a resource attribute
groupedLogs := make(map[string][]map[string]any)
for _, log := range logs {
zone := ""
if v, ok := log["ZoneName"]; ok {
if stringV, ok := v.(string); ok {
zone = stringV
}
}
groupedLogs[zone] = append(groupedLogs[zone], log)
}
for zone, logGroup := range groupedLogs {
resourceLogs := pLogs.ResourceLogs().AppendEmpty()
if zone != "" {
resource := resourceLogs.Resource()
resource.Attributes().PutStr("cloudflare.zone", zone)
}
scopeLogs := resourceLogs.ScopeLogs().AppendEmpty()
scopeLogs.Scope().SetName(receiverScopeName)
for _, log := range logGroup {
logRecord := scopeLogs.LogRecords().AppendEmpty()
logRecord.SetObservedTimestamp(now)
if v, ok := log[l.cfg.TimestampField]; ok {
if stringV, ok := v.(string); ok {
ts, err := time.Parse(time.RFC3339, stringV)
if err != nil {
l.logger.Warn(fmt.Sprintf("unable to parse %s", l.cfg.TimestampField), zap.Error(err), zap.String("value", stringV))
} else {
logRecord.SetTimestamp(pcommon.NewTimestampFromTime(ts))
}
} else {
l.logger.Warn(fmt.Sprintf("unable to parse %s", l.cfg.TimestampField), zap.Any("value", v))
}
}
if v, ok := log["EdgeResponseStatus"]; ok {
sev := plog.SeverityNumberUnspecified
switch v := v.(type) {
case string:
intV, err := strconv.ParseInt(v, 10, 64)
if err != nil {
l.logger.Warn("unable to parse EdgeResponseStatus", zap.Error(err), zap.String("value", v))
} else {
sev = severityFromStatusCode(intV)
}
case int64:
sev = severityFromStatusCode(v)
case float64:
sev = severityFromStatusCode(int64(v))
}
if sev != plog.SeverityNumberUnspecified {
logRecord.SetSeverityNumber(sev)
logRecord.SetSeverityText(sev.String())
}
}
attrs := logRecord.Attributes()
for field, attribute := range l.cfg.Attributes {
if v, ok := log[field]; ok {
switch v := v.(type) {
case string:
attrs.PutStr(attribute, v)
case int:
attrs.PutInt(attribute, int64(v))
case int64:
attrs.PutInt(attribute, v)
case float64:
attrs.PutDouble(attribute, v)
case bool:
attrs.PutBool(attribute, v)
default:
l.logger.Warn("unable to translate field to attribute, unsupported type", zap.String("field", field), zap.Any("value", v), zap.String("type", fmt.Sprintf("%T", v)))
}
}
}
err := logRecord.Body().SetEmptyMap().FromRaw(log)
if err != nil {
l.logger.Warn("unable to set body", zap.Error(err))
}
}
}
return pLogs
}
// severityFromStatusCode translates HTTP status code to OpenTelemetry severity number.
func severityFromStatusCode(statusCode int64) plog.SeverityNumber {
switch {
case statusCode < 300:
return plog.SeverityNumberInfo
case statusCode < 400:
return plog.SeverityNumberInfo2
case statusCode < 500:
return plog.SeverityNumberWarn
case statusCode < 600:
return plog.SeverityNumberError
default:
return plog.SeverityNumberUnspecified
}
}