@@ -55,7 +55,10 @@ func init() {
5555 featuregate .WithRegisterReferenceURL ("https://github.com/open-telemetry/opentelemetry-collector-contrib/pull/45459" ))
5656}
5757
58- var _ encoding.LogsUnmarshalerExtension = (* encodingExtension )(nil )
58+ var (
59+ _ encoding.LogsUnmarshalerExtension = (* encodingExtension )(nil )
60+ _ encoding.LogsDecoderExtension = (* encodingExtension )(nil )
61+ )
5962
6063type encodingExtension struct {
6164 cfg * Config
@@ -70,8 +73,8 @@ func newExtension(cfg *Config, settings extension.Settings) (*encodingExtension,
7073 case constants .FormatCloudWatchLogsSubscriptionFilter , constants .FormatCloudWatchLogsSubscriptionFilterV1 :
7174 if cfg .Format == constants .FormatCloudWatchLogsSubscriptionFilterV1 {
7275 settings .Logger .Warn ("using old format value. This format will be removed in version 0.138.0." ,
73- zap .String ("old_format" , string ( constants .FormatCloudWatchLogsSubscriptionFilterV1 ) ),
74- zap .String ("new_format" , string ( constants .FormatCloudWatchLogsSubscriptionFilter ) ),
76+ zap .String ("old_format" , constants .FormatCloudWatchLogsSubscriptionFilterV1 ),
77+ zap .String ("new_format" , constants .FormatCloudWatchLogsSubscriptionFilter ),
7578 )
7679 }
7780 return & encodingExtension {
@@ -81,8 +84,8 @@ func newExtension(cfg *Config, settings extension.Settings) (*encodingExtension,
8184 case constants .FormatVPCFlowLog , constants .FormatVPCFlowLogV1 :
8285 if cfg .Format == constants .FormatVPCFlowLogV1 {
8386 settings .Logger .Warn ("using old format value. This format will be removed in version 0.138.0." ,
84- zap .String ("old_format" , string ( constants .FormatVPCFlowLogV1 ) ),
85- zap .String ("new_format" , string ( constants .FormatVPCFlowLog ) ),
87+ zap .String ("old_format" , constants .FormatVPCFlowLogV1 ),
88+ zap .String ("new_format" , constants .FormatVPCFlowLog ),
8689 )
8790 }
8891
@@ -100,8 +103,8 @@ func newExtension(cfg *Config, settings extension.Settings) (*encodingExtension,
100103 case constants .FormatS3AccessLog , constants .FormatS3AccessLogV1 :
101104 if cfg .Format == constants .FormatS3AccessLogV1 {
102105 settings .Logger .Warn ("using old format value. This format will be removed in version 0.138.0." ,
103- zap .String ("old_format" , string ( constants .FormatS3AccessLogV1 ) ),
104- zap .String ("new_format" , string ( constants .FormatS3AccessLog ) ),
106+ zap .String ("old_format" , constants .FormatS3AccessLogV1 ),
107+ zap .String ("new_format" , constants .FormatS3AccessLog ),
105108 )
106109 }
107110 return & encodingExtension {
@@ -111,8 +114,8 @@ func newExtension(cfg *Config, settings extension.Settings) (*encodingExtension,
111114 case constants .FormatWAFLog , constants .FormatWAFLogV1 :
112115 if cfg .Format == constants .FormatWAFLogV1 {
113116 settings .Logger .Warn ("using old format value. This format will be removed in version 0.138.0." ,
114- zap .String ("old_format" , string ( constants .FormatWAFLogV1 ) ),
115- zap .String ("new_format" , string ( constants .FormatWAFLog ) ),
117+ zap .String ("old_format" , constants .FormatWAFLogV1 ),
118+ zap .String ("new_format" , constants .FormatWAFLog ),
116119 )
117120 }
118121 return & encodingExtension {
@@ -122,8 +125,8 @@ func newExtension(cfg *Config, settings extension.Settings) (*encodingExtension,
122125 case constants .FormatCloudTrailLog , constants .FormatCloudTrailLogV1 :
123126 if cfg .Format == constants .FormatCloudTrailLogV1 {
124127 settings .Logger .Warn ("using old format value. This format will be removed in version 0.138.0." ,
125- zap .String ("old_format" , string ( constants .FormatCloudTrailLogV1 ) ),
126- zap .String ("new_format" , string ( constants .FormatCloudTrailLog ) ),
128+ zap .String ("old_format" , constants .FormatCloudTrailLogV1 ),
129+ zap .String ("new_format" , constants .FormatCloudTrailLog ),
127130 )
128131 }
129132 return & encodingExtension {
@@ -135,8 +138,8 @@ func newExtension(cfg *Config, settings extension.Settings) (*encodingExtension,
135138 case constants .FormatELBAccessLog , constants .FormatELBAccessLogV1 :
136139 if cfg .Format == constants .FormatELBAccessLogV1 {
137140 settings .Logger .Warn ("using old format value. This format will be removed in version 0.138.0." ,
138- zap .String ("old_format" , string ( constants .FormatELBAccessLogV1 ) ),
139- zap .String ("new_format" , string ( constants .FormatELBAccessLog ) ),
141+ zap .String ("old_format" , constants .FormatELBAccessLogV1 ),
142+ zap .String ("new_format" , constants .FormatELBAccessLog ),
140143 )
141144 }
142145 return & encodingExtension {
@@ -167,37 +170,30 @@ func (*encodingExtension) Shutdown(_ context.Context) error {
167170 return nil
168171}
169172
170- func (e * encodingExtension ) getGzipReader (buf []byte ) (io.Reader , error ) {
171- var err error
172- gzipReader , ok := e .gzipPool .Get ().(* gzip.Reader )
173- if ! ok {
174- gzipReader , err = gzip .NewReader (bytes .NewReader (buf ))
175- } else {
176- err = gzipReader .Reset (bytes .NewBuffer (buf ))
173+ func (e * encodingExtension ) UnmarshalLogs (buf []byte ) (plog.Logs , error ) {
174+ encodingReader , reader , err := e .getReaderFromFormat (buf )
175+ if err != nil {
176+ return plog.Logs {}, fmt .Errorf ("failed to get reader for %q logs: %w" , e .format , err )
177177 }
178178
179- if err != nil {
180- if gzipReader != nil {
181- e .gzipPool .Put (gzipReader )
179+ defer func () {
180+ if encodingReader == gzipEncoding {
181+ r := reader .(* gzip.Reader )
182+ _ = r .Close ()
183+ e .gzipPool .Put (r )
182184 }
183- return nil , fmt .Errorf ("failed to decompress content: %w" , err )
184- }
185+ }()
185186
186- return gzipReader , nil
187- }
187+ logs , err := e .unmarshaler .UnmarshalAWSLogs (reader )
188+ if err != nil {
189+ return plog.Logs {}, fmt .Errorf ("failed to unmarshal logs as %q format: %w" , e .format , err )
190+ }
188191
189- // isGzipData checks if the buffer contains gzip-compressed data by examining magic bytes
190- func isGzipData (buf []byte ) bool {
191- return len (buf ) > 2 && buf [0 ] == 0x1f && buf [1 ] == 0x8b
192+ return logs , nil
192193}
193194
194- // getReaderForData returns the appropriate reader and encoding type based on data format
195- func (e * encodingExtension ) getReaderForData (buf []byte ) (string , io.Reader , error ) {
196- if isGzipData (buf ) {
197- reader , err := e .getGzipReader (buf )
198- return gzipEncoding , reader , err
199- }
200- return bytesEncoding , bytes .NewReader (buf ), nil
195+ func (e * encodingExtension ) NewLogsDecoder (reader io.Reader , options ... encoding.DecoderOption ) (encoding.LogsDecoder , error ) {
196+ return e .unmarshaler .NewLogsDecoder (reader , options ... )
201197}
202198
203199func (e * encodingExtension ) getReaderFromFormat (buf []byte ) (string , io.Reader , error ) {
@@ -229,24 +225,35 @@ func (e *encodingExtension) getReaderFromFormat(buf []byte) (string, io.Reader,
229225 }
230226}
231227
232- func (e * encodingExtension ) UnmarshalLogs (buf []byte ) (plog.Logs , error ) {
233- encodingReader , reader , err := e .getReaderFromFormat (buf )
234- if err != nil {
235- return plog.Logs {}, fmt .Errorf ("failed to get reader for %q logs: %w" , e .format , err )
228+ // getReaderForData returns the appropriate reader and encoding type based on data format
229+ func (e * encodingExtension ) getReaderForData (buf []byte ) (string , io.Reader , error ) {
230+ if isGzipData (buf ) {
231+ reader , err := e .getGzipReader (buf )
232+ return gzipEncoding , reader , err
236233 }
234+ return bytesEncoding , bytes .NewReader (buf ), nil
235+ }
237236
238- defer func () {
239- if encodingReader == gzipEncoding {
240- r := reader .(* gzip.Reader )
241- _ = r .Close ()
242- e .gzipPool .Put (r )
243- }
244- }()
237+ func (e * encodingExtension ) getGzipReader (buf []byte ) (io.Reader , error ) {
238+ var err error
239+ gzipReader , ok := e .gzipPool .Get ().(* gzip.Reader )
240+ if ! ok {
241+ gzipReader , err = gzip .NewReader (bytes .NewReader (buf ))
242+ } else {
243+ err = gzipReader .Reset (bytes .NewBuffer (buf ))
244+ }
245245
246- logs , err := e .unmarshaler .UnmarshalAWSLogs (reader )
247246 if err != nil {
248- return plog.Logs {}, fmt .Errorf ("failed to unmarshal logs as %q format: %w" , e .format , err )
247+ if gzipReader != nil {
248+ e .gzipPool .Put (gzipReader )
249+ }
250+ return nil , fmt .Errorf ("failed to decompress content: %w" , err )
249251 }
250252
251- return logs , nil
253+ return gzipReader , nil
254+ }
255+
256+ // isGzipData checks if the buffer contains gzip-compressed data by examining magic bytes
257+ func isGzipData (buf []byte ) bool {
258+ return len (buf ) > 2 && buf [0 ] == 0x1f && buf [1 ] == 0x8b
252259}
0 commit comments