|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package forwardconnector // import "go.opentelemetry.io/collector/connector/forwardconnector" |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + |
| 20 | + "go.opentelemetry.io/collector/component" |
| 21 | + "go.opentelemetry.io/collector/connector" |
| 22 | + "go.opentelemetry.io/collector/consumer" |
| 23 | + "go.opentelemetry.io/collector/internal/sharedcomponent" |
| 24 | +) |
| 25 | + |
| 26 | +const ( |
| 27 | + typeStr = "forward" |
| 28 | +) |
| 29 | + |
| 30 | +type forwardFactory struct { |
| 31 | + // This is the map of already created forward connectors for particular configurations. |
| 32 | + // We maintain this map because the Factory is asked trace, metric, and log receivers |
| 33 | + // separately but they must not create separate objects. When the connector is shutdown |
| 34 | + // it should be removed from this map so the same configuration can be recreated successfully. |
| 35 | + *sharedcomponent.SharedComponents |
| 36 | +} |
| 37 | + |
| 38 | +// NewFactory returns a connector.Factory. |
| 39 | +func NewFactory() connector.Factory { |
| 40 | + f := &forwardFactory{sharedcomponent.NewSharedComponents()} |
| 41 | + return connector.NewFactory( |
| 42 | + typeStr, |
| 43 | + createDefaultConfig, |
| 44 | + connector.WithTracesToTraces(f.createTracesToTraces, component.StabilityLevelDevelopment), |
| 45 | + connector.WithMetricsToMetrics(f.createMetricsToMetrics, component.StabilityLevelDevelopment), |
| 46 | + connector.WithLogsToLogs(f.createLogsToLogs, component.StabilityLevelDevelopment), |
| 47 | + ) |
| 48 | +} |
| 49 | + |
| 50 | +// createDefaultConfig creates the default configuration. |
| 51 | +func createDefaultConfig() component.Config { |
| 52 | + return &struct{}{} |
| 53 | +} |
| 54 | + |
| 55 | +// createTracesToTraces creates a trace receiver based on provided config. |
| 56 | +func (f *forwardFactory) createTracesToTraces( |
| 57 | + _ context.Context, |
| 58 | + set connector.CreateSettings, |
| 59 | + cfg component.Config, |
| 60 | + nextConsumer consumer.Traces, |
| 61 | +) (connector.Traces, error) { |
| 62 | + comp := f.GetOrAdd(cfg, func() component.Component { |
| 63 | + return &forward{} |
| 64 | + }) |
| 65 | + |
| 66 | + conn := comp.Unwrap().(*forward) |
| 67 | + conn.Traces = nextConsumer |
| 68 | + return conn, nil |
| 69 | +} |
| 70 | + |
| 71 | +// createMetricsToMetrics creates a metrics receiver based on provided config. |
| 72 | +func (f *forwardFactory) createMetricsToMetrics( |
| 73 | + _ context.Context, |
| 74 | + set connector.CreateSettings, |
| 75 | + cfg component.Config, |
| 76 | + nextConsumer consumer.Metrics, |
| 77 | +) (connector.Metrics, error) { |
| 78 | + comp := f.GetOrAdd(cfg, func() component.Component { |
| 79 | + return &forward{} |
| 80 | + }) |
| 81 | + |
| 82 | + conn := comp.Unwrap().(*forward) |
| 83 | + conn.Metrics = nextConsumer |
| 84 | + return conn, nil |
| 85 | +} |
| 86 | + |
| 87 | +// createLogsToLogs creates a log receiver based on provided config. |
| 88 | +func (f *forwardFactory) createLogsToLogs( |
| 89 | + _ context.Context, |
| 90 | + set connector.CreateSettings, |
| 91 | + cfg component.Config, |
| 92 | + nextConsumer consumer.Logs, |
| 93 | +) (connector.Logs, error) { |
| 94 | + comp := f.GetOrAdd(cfg, func() component.Component { |
| 95 | + return &forward{} |
| 96 | + }) |
| 97 | + |
| 98 | + conn := comp.Unwrap().(*forward) |
| 99 | + conn.Logs = nextConsumer |
| 100 | + return conn, nil |
| 101 | +} |
| 102 | + |
| 103 | +// forward is used to pass signals directly from one pipeline to another. |
| 104 | +// This is useful when there is a need to replicate data and process it in more |
| 105 | +// than one way. It can also be used to join pipelines together. |
| 106 | +type forward struct { |
| 107 | + consumer.Traces |
| 108 | + consumer.Metrics |
| 109 | + consumer.Logs |
| 110 | + component.StartFunc |
| 111 | + component.ShutdownFunc |
| 112 | +} |
| 113 | + |
| 114 | +func (c *forward) Capabilities() consumer.Capabilities { |
| 115 | + return consumer.Capabilities{MutatesData: false} |
| 116 | +} |
0 commit comments