-
Notifications
You must be signed in to change notification settings - Fork 85
/
node_config_export.go
93 lines (77 loc) · 2.53 KB
/
node_config_export.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package controller
import (
"fmt"
"strings"
"sync"
"github.com/grafana/alloy/syntax/ast"
"github.com/grafana/alloy/syntax/vm"
)
type ExportConfigNode struct {
label string
nodeID string
componentName string
mut sync.RWMutex
block *ast.BlockStmt // Current Alloy blocks to derive config from
eval *vm.Evaluator
value any
}
var _ BlockNode = (*ExportConfigNode)(nil)
// NewExportConfigNode creates a new ExportConfigNode from an initial ast.BlockStmt.
// The underlying config isn't applied until Evaluate is called.
func NewExportConfigNode(block *ast.BlockStmt, globals ComponentGlobals) *ExportConfigNode {
return &ExportConfigNode{
label: block.Label,
nodeID: BlockComponentID(block).String(),
componentName: block.GetBlockName(),
block: block,
eval: vm.New(block.Body),
}
}
type exportBlock struct {
Value any `alloy:"value,attr"`
}
// Evaluate implements BlockNode and updates the arguments for the managed config block
// by re-evaluating its Alloy block with the provided scope. The managed config block
// will be built the first time Evaluate is called.
//
// Evaluate will return an error if the Alloy block cannot be evaluated or if
// decoding to arguments fails.
func (cn *ExportConfigNode) Evaluate(scope *vm.Scope) error {
cn.mut.Lock()
defer cn.mut.Unlock()
var export exportBlock
if err := cn.eval.Evaluate(scope, &export); err != nil {
return fmt.Errorf("decoding configuration: %w", err)
}
cn.value = export.Value
return nil
}
func (cn *ExportConfigNode) Label() string { return cn.label }
// Value returns the value of the export.
func (cn *ExportConfigNode) Value() any {
cn.mut.RLock()
defer cn.mut.RUnlock()
return cn.value
}
// Block implements BlockNode and returns the current block of the managed config node.
func (cn *ExportConfigNode) Block() *ast.BlockStmt {
cn.mut.RLock()
defer cn.mut.RUnlock()
return cn.block
}
// NodeID implements dag.Node and returns the unique ID for the config node.
func (cn *ExportConfigNode) NodeID() string { return cn.nodeID }
// UpdateBlock updates the Alloy block used to construct arguments.
// The new block isn't used until the next time Evaluate is invoked.
//
// UpdateBlock will panic if the block does not match the component ID of the
// ExportConfigNode.
func (cn *ExportConfigNode) UpdateBlock(b *ast.BlockStmt) {
if !BlockComponentID(b).Equals(strings.Split(cn.nodeID, ".")) {
panic("UpdateBlock called with an Alloy block with a different ID")
}
cn.mut.Lock()
defer cn.mut.Unlock()
cn.block = b
cn.eval = vm.New(b.Body)
}