/
logs_router.go
69 lines (59 loc) · 1.76 KB
/
logs_router.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package connector // import "go.opentelemetry.io/collector/connector"
import (
"fmt"
"go.uber.org/multierr"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/internal/fanoutconsumer"
)
// LogsRouterAndConsumer feeds the first consumer.Logs in each of the specified pipelines.
type LogsRouterAndConsumer interface {
consumer.Logs
Consumer(...component.ID) (consumer.Logs, error)
PipelineIDs() []component.ID
privateFunc()
}
type logsRouter struct {
consumer.Logs
consumers map[component.ID]consumer.Logs
}
func NewLogsRouter(cm map[component.ID]consumer.Logs) LogsRouterAndConsumer {
consumers := make([]consumer.Logs, 0, len(cm))
for _, consumer := range cm {
consumers = append(consumers, consumer)
}
return &logsRouter{
Logs: fanoutconsumer.NewLogs(consumers),
consumers: cm,
}
}
func (r *logsRouter) PipelineIDs() []component.ID {
ids := make([]component.ID, 0, len(r.consumers))
for id := range r.consumers {
ids = append(ids, id)
}
return ids
}
func (r *logsRouter) Consumer(pipelineIDs ...component.ID) (consumer.Logs, error) {
if len(pipelineIDs) == 0 {
return nil, fmt.Errorf("missing consumers")
}
consumers := make([]consumer.Logs, 0, len(pipelineIDs))
var errors error
for _, pipelineID := range pipelineIDs {
c, ok := r.consumers[pipelineID]
if ok {
consumers = append(consumers, c)
} else {
errors = multierr.Append(errors, fmt.Errorf("missing consumer: %q", pipelineID))
}
}
if errors != nil {
// TODO potentially this could return a NewLogs with the valid consumers
return nil, errors
}
return fanoutconsumer.NewLogs(consumers), nil
}
func (r *logsRouter) privateFunc() {}