Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[chore] remove duplicate code from the connector #10082

Merged
merged 4 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions connector/logs_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ type LogsRouterAndConsumer interface {

type logsRouter struct {
consumer.Logs
consumers map[component.ID]consumer.Logs
baseRouter[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)
for _, cons := range cm {
consumers = append(consumers, cons)
}
return &logsRouter{
Logs: fanoutconsumer.NewLogs(consumers),
consumers: cm,
Logs: fanoutconsumer.NewLogs(consumers),
baseRouter: newBaseRouter(fanoutconsumer.NewLogs, cm),
}
}

Expand Down
39 changes: 3 additions & 36 deletions connector/metrics_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
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"
Expand All @@ -23,7 +19,7 @@ type MetricsRouterAndConsumer interface {

type metricsRouter struct {
consumer.Metrics
consumers map[component.ID]consumer.Metrics
baseRouter[consumer.Metrics]
}

func NewMetricsRouter(cm map[component.ID]consumer.Metrics) MetricsRouterAndConsumer {
Expand All @@ -32,38 +28,9 @@ func NewMetricsRouter(cm map[component.ID]consumer.Metrics) MetricsRouterAndCons
consumers = append(consumers, cons)
}
return &metricsRouter{
Metrics: fanoutconsumer.NewMetrics(consumers),
consumers: cm,
}
}

func (r *metricsRouter) PipelineIDs() []component.ID {
ids := make([]component.ID, 0, len(r.consumers))
for id := range r.consumers {
ids = append(ids, id)
}
return ids
}

func (r *metricsRouter) Consumer(pipelineIDs ...component.ID) (consumer.Metrics, error) {
if len(pipelineIDs) == 0 {
return nil, fmt.Errorf("missing consumers")
}
consumers := make([]consumer.Metrics, 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 NewMetrics with the valid consumers
return nil, errors
Metrics: fanoutconsumer.NewMetrics(consumers),
baseRouter: newBaseRouter(fanoutconsumer.NewMetrics, cm),
}
return fanoutconsumer.NewMetrics(consumers), nil
}

func (r *metricsRouter) privateFunc() {}
55 changes: 55 additions & 0 deletions connector/router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// 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"
)

type baseRouter[T any] struct {
fanout func([]T) T
consumers map[component.ID]T
}

func newBaseRouter[T any](fanout func([]T) T, cm map[component.ID]T) baseRouter[T] {
consumers := make(map[component.ID]T, len(cm))
for k, v := range cm {
consumers[k] = v
}
return baseRouter[T]{fanout: fanout, consumers: consumers}
}

func (r *baseRouter[T]) PipelineIDs() []component.ID {
ids := make([]component.ID, 0, len(r.consumers))
for id := range r.consumers {
ids = append(ids, id)
}
return ids
}

func (r *baseRouter[T]) Consumer(pipelineIDs ...component.ID) (T, error) {
var ret T
if len(pipelineIDs) == 0 {
return ret, fmt.Errorf("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
}
43 changes: 5 additions & 38 deletions connector/traces_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
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"
Expand All @@ -23,47 +19,18 @@ type TracesRouterAndConsumer interface {

type tracesRouter struct {
consumer.Traces
consumers map[component.ID]consumer.Traces
baseRouter[consumer.Traces]
}

func NewTracesRouter(cm map[component.ID]consumer.Traces) TracesRouterAndConsumer {
consumers := make([]consumer.Traces, 0, len(cm))
for _, c := range cm {
consumers = append(consumers, c)
for _, cons := range cm {
consumers = append(consumers, cons)
}
return &tracesRouter{
Traces: fanoutconsumer.NewTraces(consumers),
consumers: cm,
}
}

func (r *tracesRouter) PipelineIDs() []component.ID {
ids := make([]component.ID, 0, len(r.consumers))
for id := range r.consumers {
ids = append(ids, id)
}
return ids
}

func (r *tracesRouter) Consumer(pipelineIDs ...component.ID) (consumer.Traces, error) {
if len(pipelineIDs) == 0 {
return nil, fmt.Errorf("missing consumers")
}
consumers := make([]consumer.Traces, 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 nil, errors
Traces: fanoutconsumer.NewTraces(consumers),
baseRouter: newBaseRouter(fanoutconsumer.NewTraces, cm),
}
return fanoutconsumer.NewTraces(consumers), nil
}

func (r *tracesRouter) privateFunc() {}
Loading