| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package connector |
| 5 |
|
| 6 |
import ( |
| 7 |
"errors" |
| 8 |
"fmt" |
| 9 |
|
| 10 |
"go.uber.org/multierr" |
| 11 |
|
| 12 |
"go.opentelemetry.io/collector/connector/internal" |
| 13 |
"go.opentelemetry.io/collector/consumer" |
| 14 |
"go.opentelemetry.io/collector/internal/fanoutconsumer" |
| 15 |
"go.opentelemetry.io/collector/pipeline" |
| 16 |
) |
| 17 |
|
| 18 |
|
| 19 |
type LogsRouterAndConsumer interface { |
| 20 |
consumer.Logs |
| 21 |
Consumer(...pipeline.ID) (consumer.Logs, error) |
| 22 |
PipelineIDs() []pipeline.ID |
| 23 |
privateFunc() |
| 24 |
} |
| 25 |
|
| 26 |
type logsRouter struct { |
| 27 |
consumer.Logs |
| 28 |
internal.BaseRouter[consumer.Logs] |
| 29 |
} |
| 30 |
|
| 31 |
func NewLogsRouter(cm map[pipeline.ID]consumer.Logs) LogsRouterAndConsumer { |
| 32 |
consumers := make([]consumer.Logs, 0, len(cm)) |
| 33 |
for _, cons := range cm { |
| 34 |
consumers = append(consumers, cons) |
| 35 |
} |
| 36 |
return &logsRouter{ |
| 37 |
Logs: fanoutconsumer.NewLogs(consumers), |
| 38 |
BaseRouter: internal.NewBaseRouter(fanoutconsumer.NewLogs, cm), |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
func (r *logsRouter) PipelineIDs() []pipeline.ID { |
| 43 |
ids := make([]pipeline.ID, 0, len(r.Consumers)) |
| 44 |
for id := range r.Consumers { |
| 45 |
ids = append(ids, id) |
| 46 |
} |
| 47 |
return ids |
| 48 |
} |
| 49 |
|
| 50 |
func (r *logsRouter) Consumer(pipelineIDs ...pipeline.ID) (consumer.Logs, error) { |
| 51 |
if len(pipelineIDs) == 0 { |
| 52 |
return nil, errors.New("missing consumers") |
| 53 |
} |
| 54 |
consumers := make([]consumer.Logs, 0, len(pipelineIDs)) |
| 55 |
var errors error |
| 56 |
for _, pipelineID := range pipelineIDs { |
| 57 |
c, ok := r.Consumers[pipelineID] |
| 58 |
if ok { |
| 59 |
consumers = append(consumers, c) |
| 60 |
} else { |
| 61 |
errors = multierr.Append(errors, fmt.Errorf("missing consumer: %q", pipelineID)) |
| 62 |
} |
| 63 |
} |
| 64 |
if errors != nil { |
| 65 |
|
| 66 |
return nil, errors |
| 67 |
} |
| 68 |
return fanoutconsumer.NewLogs(consumers), nil |
| 69 |
} |
| 70 |
|
| 71 |
func (r *logsRouter) privateFunc() {} |