-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[pkg/ottl] Define functions using a Factory interface (#18822)
Convert OTTL factories to be defined with an interface Co-authored-by: Evan Bradley <evan-bradley@users.noreply.github.com>
- Loading branch information
1 parent
546c0da
commit b2290e6
Showing
69 changed files
with
1,320 additions
and
424 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,18 @@ | ||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: breaking | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) | ||
component: pkg/ottl | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Reimplement all OTTL function factories to implement the `ottl.Factory` interface. | ||
|
||
# One or more tracking issues related to the change | ||
issues: [14712] | ||
|
||
# (Optional) One or more lines of additional information to render under the primary note. | ||
# These lines will be padded with 2 spaces and then inserted directly into the document. | ||
# Use pipe (|) for multiline entries. | ||
subtext: | | ||
The `ottl.Factory` interface allows making factories extendable and defines | ||
canonical names for the functions across components using the OTTL. |
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
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
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
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
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
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
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
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
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
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
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,96 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package ottl // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" | ||
|
||
import "go.opentelemetry.io/collector/component" | ||
|
||
// Arguments holds the arguments for an OTTL function, with arguments | ||
// specified as fields on a struct. Argument ordering is defined | ||
type Arguments interface{} | ||
|
||
// FunctionContext contains data provided by the Collector | ||
// component to the OTTL for use in functions. | ||
type FunctionContext struct { | ||
Set component.TelemetrySettings | ||
} | ||
|
||
// Factory defines an OTTL function factory that will generate an OTTL | ||
// function to be called based on an invocation within a statement. | ||
type Factory[K any] interface { | ||
// Name is the canonical name to be used by the user when invocating | ||
// the function generated by this Factory. | ||
Name() string | ||
|
||
// CreateDefaultArguments initializes an Arguments struct specific to this | ||
// Factory containing the arguments for the function. | ||
CreateDefaultArguments() Arguments | ||
|
||
// CreateFunction creates an OTTL function that will use the given Arguments. | ||
CreateFunction(fCtx FunctionContext, args Arguments) (ExprFunc[K], error) | ||
|
||
// Disallow implementations outside this package. | ||
unexportedFactoryFunc() | ||
} | ||
|
||
type CreateFunctionFunc[K any] func(fCtx FunctionContext, args Arguments) (ExprFunc[K], error) | ||
|
||
type factory[K any] struct { | ||
name string | ||
args Arguments | ||
createFunctionFunc CreateFunctionFunc[K] | ||
} | ||
|
||
// nolint:unused | ||
func (f *factory[K]) unexportedFactoryFunc() {} | ||
|
||
func (f *factory[K]) Name() string { | ||
return f.name | ||
} | ||
|
||
func (f *factory[K]) CreateDefaultArguments() Arguments { | ||
return f.args | ||
} | ||
|
||
func (f *factory[K]) CreateFunction(fCtx FunctionContext, args Arguments) (ExprFunc[K], error) { | ||
return f.createFunctionFunc(fCtx, args) | ||
} | ||
|
||
type FactoryOption[K any] func(factory *factory[K]) | ||
|
||
func NewFactory[K any](name string, args Arguments, createFunctionFunc CreateFunctionFunc[K], options ...FactoryOption[K]) Factory[K] { | ||
f := &factory[K]{ | ||
name: name, | ||
args: args, | ||
createFunctionFunc: createFunctionFunc, | ||
} | ||
|
||
for _, option := range options { | ||
option(f) | ||
} | ||
|
||
return f | ||
} | ||
|
||
// CreateFactoryMap takes a list of factories and returns a map of Factories | ||
// keyed on their canonical names. | ||
func CreateFactoryMap[K any](factories ...Factory[K]) map[string]Factory[K] { | ||
factoryMap := map[string]Factory[K]{} | ||
|
||
for _, fn := range factories { | ||
factoryMap[fn.Name()] = fn | ||
} | ||
|
||
return factoryMap | ||
} |
Oops, something went wrong.