-
Notifications
You must be signed in to change notification settings - Fork 245
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
238 additions
and
1 deletion.
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
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
47 changes: 47 additions & 0 deletions
47
internal/runtime/internal/controller/node_config_foreach.go
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,47 @@ | ||
package controller | ||
|
||
import ( | ||
"github.com/grafana/alloy/syntax/ast" | ||
"github.com/grafana/alloy/syntax/vm" | ||
) | ||
|
||
type ForeachConfigNode struct { | ||
nodeID string | ||
label string | ||
block *ast.BlockStmt // Current Alloy blocks to derive config from | ||
} | ||
|
||
var _ BlockNode = (*ForeachConfigNode)(nil) | ||
|
||
// For now the Foreach doesn't have the ability to export arguments. | ||
//TODO: We could implement this in the future? | ||
|
||
type ForeachArguments struct { | ||
Collection string `alloy:"collection,attr` | ||
//TODO: Is the "var" argument really needed? | ||
// We could just have a variable with a fixed name referencing the current thing we are iterating over. | ||
Var string `alloy:"var,attr,optional` | ||
} | ||
|
||
func NewForeachConfigNode(block *ast.BlockStmt, globals ComponentGlobals) *ForeachConfigNode { | ||
nodeID := BlockComponentID(block).String() | ||
|
||
return &ForeachConfigNode{ | ||
nodeID: nodeID, | ||
label: block.Label, | ||
block: block, | ||
} | ||
} | ||
|
||
func (fn *ForeachConfigNode) Label() string { return fn.label } | ||
|
||
func (fn *ForeachConfigNode) NodeID() string { return fn.nodeID } | ||
|
||
func (fn *ForeachConfigNode) Block() *ast.BlockStmt { return fn.block } | ||
|
||
func (fn *ForeachConfigNode) Evaluate(scope *vm.Scope) error { | ||
return nil | ||
} | ||
|
||
func (fn *ForeachConfigNode) UpdateBlock(b *ast.BlockStmt) { | ||
} |
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,73 @@ | ||
package testcomponents | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/go-kit/log" | ||
"github.com/grafana/alloy/internal/component" | ||
"github.com/grafana/alloy/internal/featuregate" | ||
"github.com/grafana/alloy/internal/runtime/logging/level" | ||
"go.uber.org/atomic" | ||
) | ||
|
||
func init() { | ||
component.Register(component.Registration{ | ||
Name: "testcomponents.summation1", | ||
Stability: featuregate.StabilityPublicPreview, | ||
Args: SummationConfig_1{}, | ||
Exports: SummationExports_1{}, | ||
|
||
Build: func(opts component.Options, args component.Arguments) (component.Component, error) { | ||
return NewSummation(opts, args.(SummationConfig)) | ||
}, | ||
}) | ||
} | ||
|
||
type SummationConfig_1 struct { | ||
Input int `alloy:"input,attr"` | ||
//TODO: What should the type be? | ||
ForwardTo []string `alloy:"forward_to,attr"` | ||
} | ||
|
||
type SummationExports_1 struct { | ||
Sum int `alloy:"sum,attr"` | ||
LastAdded int `alloy:"last_added,attr"` | ||
} | ||
|
||
type Summation_1 struct { | ||
opts component.Options | ||
log log.Logger | ||
sum atomic.Int32 | ||
} | ||
|
||
// NewSummation creates a new summation component. | ||
func NewSummation_1(o component.Options, cfg SummationConfig) (*Summation, error) { | ||
t := &Summation{opts: o, log: o.Logger} | ||
if err := t.Update(cfg); err != nil { | ||
return nil, err | ||
} | ||
return t, nil | ||
} | ||
|
||
var ( | ||
_ component.Component = (*Summation)(nil) | ||
) | ||
|
||
// Run implements Component. | ||
func (t *Summation_1) Run(ctx context.Context) error { | ||
<-ctx.Done() | ||
return nil | ||
} | ||
|
||
// Update implements Component. | ||
func (t *Summation_1) Update(args component.Arguments) error { | ||
c := args.(SummationConfig) | ||
newSum := int(t.sum.Add(int32(c.Input))) | ||
|
||
level.Info(t.log).Log("msg", "updated sum", "value", newSum, "input", c.Input) | ||
t.opts.OnStateChange(SummationExports{Sum: newSum, LastAdded: c.Input}) | ||
|
||
//TODO: Send the data over to all the receivers listed in forward_to | ||
|
||
return nil | ||
} |
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,4 @@ | ||
package testcomponents | ||
|
||
//TODO: Implement the component proposed in: | ||
// alloy/internal/runtime/testdata/foreach/foreach_1.txtar |
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,17 @@ | ||
-- main.alloy -- | ||
foreach "testForeach" { | ||
collection = [1, 2, 3, 4] | ||
//var = "num" | ||
|
||
// Similar to testcomponents.summation, but with a "forward_to" | ||
testcomponents.summation1 "sum" { | ||
//TODO: Use the num variable here | ||
// input = num | ||
input = 1 | ||
forward_to = testcomponents.summation2.final.receiver | ||
} | ||
} | ||
|
||
// Similar to testcomponents.summation, but with a "receiver" export | ||
testcomponents.summation2 "final" { | ||
} |