|
| 1 | +// Copyright (c) F5, Inc. |
| 2 | +// |
| 3 | +// This source code is licensed under the Apache License, Version 2.0 license found in the |
| 4 | +// LICENSE file in the root directory of this source tree. |
| 5 | +package logsgzipprocessor |
| 6 | + |
| 7 | +import ( |
| 8 | + "bytes" |
| 9 | + "compress/gzip" |
| 10 | + "context" |
| 11 | + "fmt" |
| 12 | + "io" |
| 13 | + "sync" |
| 14 | + |
| 15 | + "go.opentelemetry.io/collector/component" |
| 16 | + "go.opentelemetry.io/collector/consumer" |
| 17 | + "go.opentelemetry.io/collector/pdata/pcommon" |
| 18 | + "go.opentelemetry.io/collector/pdata/plog" |
| 19 | + "go.opentelemetry.io/collector/processor" |
| 20 | + "go.uber.org/multierr" |
| 21 | + "go.uber.org/zap" |
| 22 | +) |
| 23 | + |
| 24 | +// nolint: ireturn |
| 25 | +func NewFactory() processor.Factory { |
| 26 | + return processor.NewFactory( |
| 27 | + component.MustNewType("logsgzip"), |
| 28 | + func() component.Config { |
| 29 | + return &struct{}{} |
| 30 | + }, |
| 31 | + processor.WithLogs(createLogsGzipProcessor, component.StabilityLevelBeta), |
| 32 | + ) |
| 33 | +} |
| 34 | + |
| 35 | +// nolint: ireturn |
| 36 | +func createLogsGzipProcessor(_ context.Context, |
| 37 | + settings processor.Settings, |
| 38 | + cfg component.Config, |
| 39 | + logs consumer.Logs, |
| 40 | +) (processor.Logs, error) { |
| 41 | + logger := settings.Logger |
| 42 | + logger.Info("Creating logs gzip processor") |
| 43 | + |
| 44 | + return newLogsGzipProcessor(logs, settings), nil |
| 45 | +} |
| 46 | + |
| 47 | +// logsGzipProcessor is a custom-processor implementation for compressing individual log records into |
| 48 | +// gzip format. This can be used to reduce the size of log records and improve performance when processing |
| 49 | +// large log volumes. This processor will be used by default for agent interacting with NGINX One |
| 50 | +// console (https://docs.nginx.com/nginx-one/about/). |
| 51 | +type logsGzipProcessor struct { |
| 52 | + nextConsumer consumer.Logs |
| 53 | + // We use sync.Pool to efficiently manage and reuse gzip.Writer instances within this processor. |
| 54 | + // Otherwise, creating a new compressor for every log record would result in frequent memory allocations |
| 55 | + // and increased garbage collection overhead, especially under high-throughput workload like this one. |
| 56 | + // By pooling these objects, we minimize allocation churn, reduce GC pressure, and improve overall performance. |
| 57 | + pool *sync.Pool |
| 58 | + settings processor.Settings |
| 59 | +} |
| 60 | + |
| 61 | +type GzipWriter interface { |
| 62 | + Write(p []byte) (int, error) |
| 63 | + Close() error |
| 64 | + Reset(w io.Writer) |
| 65 | +} |
| 66 | + |
| 67 | +func newLogsGzipProcessor(logs consumer.Logs, settings processor.Settings) *logsGzipProcessor { |
| 68 | + return &logsGzipProcessor{ |
| 69 | + nextConsumer: logs, |
| 70 | + pool: &sync.Pool{ |
| 71 | + New: func() any { |
| 72 | + return gzip.NewWriter(nil) |
| 73 | + }, |
| 74 | + }, |
| 75 | + settings: settings, |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +func (p *logsGzipProcessor) ConsumeLogs(ctx context.Context, ld plog.Logs) error { |
| 80 | + var errs error |
| 81 | + resourceLogs := ld.ResourceLogs() |
| 82 | + for i := range resourceLogs.Len() { |
| 83 | + scopeLogs := resourceLogs.At(i).ScopeLogs() |
| 84 | + for j := range scopeLogs.Len() { |
| 85 | + err := p.processLogRecords(scopeLogs.At(j).LogRecords()) |
| 86 | + if err != nil { |
| 87 | + errs = multierr.Append(errs, err) |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + if errs != nil { |
| 92 | + return fmt.Errorf("failed processing log records: %w", errs) |
| 93 | + } |
| 94 | + |
| 95 | + return p.nextConsumer.ConsumeLogs(ctx, ld) |
| 96 | +} |
| 97 | + |
| 98 | +func (p *logsGzipProcessor) processLogRecords(logRecords plog.LogRecordSlice) error { |
| 99 | + var errs error |
| 100 | + // Filter out unsupported data types in the log before processing |
| 101 | + logRecords.RemoveIf(func(lr plog.LogRecord) bool { |
| 102 | + body := lr.Body() |
| 103 | + // Keep only STRING or BYTES types |
| 104 | + if body.Type() != pcommon.ValueTypeStr && |
| 105 | + body.Type() != pcommon.ValueTypeBytes { |
| 106 | + p.settings.Logger.Debug("Skipping log record with unsupported body type", zap.Any("type", body.Type())) |
| 107 | + return true |
| 108 | + } |
| 109 | + |
| 110 | + return false |
| 111 | + }) |
| 112 | + // Process remaining valid records |
| 113 | + for k := range logRecords.Len() { |
| 114 | + record := logRecords.At(k) |
| 115 | + body := record.Body() |
| 116 | + var data []byte |
| 117 | + //nolint:exhaustive // Already filtered out other types with RemoveIf |
| 118 | + switch body.Type() { |
| 119 | + case pcommon.ValueTypeStr: |
| 120 | + data = []byte(body.Str()) |
| 121 | + case pcommon.ValueTypeBytes: |
| 122 | + data = body.Bytes().AsRaw() |
| 123 | + } |
| 124 | + gzipped, err := p.gzipCompress(data) |
| 125 | + if err != nil { |
| 126 | + errs = multierr.Append(errs, fmt.Errorf("failed to compress log record: %w", err)) |
| 127 | + |
| 128 | + continue |
| 129 | + } |
| 130 | + err = record.Body().FromRaw(gzipped) |
| 131 | + if err != nil { |
| 132 | + errs = multierr.Append(errs, fmt.Errorf("failed to set gzipped data to log record body: %w", err)) |
| 133 | + |
| 134 | + continue |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + return errs |
| 139 | +} |
| 140 | + |
| 141 | +func (p *logsGzipProcessor) gzipCompress(data []byte) ([]byte, error) { |
| 142 | + var buf bytes.Buffer |
| 143 | + var err error |
| 144 | + wIface := p.pool.Get() |
| 145 | + w, ok := wIface.(GzipWriter) |
| 146 | + if !ok { |
| 147 | + return nil, fmt.Errorf("writer of type %T not supported", wIface) |
| 148 | + } |
| 149 | + w.Reset(&buf) |
| 150 | + defer func() { |
| 151 | + if err = w.Close(); err != nil { |
| 152 | + p.settings.Logger.Error("Failed to close gzip writer", zap.Error(err)) |
| 153 | + } |
| 154 | + p.pool.Put(w) |
| 155 | + }() |
| 156 | + |
| 157 | + _, err = w.Write(data) |
| 158 | + if err != nil { |
| 159 | + return nil, err |
| 160 | + } |
| 161 | + if err = w.Close(); err != nil { |
| 162 | + return nil, err |
| 163 | + } |
| 164 | + |
| 165 | + return buf.Bytes(), nil |
| 166 | +} |
| 167 | + |
| 168 | +func (p *logsGzipProcessor) Capabilities() consumer.Capabilities { |
| 169 | + return consumer.Capabilities{ |
| 170 | + MutatesData: true, |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +func (p *logsGzipProcessor) Start(ctx context.Context, _ component.Host) error { |
| 175 | + p.settings.Logger.Info("Starting logs gzip processor") |
| 176 | + return nil |
| 177 | +} |
| 178 | + |
| 179 | +func (p *logsGzipProcessor) Shutdown(ctx context.Context) error { |
| 180 | + p.settings.Logger.Info("Shutting down logs gzip processor") |
| 181 | + return nil |
| 182 | +} |
0 commit comments