Files
MarioandGitHub e0354d1bed Update Prometheus to v0.307.2, OTel collector contrib to v0.138.0, OTel to v1.44.0 (#5810)
* Update Prometheus to v0.307.2

* Update otel collector

* Update OTel collector
2025-10-28 11:30:17 +01:00

56 lines
1.3 KiB
Go

// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package internal // import "go.opentelemetry.io/collector/connector/internal"
import (
"errors"
"fmt"
"maps"
"go.uber.org/multierr"
"go.opentelemetry.io/collector/pipeline"
)
type BaseRouter[T any] struct {
fanout func([]T) T
Consumers map[pipeline.ID]T
}
func NewBaseRouter[T any](fanout func([]T) T, cm map[pipeline.ID]T) BaseRouter[T] {
consumers := make(map[pipeline.ID]T, len(cm))
maps.Copy(consumers, cm)
return BaseRouter[T]{fanout: fanout, Consumers: consumers}
}
func (r *BaseRouter[T]) PipelineIDs() []pipeline.ID {
ids := make([]pipeline.ID, 0, len(r.Consumers))
for id := range r.Consumers {
ids = append(ids, id)
}
return ids
}
func (r *BaseRouter[T]) Consumer(pipelineIDs ...pipeline.ID) (T, error) {
var ret T
if len(pipelineIDs) == 0 {
return ret, errors.New("missing consumers")
}
consumers := make([]T, 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 NewTraces with the valid consumers
return ret, errors
}
return r.fanout(consumers), nil
}