Debian Codegraph @

connector/internal/router.go

raw symbols SCIP
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package internal // import "go.opentelemetry.io/collector/connector/internal"
5
6 import (
7 "errors"
8 "fmt"
9 "maps"
10
11 "go.uber.org/multierr"
12
13 "go.opentelemetry.io/collector/pipeline"
14 )
15
16 type BaseRouter[T any] struct {
17 fanout func([]T) T
18 Consumers map[pipeline.ID]T
19 }
20
21 func NewBaseRouter[T any](fanout func([]T) T, cm map[pipeline.ID]T) BaseRouter[T] {
22 consumers := make(map[pipeline.ID]T, len(cm))
23 maps.Copy(consumers, cm)
24 return BaseRouter[T]{fanout: fanout, Consumers: consumers}
25 }
26
27 func (r *BaseRouter[T]) PipelineIDs() []pipeline.ID {
28 ids := make([]pipeline.ID, 0, len(r.Consumers))
29 for id := range r.Consumers {
30 ids = append(ids, id)
31 }
32 return ids
33 }
34
35 func (r *BaseRouter[T]) Consumer(pipelineIDs ...pipeline.ID) (T, error) {
36 var ret T
37 if len(pipelineIDs) == 0 {
38 return ret, errors.New("missing consumers")
39 }
40 consumers := make([]T, 0, len(pipelineIDs))
41 var errors error
42 for _, pipelineID := range pipelineIDs {
43 c, ok := r.Consumers[pipelineID]
44 if ok {
45 consumers = append(consumers, c)
46 } else {
47 errors = multierr.Append(errors, fmt.Errorf("missing consumer: %q", pipelineID))
48 }
49 }
50 if errors != nil {
51 // TODO potentially this could return a NewTraces with the valid consumers
52 return ret, errors
53 }
54 return r.fanout(consumers), nil
55 }