forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.go
More file actions
259 lines (230 loc) · 9.92 KB
/
extension.go
File metadata and controls
259 lines (230 loc) · 9.92 KB
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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package awslogsencodingextension // import "github.com/open-telemetry/opentelemetry-collector-contrib/extension/encoding/awslogsencodingextension"
import (
"bytes"
"context"
"fmt"
"io"
"sync"
"github.com/klauspost/compress/gzip"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/extension"
"go.opentelemetry.io/collector/featuregate"
"go.opentelemetry.io/collector/pdata/plog"
"go.uber.org/zap"
"github.com/open-telemetry/opentelemetry-collector-contrib/extension/encoding"
"github.com/open-telemetry/opentelemetry-collector-contrib/extension/encoding/awslogsencodingextension/internal/constants"
awsunmarshaler "github.com/open-telemetry/opentelemetry-collector-contrib/extension/encoding/awslogsencodingextension/internal/unmarshaler"
"github.com/open-telemetry/opentelemetry-collector-contrib/extension/encoding/awslogsencodingextension/internal/unmarshaler/cloudtraillog"
elbaccesslogs "github.com/open-telemetry/opentelemetry-collector-contrib/extension/encoding/awslogsencodingextension/internal/unmarshaler/elb-access-log"
networkfirewall "github.com/open-telemetry/opentelemetry-collector-contrib/extension/encoding/awslogsencodingextension/internal/unmarshaler/network-firewall-log"
s3accesslog "github.com/open-telemetry/opentelemetry-collector-contrib/extension/encoding/awslogsencodingextension/internal/unmarshaler/s3-access-log"
subscriptionfilter "github.com/open-telemetry/opentelemetry-collector-contrib/extension/encoding/awslogsencodingextension/internal/unmarshaler/subscription-filter"
vpcflowlog "github.com/open-telemetry/opentelemetry-collector-contrib/extension/encoding/awslogsencodingextension/internal/unmarshaler/vpc-flow-log"
"github.com/open-telemetry/opentelemetry-collector-contrib/extension/encoding/awslogsencodingextension/internal/unmarshaler/waf"
)
const (
gzipEncoding = "gzip"
bytesEncoding = "bytes"
parquetEncoding = "parquet"
)
var (
vpcFlowStartISO8601FormatFeatureGate *featuregate.Gate
cloudTrailUserIdentityPrefixFeatureGate *featuregate.Gate
)
func init() {
vpcFlowStartISO8601FormatFeatureGate = featuregate.GlobalRegistry().MustRegister(
constants.VPCFlowStartISO8601FormatID,
featuregate.StageAlpha,
featuregate.WithRegisterDescription("When enabled, aws.vpc.flow.start field will be formatted as ISO-8601 string instead of seconds since epoch integer."),
featuregate.WithRegisterReferenceURL("https://github.com/open-telemetry/opentelemetry-collector-contrib/pull/43390"),
)
cloudTrailUserIdentityPrefixFeatureGate = featuregate.GlobalRegistry().MustRegister(
constants.CloudTrailEnableUserIdentityPrefixID,
featuregate.StageAlpha,
featuregate.WithRegisterDescription("When enabled, CloudTrail log userIdentity attributes will use 'aws.user_identity' prefix. This helps to preserve the attribute origin."),
featuregate.WithRegisterReferenceURL("https://github.com/open-telemetry/opentelemetry-collector-contrib/pull/45459"))
}
var (
_ encoding.LogsUnmarshalerExtension = (*encodingExtension)(nil)
_ encoding.LogsDecoderExtension = (*encodingExtension)(nil)
)
type encodingExtension struct {
cfg *Config
unmarshaler awsunmarshaler.AWSUnmarshaler
format string
gzipPool sync.Pool
}
func newExtension(cfg *Config, settings extension.Settings) (*encodingExtension, error) {
switch cfg.Format {
case constants.FormatCloudWatchLogsSubscriptionFilter, constants.FormatCloudWatchLogsSubscriptionFilterV1:
if cfg.Format == constants.FormatCloudWatchLogsSubscriptionFilterV1 {
settings.Logger.Warn("using old format value. This format will be removed in version 0.138.0.",
zap.String("old_format", constants.FormatCloudWatchLogsSubscriptionFilterV1),
zap.String("new_format", constants.FormatCloudWatchLogsSubscriptionFilter),
)
}
return &encodingExtension{
unmarshaler: subscriptionfilter.NewSubscriptionFilterUnmarshaler(settings.BuildInfo),
format: constants.FormatCloudWatchLogsSubscriptionFilter,
}, nil
case constants.FormatVPCFlowLog, constants.FormatVPCFlowLogV1:
if cfg.Format == constants.FormatVPCFlowLogV1 {
settings.Logger.Warn("using old format value. This format will be removed in version 0.138.0.",
zap.String("old_format", constants.FormatVPCFlowLogV1),
zap.String("new_format", constants.FormatVPCFlowLog),
)
}
unmarshaler, err := vpcflowlog.NewVPCFlowLogUnmarshaler(
cfg.VPCFlowLogConfig,
settings.BuildInfo,
settings.Logger,
vpcFlowStartISO8601FormatFeatureGate.IsEnabled(),
)
return &encodingExtension{
cfg: cfg,
unmarshaler: unmarshaler,
format: constants.FormatVPCFlowLog,
}, err
case constants.FormatS3AccessLog, constants.FormatS3AccessLogV1:
if cfg.Format == constants.FormatS3AccessLogV1 {
settings.Logger.Warn("using old format value. This format will be removed in version 0.138.0.",
zap.String("old_format", constants.FormatS3AccessLogV1),
zap.String("new_format", constants.FormatS3AccessLog),
)
}
return &encodingExtension{
unmarshaler: s3accesslog.NewS3AccessLogUnmarshaler(settings.BuildInfo),
format: constants.FormatS3AccessLog,
}, nil
case constants.FormatWAFLog, constants.FormatWAFLogV1:
if cfg.Format == constants.FormatWAFLogV1 {
settings.Logger.Warn("using old format value. This format will be removed in version 0.138.0.",
zap.String("old_format", constants.FormatWAFLogV1),
zap.String("new_format", constants.FormatWAFLog),
)
}
return &encodingExtension{
unmarshaler: waf.NewWAFLogUnmarshaler(settings.BuildInfo),
format: constants.FormatWAFLog,
}, nil
case constants.FormatCloudTrailLog, constants.FormatCloudTrailLogV1:
if cfg.Format == constants.FormatCloudTrailLogV1 {
settings.Logger.Warn("using old format value. This format will be removed in version 0.138.0.",
zap.String("old_format", constants.FormatCloudTrailLogV1),
zap.String("new_format", constants.FormatCloudTrailLog),
)
}
return &encodingExtension{
unmarshaler: cloudtraillog.NewCloudTrailLogUnmarshaler(
settings.BuildInfo,
cloudTrailUserIdentityPrefixFeatureGate.IsEnabled()),
format: constants.FormatCloudTrailLog,
}, nil
case constants.FormatELBAccessLog, constants.FormatELBAccessLogV1:
if cfg.Format == constants.FormatELBAccessLogV1 {
settings.Logger.Warn("using old format value. This format will be removed in version 0.138.0.",
zap.String("old_format", constants.FormatELBAccessLogV1),
zap.String("new_format", constants.FormatELBAccessLog),
)
}
return &encodingExtension{
unmarshaler: elbaccesslogs.NewELBAccessLogUnmarshaler(
settings.BuildInfo,
settings.Logger,
),
format: constants.FormatELBAccessLog,
}, nil
case constants.FormatNetworkFirewallLog:
return &encodingExtension{
unmarshaler: networkfirewall.NewNetworkFirewallLogUnmarshaler(settings.BuildInfo),
format: constants.FormatNetworkFirewallLog,
}, nil
default:
// Format will have been validated by Config.Validate,
// so we'll only get here if we haven't handled a valid
// format.
return nil, fmt.Errorf("unimplemented format %q", cfg.Format)
}
}
func (*encodingExtension) Start(_ context.Context, _ component.Host) error {
return nil
}
func (*encodingExtension) Shutdown(_ context.Context) error {
return nil
}
func (e *encodingExtension) UnmarshalLogs(buf []byte) (plog.Logs, error) {
encodingReader, reader, err := e.getReaderFromFormat(buf)
if err != nil {
return plog.Logs{}, fmt.Errorf("failed to get reader for %q logs: %w", e.format, err)
}
defer func() {
if encodingReader == gzipEncoding {
r := reader.(*gzip.Reader)
_ = r.Close()
e.gzipPool.Put(r)
}
}()
logs, err := e.unmarshaler.UnmarshalAWSLogs(reader)
if err != nil {
return plog.Logs{}, fmt.Errorf("failed to unmarshal logs as %q format: %w", e.format, err)
}
return logs, nil
}
func (e *encodingExtension) NewLogsDecoder(reader io.Reader, options ...encoding.DecoderOption) (encoding.LogsDecoder, error) {
return e.unmarshaler.NewLogsDecoder(reader, options...)
}
func (e *encodingExtension) getReaderFromFormat(buf []byte) (string, io.Reader, error) {
switch e.format {
case constants.FormatWAFLog, constants.FormatCloudWatchLogsSubscriptionFilter, constants.FormatCloudTrailLog, constants.FormatELBAccessLog, constants.FormatNetworkFirewallLog:
return e.getReaderForData(buf)
case constants.FormatS3AccessLog:
return bytesEncoding, bytes.NewReader(buf), nil
case constants.FormatVPCFlowLog:
switch e.cfg.VPCFlowLogConfig.FileFormat {
case constants.FileFormatParquet:
return parquetEncoding, nil, fmt.Errorf("%q still needs to be implemented", constants.FileFormatParquet)
case constants.FileFormatPlainText:
return e.getReaderForData(buf)
default:
// should not be possible
return "", nil, fmt.Errorf(
"unsupported file fileFormat %q for VPC flow log, expected one of %q",
e.cfg.VPCFlowLogConfig.FileFormat,
supportedVPCFlowLogFileFormat,
)
}
default:
// should not be possible
return "", nil, fmt.Errorf("unimplemented: format %q has no reader", e.format)
}
}
// getReaderForData returns the appropriate reader and encoding type based on data format
func (e *encodingExtension) getReaderForData(buf []byte) (string, io.Reader, error) {
if isGzipData(buf) {
reader, err := e.getGzipReader(buf)
return gzipEncoding, reader, err
}
return bytesEncoding, bytes.NewReader(buf), nil
}
func (e *encodingExtension) getGzipReader(buf []byte) (io.Reader, error) {
var err error
gzipReader, ok := e.gzipPool.Get().(*gzip.Reader)
if !ok {
gzipReader, err = gzip.NewReader(bytes.NewReader(buf))
} else {
err = gzipReader.Reset(bytes.NewBuffer(buf))
}
if err != nil {
if gzipReader != nil {
e.gzipPool.Put(gzipReader)
}
return nil, fmt.Errorf("failed to decompress content: %w", err)
}
return gzipReader, nil
}
// isGzipData checks if the buffer contains gzip-compressed data by examining magic bytes
func isGzipData(buf []byte) bool {
return len(buf) > 2 && buf[0] == 0x1f && buf[1] == 0x8b
}