Skip to content

Commit

Permalink
[chore][pkg/stanza] Cleanup router operator files (open-telemetry#32069)
Browse files Browse the repository at this point in the history
  • Loading branch information
djaglowski authored and ycombinator committed Apr 9, 2024
1 parent 95e639a commit 164311a
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 199 deletions.
86 changes: 86 additions & 0 deletions pkg/stanza/operator/transformer/router/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package router // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/transformer/router"

import (
"fmt"

"go.uber.org/zap"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/helper"
)

const operatorType = "router"

func init() {
operator.Register(operatorType, func() operator.Builder { return NewConfig() })
}

// NewConfig config creates a new router operator config with default values
func NewConfig() *Config {
return NewConfigWithID(operatorType)
}

// NewConfigWithID config creates a new router operator config with default values
func NewConfigWithID(operatorID string) *Config {
return &Config{
BasicConfig: helper.NewBasicConfig(operatorID, operatorType),
}
}

// Config is the configuration of a router operator
type Config struct {
helper.BasicConfig `mapstructure:",squash"`
Routes []*RouteConfig `mapstructure:"routes"`
Default []string `mapstructure:"default"`
}

// RouteConfig is the configuration of a route on a router operator
type RouteConfig struct {
helper.AttributerConfig `mapstructure:",squash"`
Expression string `mapstructure:"expr"`
OutputIDs []string `mapstructure:"output"`
}

// Build will build a router operator from the supplied configuration
func (c Config) Build(logger *zap.SugaredLogger) (operator.Operator, error) {
basicOperator, err := c.BasicConfig.Build(logger)
if err != nil {
return nil, err
}

if c.Default != nil {
defaultRoute := &RouteConfig{
Expression: "true",
OutputIDs: c.Default,
}
c.Routes = append(c.Routes, defaultRoute)
}

routes := make([]*Route, 0, len(c.Routes))
for _, routeConfig := range c.Routes {
compiled, err := helper.ExprCompileBool(routeConfig.Expression)
if err != nil {
return nil, fmt.Errorf("failed to compile expression '%s': %w", routeConfig.Expression, err)
}

attributer, err := routeConfig.AttributerConfig.Build()
if err != nil {
return nil, fmt.Errorf("failed to build attributer for route '%s': %w", routeConfig.Expression, err)
}

route := Route{
Attributer: attributer,
Expression: compiled,
OutputIDs: routeConfig.OutputIDs,
}
routes = append(routes, &route)
}

return &Transformer{
BasicOperator: basicOperator,
routes: routes,
}, nil
}
199 changes: 0 additions & 199 deletions pkg/stanza/operator/transformer/router/router.go

This file was deleted.

Loading

0 comments on commit 164311a

Please sign in to comment.