| 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/pipeline" |
| 19 |
) |
| 20 |
|
| 21 |
var routerType = component.MustNewType("examplerouter") |
| 22 |
|
| 23 |
// ExampleRouterFactory is factory for ExampleRouter. |
| 24 |
var ExampleRouterFactory = xconnector.NewFactory( |
| 25 |
routerType, |
| 26 |
createExampleRouterDefaultConfig, |
| 27 |
xconnector.WithTracesToTraces(createExampleTracesRouter, component.StabilityLevelDevelopment), |
| 28 |
xconnector.WithMetricsToMetrics(createExampleMetricsRouter, component.StabilityLevelDevelopment), |
| 29 |
xconnector.WithLogsToLogs(createExampleLogsRouter, component.StabilityLevelDevelopment), |
| 30 |
xconnector.WithProfilesToProfiles(createExampleProfilesRouter, component.StabilityLevelDevelopment), |
| 31 |
) |
| 32 |
|
| 33 |
type LeftRightConfig struct { |
| 34 |
Left pipeline.ID `mapstructure:"left"` |
| 35 |
Right pipeline.ID `mapstructure:"right"` |
| 36 |
} |
| 37 |
|
| 38 |
type ExampleRouterConfig struct { |
| 39 |
Traces *LeftRightConfig `mapstructure:"traces"` |
| 40 |
Metrics *LeftRightConfig `mapstructure:"metrics"` |
| 41 |
Logs *LeftRightConfig `mapstructure:"logs"` |
| 42 |
Profiles *LeftRightConfig `mapstructure:"profiles"` |
| 43 |
} |
| 44 |
|
| 45 |
func createExampleRouterDefaultConfig() component.Config { |
| 46 |
return &ExampleRouterConfig{} |
| 47 |
} |
| 48 |
|
| 49 |
func createExampleTracesRouter(_ context.Context, _ connector.Settings, cfg component.Config, traces consumer.Traces) (connector.Traces, error) { |
| 50 |
c := cfg.(ExampleRouterConfig) |
| 51 |
r := traces.(connector.TracesRouterAndConsumer) |
| 52 |
left, _ := r.Consumer(c.Traces.Left) |
| 53 |
right, _ := r.Consumer(c.Traces.Right) |
| 54 |
return &ExampleRouter{ |
| 55 |
tracesRight: right, |
| 56 |
tracesLeft: left, |
| 57 |
}, nil |
| 58 |
} |
| 59 |
|
| 60 |
func createExampleMetricsRouter(_ context.Context, _ connector.Settings, cfg component.Config, metrics consumer.Metrics) (connector.Metrics, error) { |
| 61 |
c := cfg.(ExampleRouterConfig) |
| 62 |
r := metrics.(connector.MetricsRouterAndConsumer) |
| 63 |
left, _ := r.Consumer(c.Metrics.Left) |
| 64 |
right, _ := r.Consumer(c.Metrics.Right) |
| 65 |
return &ExampleRouter{ |
| 66 |
metricsRight: right, |
| 67 |
metricsLeft: left, |
| 68 |
}, nil |
| 69 |
} |
| 70 |
|
| 71 |
func createExampleLogsRouter(_ context.Context, _ connector.Settings, cfg component.Config, logs consumer.Logs) (connector.Logs, error) { |
| 72 |
c := cfg.(ExampleRouterConfig) |
| 73 |
r := logs.(connector.LogsRouterAndConsumer) |
| 74 |
left, _ := r.Consumer(c.Logs.Left) |
| 75 |
right, _ := r.Consumer(c.Logs.Right) |
| 76 |
return &ExampleRouter{ |
| 77 |
logsRight: right, |
| 78 |
logsLeft: left, |
| 79 |
}, nil |
| 80 |
} |
| 81 |
|
| 82 |
func createExampleProfilesRouter(_ context.Context, _ connector.Settings, cfg component.Config, profiles xconsumer.Profiles) (xconnector.Profiles, error) { |
| 83 |
c := cfg.(ExampleRouterConfig) |
| 84 |
r := profiles.(xconnector.ProfilesRouterAndConsumer) |
| 85 |
left, _ := r.Consumer(c.Profiles.Left) |
| 86 |
right, _ := r.Consumer(c.Profiles.Right) |
| 87 |
return &ExampleRouter{ |
| 88 |
profilesRight: right, |
| 89 |
profilesLeft: left, |
| 90 |
}, nil |
| 91 |
} |
| 92 |
|
| 93 |
type ExampleRouter struct { |
| 94 |
componentState |
| 95 |
|
| 96 |
tracesRight consumer.Traces |
| 97 |
tracesLeft consumer.Traces |
| 98 |
tracesNum int |
| 99 |
|
| 100 |
metricsRight consumer.Metrics |
| 101 |
metricsLeft consumer.Metrics |
| 102 |
metricsNum int |
| 103 |
|
| 104 |
logsRight consumer.Logs |
| 105 |
logsLeft consumer.Logs |
| 106 |
logsNum int |
| 107 |
|
| 108 |
profilesRight xconsumer.Profiles |
| 109 |
profilesLeft xconsumer.Profiles |
| 110 |
profilesNum int |
| 111 |
} |
| 112 |
|
| 113 |
func (r *ExampleRouter) ConsumeTraces(ctx context.Context, td ptrace.Traces) error { |
| 114 |
r.tracesNum++ |
| 115 |
if r.tracesNum%2 == 0 { |
| 116 |
return r.tracesLeft.ConsumeTraces(ctx, td) |
| 117 |
} |
| 118 |
return r.tracesRight.ConsumeTraces(ctx, td) |
| 119 |
} |
| 120 |
|
| 121 |
func (r *ExampleRouter) ConsumeMetrics(ctx context.Context, md pmetric.Metrics) error { |
| 122 |
r.metricsNum++ |
| 123 |
if r.metricsNum%2 == 0 { |
| 124 |
return r.metricsLeft.ConsumeMetrics(ctx, md) |
| 125 |
} |
| 126 |
return r.metricsRight.ConsumeMetrics(ctx, md) |
| 127 |
} |
| 128 |
|
| 129 |
func (r *ExampleRouter) ConsumeLogs(ctx context.Context, ld plog.Logs) error { |
| 130 |
r.logsNum++ |
| 131 |
if r.logsNum%2 == 0 { |
| 132 |
return r.logsLeft.ConsumeLogs(ctx, ld) |
| 133 |
} |
| 134 |
return r.logsRight.ConsumeLogs(ctx, ld) |
| 135 |
} |
| 136 |
|
| 137 |
func (r *ExampleRouter) ConsumeProfiles(ctx context.Context, td pprofile.Profiles) error { |
| 138 |
r.profilesNum++ |
| 139 |
if r.profilesNum%2 == 0 { |
| 140 |
return r.profilesLeft.ConsumeProfiles(ctx, td) |
| 141 |
} |
| 142 |
return r.profilesRight.ConsumeProfiles(ctx, td) |
| 143 |
} |
| 144 |
|
| 145 |
func (r *ExampleRouter) Capabilities() consumer.Capabilities { |
| 146 |
return consumer.Capabilities{MutatesData: false} |
| 147 |
} |