| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
//go:generate mdatagen metadata.yaml |
| 5 |
|
| 6 |
package memorylimiterprocessor // import "go.opentelemetry.io/collector/processor/memorylimiterprocessor" |
| 7 |
|
| 8 |
import ( |
| 9 |
"context" |
| 10 |
"sync" |
| 11 |
|
| 12 |
"go.opentelemetry.io/collector/component" |
| 13 |
"go.opentelemetry.io/collector/consumer" |
| 14 |
"go.opentelemetry.io/collector/consumer/xconsumer" |
| 15 |
"go.opentelemetry.io/collector/internal/memorylimiter" |
| 16 |
"go.opentelemetry.io/collector/internal/telemetry" |
| 17 |
"go.opentelemetry.io/collector/processor" |
| 18 |
"go.opentelemetry.io/collector/processor/memorylimiterprocessor/internal/metadata" |
| 19 |
"go.opentelemetry.io/collector/processor/processorhelper" |
| 20 |
"go.opentelemetry.io/collector/processor/processorhelper/xprocessorhelper" |
| 21 |
"go.opentelemetry.io/collector/processor/xprocessor" |
| 22 |
) |
| 23 |
|
| 24 |
var processorCapabilities = consumer.Capabilities{MutatesData: false} |
| 25 |
|
| 26 |
type factory struct { |
| 27 |
// memoryLimiters stores memoryLimiter instances with unique configs that multiple processors can reuse. |
| 28 |
// This avoids running multiple memory checks (ie: GC) for every processor using the same processor config. |
| 29 |
memoryLimiters map[component.Config]*memoryLimiterProcessor |
| 30 |
lock sync.Mutex |
| 31 |
} |
| 32 |
|
| 33 |
// NewFactory returns a new factory for the Memory Limiter processor. |
| 34 |
func NewFactory() xprocessor.Factory { |
| 35 |
f := &factory{ |
| 36 |
memoryLimiters: map[component.Config]*memoryLimiterProcessor{}, |
| 37 |
} |
| 38 |
return xprocessor.NewFactory( |
| 39 |
metadata.Type, |
| 40 |
createDefaultConfig, |
| 41 |
xprocessor.WithTraces(f.createTraces, metadata.TracesStability), |
| 42 |
xprocessor.WithMetrics(f.createMetrics, metadata.MetricsStability), |
| 43 |
xprocessor.WithLogs(f.createLogs, metadata.LogsStability), |
| 44 |
xprocessor.WithProfiles(f.createProfiles, metadata.ProfilesStability)) |
| 45 |
} |
| 46 |
|
| 47 |
// CreateDefaultConfig creates the default configuration for processor. Notice |
| 48 |
// that the default configuration is expected to fail for this processor. |
| 49 |
func createDefaultConfig() component.Config { |
| 50 |
return memorylimiter.NewDefaultConfig() |
| 51 |
} |
| 52 |
|
| 53 |
func (f *factory) createTraces( |
| 54 |
ctx context.Context, |
| 55 |
set processor.Settings, |
| 56 |
cfg component.Config, |
| 57 |
nextConsumer consumer.Traces, |
| 58 |
) (processor.Traces, error) { |
| 59 |
memLimiter, err := f.getMemoryLimiter(set, cfg) |
| 60 |
if err != nil { |
| 61 |
return nil, err |
| 62 |
} |
| 63 |
return processorhelper.NewTraces(ctx, set, cfg, nextConsumer, |
| 64 |
memLimiter.processTraces, |
| 65 |
processorhelper.WithCapabilities(processorCapabilities), |
| 66 |
processorhelper.WithStart(memLimiter.start), |
| 67 |
processorhelper.WithShutdown(memLimiter.shutdown)) |
| 68 |
} |
| 69 |
|
| 70 |
func (f *factory) createMetrics( |
| 71 |
ctx context.Context, |
| 72 |
set processor.Settings, |
| 73 |
cfg component.Config, |
| 74 |
nextConsumer consumer.Metrics, |
| 75 |
) (processor.Metrics, error) { |
| 76 |
memLimiter, err := f.getMemoryLimiter(set, cfg) |
| 77 |
if err != nil { |
| 78 |
return nil, err |
| 79 |
} |
| 80 |
return processorhelper.NewMetrics(ctx, set, cfg, nextConsumer, |
| 81 |
memLimiter.processMetrics, |
| 82 |
processorhelper.WithCapabilities(processorCapabilities), |
| 83 |
processorhelper.WithStart(memLimiter.start), |
| 84 |
processorhelper.WithShutdown(memLimiter.shutdown)) |
| 85 |
} |
| 86 |
|
| 87 |
func (f *factory) createLogs( |
| 88 |
ctx context.Context, |
| 89 |
set processor.Settings, |
| 90 |
cfg component.Config, |
| 91 |
nextConsumer consumer.Logs, |
| 92 |
) (processor.Logs, error) { |
| 93 |
memLimiter, err := f.getMemoryLimiter(set, cfg) |
| 94 |
if err != nil { |
| 95 |
return nil, err |
| 96 |
} |
| 97 |
return processorhelper.NewLogs(ctx, set, cfg, nextConsumer, |
| 98 |
memLimiter.processLogs, |
| 99 |
processorhelper.WithCapabilities(processorCapabilities), |
| 100 |
processorhelper.WithStart(memLimiter.start), |
| 101 |
processorhelper.WithShutdown(memLimiter.shutdown)) |
| 102 |
} |
| 103 |
|
| 104 |
func (f *factory) createProfiles( |
| 105 |
ctx context.Context, |
| 106 |
set processor.Settings, |
| 107 |
cfg component.Config, |
| 108 |
nextConsumer xconsumer.Profiles, |
| 109 |
) (xprocessor.Profiles, error) { |
| 110 |
memLimiter, err := f.getMemoryLimiter(set, cfg) |
| 111 |
if err != nil { |
| 112 |
return nil, err |
| 113 |
} |
| 114 |
|
| 115 |
return xprocessorhelper.NewProfiles( |
| 116 |
ctx, |
| 117 |
set, |
| 118 |
cfg, |
| 119 |
nextConsumer, |
| 120 |
memLimiter.processProfiles, |
| 121 |
xprocessorhelper.WithCapabilities(processorCapabilities), |
| 122 |
xprocessorhelper.WithStart(memLimiter.start), |
| 123 |
xprocessorhelper.WithShutdown(memLimiter.shutdown), |
| 124 |
) |
| 125 |
} |
| 126 |
|
| 127 |
// getMemoryLimiter checks if we have a cached memoryLimiter with a specific config, |
| 128 |
// otherwise initialize and add one to the store. |
| 129 |
func (f *factory) getMemoryLimiter(set processor.Settings, cfg component.Config) (*memoryLimiterProcessor, error) { |
| 130 |
f.lock.Lock() |
| 131 |
defer f.lock.Unlock() |
| 132 |
|
| 133 |
if memLimiter, ok := f.memoryLimiters[cfg]; ok { |
| 134 |
return memLimiter, nil |
| 135 |
} |
| 136 |
|
| 137 |
set.TelemetrySettings = telemetry.DropInjectedAttributes( |
| 138 |
set.TelemetrySettings, |
| 139 |
telemetry.SignalKey, |
| 140 |
telemetry.PipelineIDKey, |
| 141 |
telemetry.ComponentIDKey, |
| 142 |
) |
| 143 |
set.Logger.Debug("created singleton logger") |
| 144 |
|
| 145 |
memLimiter, err := newMemoryLimiterProcessor(set, cfg.(*Config)) |
| 146 |
if err != nil { |
| 147 |
return nil, err |
| 148 |
} |
| 149 |
|
| 150 |
f.memoryLimiters[cfg] = memLimiter |
| 151 |
return memLimiter, nil |
| 152 |
} |