-
Notifications
You must be signed in to change notification settings - Fork 238
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
9 changed files
with
304 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,67 @@ | ||
package testcomponents | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/go-kit/log" | ||
"github.com/grafana/alloy/internal/component" | ||
"github.com/grafana/alloy/internal/featuregate" | ||
) | ||
|
||
func init() { | ||
component.Register(component.Registration{ | ||
Name: "testcomponents.summation_entry", | ||
Stability: featuregate.StabilityPublicPreview, | ||
Args: SummationConfig_Entry{}, | ||
Exports: SummationExports_Entry{}, | ||
|
||
Build: func(opts component.Options, args component.Arguments) (component.Component, error) { | ||
return NewSummation_Entry(opts, args.(SummationConfig_Entry)) | ||
}, | ||
}) | ||
} | ||
|
||
// Accepts a single integer input and forwards it to all the components listed in forward_to. | ||
type SummationConfig_Entry struct { | ||
Input int `alloy:"input,attr"` | ||
//TODO: What should the type be? | ||
ForwardTo []IntReceiver `alloy:"forward_to,attr"` | ||
} | ||
|
||
type SummationExports_Entry struct { | ||
} | ||
|
||
type Summation_Entry struct { | ||
opts component.Options | ||
log log.Logger | ||
} | ||
|
||
// NewSummation creates a new summation component. | ||
func NewSummation_Entry(o component.Options, cfg SummationConfig_Entry) (*Summation_Entry, error) { | ||
t := &Summation_Entry{opts: o, log: o.Logger} | ||
if err := t.Update(cfg); err != nil { | ||
return nil, err | ||
} | ||
return t, nil | ||
} | ||
|
||
var ( | ||
_ component.Component = (*Summation_Entry)(nil) | ||
) | ||
|
||
// Run implements Component. | ||
func (t *Summation_Entry) Run(ctx context.Context) error { | ||
<-ctx.Done() | ||
return nil | ||
} | ||
|
||
// Update implements Component. | ||
func (t *Summation_Entry) Update(args component.Arguments) error { | ||
c := args.(SummationConfig_Entry) | ||
|
||
for _, r := range c.ForwardTo { | ||
r.ReceiveInt(c.Input) | ||
} | ||
|
||
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,81 @@ | ||
package testcomponents | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/go-kit/log" | ||
"github.com/grafana/alloy/internal/component" | ||
"github.com/grafana/alloy/internal/featuregate" | ||
"go.uber.org/atomic" | ||
) | ||
|
||
func init() { | ||
component.Register(component.Registration{ | ||
Name: "testcomponents.summation2", | ||
Stability: featuregate.StabilityPublicPreview, | ||
Args: SummationConfig_2{}, | ||
Exports: SummationExports_2{}, | ||
|
||
Build: func(opts component.Options, args component.Arguments) (component.Component, error) { | ||
return NewSummation_2(opts, args.(SummationConfig_2)) | ||
}, | ||
}) | ||
} | ||
|
||
type IntReceiver interface { | ||
ReceiveInt(int) | ||
} | ||
|
||
type IntReceiverImpl struct { | ||
sum atomic.Int32 | ||
} | ||
|
||
func (r IntReceiverImpl) ReceiveInt(i int) { | ||
r.sum.Add(int32(i)) | ||
} | ||
|
||
type SummationConfig_2 struct { | ||
} | ||
|
||
type SummationExports_2 struct { | ||
Receiver IntReceiver `alloy:"receiver,attr"` | ||
Sum int `alloy:"sum,attr"` | ||
LastAdded int `alloy:"last_added,attr"` | ||
} | ||
|
||
type Summation_2 struct { | ||
opts component.Options | ||
log log.Logger | ||
receiver IntReceiver | ||
} | ||
|
||
// NewSummation creates a new summation component. | ||
func NewSummation_2(o component.Options, cfg SummationConfig_2) (*Summation_2, error) { | ||
recv := IntReceiverImpl{} | ||
o.OnStateChange(SummationExports_2{ | ||
Receiver: recv, | ||
}) | ||
|
||
t := &Summation_2{ | ||
opts: o, | ||
log: o.Logger, | ||
receiver: recv, | ||
} | ||
|
||
return t, nil | ||
} | ||
|
||
var ( | ||
_ component.Component = (*Summation)(nil) | ||
) | ||
|
||
// Run implements Component. | ||
func (t *Summation_2) Run(ctx context.Context) error { | ||
<-ctx.Done() | ||
return nil | ||
} | ||
|
||
// Update implements Component. | ||
func (t *Summation_2) Update(args component.Arguments) error { | ||
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,12 @@ | ||
declare "config" { | ||
argument "input" {} | ||
|
||
testcomponents.passthrough "pt" { | ||
input = argument.input.value | ||
lag = "1ms" | ||
} | ||
|
||
export "output" { | ||
value = testcomponents.passthrough.pt.output | ||
} | ||
} |
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" { | ||
} |