| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package testcomponents // import "go.opentelemetry.io/collector/service/internal/testcomponents" |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
|
| 9 |
"go.opentelemetry.io/collector/component" |
| 10 |
"go.opentelemetry.io/collector/connector" |
| 11 |
"go.opentelemetry.io/collector/connector/xconnector" |
| 12 |
"go.opentelemetry.io/collector/consumer" |
| 13 |
"go.opentelemetry.io/collector/consumer/xconsumer" |
| 14 |
"go.opentelemetry.io/collector/pdata/plog" |
| 15 |
"go.opentelemetry.io/collector/pdata/pmetric" |
| 16 |
"go.opentelemetry.io/collector/pdata/pprofile" |
| 17 |
"go.opentelemetry.io/collector/pdata/ptrace" |
| 18 |
"go.opentelemetry.io/collector/pdata/testdata" |
| 19 |
) |
| 20 |
|
| 21 |
var connType = component.MustNewType("exampleconnector") |
| 22 |
|
| 23 |
// ExampleConnectorFactory is factory for ExampleConnector. |
| 24 |
var ExampleConnectorFactory = xconnector.NewFactory( |
| 25 |
connType, |
| 26 |
createExampleConnectorDefaultConfig, |
| 27 |
|
| 28 |
xconnector.WithTracesToTraces(createExampleTracesToTraces, component.StabilityLevelDevelopment), |
| 29 |
xconnector.WithTracesToMetrics(createExampleTracesToMetrics, component.StabilityLevelDevelopment), |
| 30 |
xconnector.WithTracesToLogs(createExampleTracesToLogs, component.StabilityLevelDevelopment), |
| 31 |
xconnector.WithTracesToProfiles(createExampleTracesToProfiles, component.StabilityLevelDevelopment), |
| 32 |
|
| 33 |
xconnector.WithMetricsToTraces(createExampleMetricsToTraces, component.StabilityLevelDevelopment), |
| 34 |
xconnector.WithMetricsToMetrics(createExampleMetricsToMetrics, component.StabilityLevelDevelopment), |
| 35 |
xconnector.WithMetricsToLogs(createExampleMetricsToLogs, component.StabilityLevelDevelopment), |
| 36 |
xconnector.WithMetricsToProfiles(createExampleMetricsToProfiles, component.StabilityLevelDevelopment), |
| 37 |
|
| 38 |
xconnector.WithLogsToTraces(createExampleLogsToTraces, component.StabilityLevelDevelopment), |
| 39 |
xconnector.WithLogsToMetrics(createExampleLogsToMetrics, component.StabilityLevelDevelopment), |
| 40 |
xconnector.WithLogsToLogs(createExampleLogsToLogs, component.StabilityLevelDevelopment), |
| 41 |
xconnector.WithLogsToProfiles(createExampleLogsToProfiles, component.StabilityLevelDevelopment), |
| 42 |
|
| 43 |
xconnector.WithProfilesToTraces(createExampleProfilesToTraces, component.StabilityLevelDevelopment), |
| 44 |
xconnector.WithProfilesToMetrics(createExampleProfilesToMetrics, component.StabilityLevelDevelopment), |
| 45 |
xconnector.WithProfilesToLogs(createExampleProfilesToLogs, component.StabilityLevelDevelopment), |
| 46 |
xconnector.WithProfilesToProfiles(createExampleProfilesToProfiles, component.StabilityLevelDevelopment), |
| 47 |
) |
| 48 |
|
| 49 |
var MockForwardConnectorFactory = xconnector.NewFactory( |
| 50 |
component.MustNewType("mockforward"), |
| 51 |
createExampleConnectorDefaultConfig, |
| 52 |
xconnector.WithTracesToTraces(createExampleTracesToTraces, component.StabilityLevelDevelopment), |
| 53 |
xconnector.WithMetricsToMetrics(createExampleMetricsToMetrics, component.StabilityLevelDevelopment), |
| 54 |
xconnector.WithLogsToLogs(createExampleLogsToLogs, component.StabilityLevelDevelopment), |
| 55 |
xconnector.WithProfilesToProfiles(createExampleProfilesToProfiles, component.StabilityLevelDevelopment), |
| 56 |
) |
| 57 |
|
| 58 |
func createExampleConnectorDefaultConfig() component.Config { |
| 59 |
return &struct{}{} |
| 60 |
} |
| 61 |
|
| 62 |
func createExampleTracesToTraces(_ context.Context, set connector.Settings, _ component.Config, traces consumer.Traces) (connector.Traces, error) { |
| 63 |
return &ExampleConnector{ |
| 64 |
ConsumeTracesFunc: traces.ConsumeTraces, |
| 65 |
mutatesData: set.ID.Name() == "mutate", |
| 66 |
}, nil |
| 67 |
} |
| 68 |
|
| 69 |
func createExampleTracesToMetrics(_ context.Context, set connector.Settings, _ component.Config, metrics consumer.Metrics) (connector.Traces, error) { |
| 70 |
return &ExampleConnector{ |
| 71 |
ConsumeTracesFunc: func(ctx context.Context, td ptrace.Traces) error { |
| 72 |
return metrics.ConsumeMetrics(ctx, testdata.GenerateMetrics(td.SpanCount())) |
| 73 |
}, |
| 74 |
mutatesData: set.ID.Name() == "mutate", |
| 75 |
}, nil |
| 76 |
} |
| 77 |
|
| 78 |
func createExampleTracesToLogs(_ context.Context, set connector.Settings, _ component.Config, logs consumer.Logs) (connector.Traces, error) { |
| 79 |
return &ExampleConnector{ |
| 80 |
ConsumeTracesFunc: func(ctx context.Context, td ptrace.Traces) error { |
| 81 |
return logs.ConsumeLogs(ctx, testdata.GenerateLogs(td.SpanCount())) |
| 82 |
}, |
| 83 |
mutatesData: set.ID.Name() == "mutate", |
| 84 |
}, nil |
| 85 |
} |
| 86 |
|
| 87 |
func createExampleTracesToProfiles(_ context.Context, set connector.Settings, _ component.Config, profiles xconsumer.Profiles) (connector.Traces, error) { |
| 88 |
return &ExampleConnector{ |
| 89 |
ConsumeTracesFunc: func(ctx context.Context, td ptrace.Traces) error { |
| 90 |
return profiles.ConsumeProfiles(ctx, testdata.GenerateProfiles(td.SpanCount())) |
| 91 |
}, |
| 92 |
mutatesData: set.ID.Name() == "mutate", |
| 93 |
}, nil |
| 94 |
} |
| 95 |
|
| 96 |
func createExampleMetricsToTraces(_ context.Context, set connector.Settings, _ component.Config, traces consumer.Traces) (connector.Metrics, error) { |
| 97 |
return &ExampleConnector{ |
| 98 |
ConsumeMetricsFunc: func(ctx context.Context, md pmetric.Metrics) error { |
| 99 |
return traces.ConsumeTraces(ctx, testdata.GenerateTraces(md.MetricCount())) |
| 100 |
}, |
| 101 |
mutatesData: set.ID.Name() == "mutate", |
| 102 |
}, nil |
| 103 |
} |
| 104 |
|
| 105 |
func createExampleMetricsToMetrics(_ context.Context, set connector.Settings, _ component.Config, metrics consumer.Metrics) (connector.Metrics, error) { |
| 106 |
return &ExampleConnector{ |
| 107 |
ConsumeMetricsFunc: metrics.ConsumeMetrics, |
| 108 |
mutatesData: set.ID.Name() == "mutate", |
| 109 |
}, nil |
| 110 |
} |
| 111 |
|
| 112 |
func createExampleMetricsToLogs(_ context.Context, set connector.Settings, _ component.Config, logs consumer.Logs) (connector.Metrics, error) { |
| 113 |
return &ExampleConnector{ |
| 114 |
ConsumeMetricsFunc: func(ctx context.Context, md pmetric.Metrics) error { |
| 115 |
return logs.ConsumeLogs(ctx, testdata.GenerateLogs(md.MetricCount())) |
| 116 |
}, |
| 117 |
mutatesData: set.ID.Name() == "mutate", |
| 118 |
}, nil |
| 119 |
} |
| 120 |
|
| 121 |
func createExampleMetricsToProfiles(_ context.Context, set connector.Settings, _ component.Config, profiles xconsumer.Profiles) (connector.Metrics, error) { |
| 122 |
return &ExampleConnector{ |
| 123 |
ConsumeMetricsFunc: func(ctx context.Context, md pmetric.Metrics) error { |
| 124 |
return profiles.ConsumeProfiles(ctx, testdata.GenerateProfiles(md.MetricCount())) |
| 125 |
}, |
| 126 |
mutatesData: set.ID.Name() == "mutate", |
| 127 |
}, nil |
| 128 |
} |
| 129 |
|
| 130 |
func createExampleLogsToTraces(_ context.Context, set connector.Settings, _ component.Config, traces consumer.Traces) (connector.Logs, error) { |
| 131 |
return &ExampleConnector{ |
| 132 |
ConsumeLogsFunc: func(ctx context.Context, ld plog.Logs) error { |
| 133 |
return traces.ConsumeTraces(ctx, testdata.GenerateTraces(ld.LogRecordCount())) |
| 134 |
}, |
| 135 |
mutatesData: set.ID.Name() == "mutate", |
| 136 |
}, nil |
| 137 |
} |
| 138 |
|
| 139 |
func createExampleLogsToMetrics(_ context.Context, set connector.Settings, _ component.Config, metrics consumer.Metrics) (connector.Logs, error) { |
| 140 |
return &ExampleConnector{ |
| 141 |
ConsumeLogsFunc: func(ctx context.Context, ld plog.Logs) error { |
| 142 |
return metrics.ConsumeMetrics(ctx, testdata.GenerateMetrics(ld.LogRecordCount())) |
| 143 |
}, |
| 144 |
mutatesData: set.ID.Name() == "mutate", |
| 145 |
}, nil |
| 146 |
} |
| 147 |
|
| 148 |
func createExampleLogsToLogs(_ context.Context, set connector.Settings, _ component.Config, logs consumer.Logs) (connector.Logs, error) { |
| 149 |
return &ExampleConnector{ |
| 150 |
ConsumeLogsFunc: logs.ConsumeLogs, |
| 151 |
mutatesData: set.ID.Name() == "mutate", |
| 152 |
}, nil |
| 153 |
} |
| 154 |
|
| 155 |
func createExampleLogsToProfiles(_ context.Context, set connector.Settings, _ component.Config, profiles xconsumer.Profiles) (connector.Logs, error) { |
| 156 |
return &ExampleConnector{ |
| 157 |
ConsumeLogsFunc: func(ctx context.Context, ld plog.Logs) error { |
| 158 |
return profiles.ConsumeProfiles(ctx, testdata.GenerateProfiles(ld.LogRecordCount())) |
| 159 |
}, |
| 160 |
mutatesData: set.ID.Name() == "mutate", |
| 161 |
}, nil |
| 162 |
} |
| 163 |
|
| 164 |
func createExampleProfilesToTraces(_ context.Context, set connector.Settings, _ component.Config, traces consumer.Traces) (xconnector.Profiles, error) { |
| 165 |
return &ExampleConnector{ |
| 166 |
ConsumeProfilesFunc: func(ctx context.Context, _ pprofile.Profiles) error { |
| 167 |
return traces.ConsumeTraces(ctx, testdata.GenerateTraces(1)) |
| 168 |
}, |
| 169 |
mutatesData: set.ID.Name() == "mutate", |
| 170 |
}, nil |
| 171 |
} |
| 172 |
|
| 173 |
func createExampleProfilesToMetrics(_ context.Context, set connector.Settings, _ component.Config, metrics consumer.Metrics) (xconnector.Profiles, error) { |
| 174 |
return &ExampleConnector{ |
| 175 |
ConsumeProfilesFunc: func(ctx context.Context, _ pprofile.Profiles) error { |
| 176 |
return metrics.ConsumeMetrics(ctx, testdata.GenerateMetrics(1)) |
| 177 |
}, |
| 178 |
mutatesData: set.ID.Name() == "mutate", |
| 179 |
}, nil |
| 180 |
} |
| 181 |
|
| 182 |
func createExampleProfilesToLogs(_ context.Context, set connector.Settings, _ component.Config, logs consumer.Logs) (xconnector.Profiles, error) { |
| 183 |
return &ExampleConnector{ |
| 184 |
ConsumeProfilesFunc: func(ctx context.Context, _ pprofile.Profiles) error { |
| 185 |
return logs.ConsumeLogs(ctx, testdata.GenerateLogs(1)) |
| 186 |
}, |
| 187 |
mutatesData: set.ID.Name() == "mutate", |
| 188 |
}, nil |
| 189 |
} |
| 190 |
|
| 191 |
func createExampleProfilesToProfiles(_ context.Context, set connector.Settings, _ component.Config, profiles xconsumer.Profiles) (xconnector.Profiles, error) { |
| 192 |
return &ExampleConnector{ |
| 193 |
ConsumeProfilesFunc: profiles.ConsumeProfiles, |
| 194 |
mutatesData: set.ID.Name() == "mutate", |
| 195 |
}, nil |
| 196 |
} |
| 197 |
|
| 198 |
type ExampleConnector struct { |
| 199 |
componentState |
| 200 |
consumer.ConsumeTracesFunc |
| 201 |
consumer.ConsumeMetricsFunc |
| 202 |
consumer.ConsumeLogsFunc |
| 203 |
xconsumer.ConsumeProfilesFunc |
| 204 |
mutatesData bool |
| 205 |
} |
| 206 |
|
| 207 |
func (c *ExampleConnector) Capabilities() consumer.Capabilities { |
| 208 |
return consumer.Capabilities{MutatesData: c.mutatesData} |
| 209 |
} |