forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[chore][pkg/stanza] Cleanup router operator files (open-telemetry#32069)
- Loading branch information
1 parent
adb1aa0
commit 27ad158
Showing
4 changed files
with
212 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.