Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/flow/internal/controller/config_export.go
4095 views
1
package controller
2
3
import (
4
"fmt"
5
"sync"
6
7
"github.com/grafana/agent/pkg/river/ast"
8
"github.com/grafana/agent/pkg/river/vm"
9
)
10
11
type ExportConfigNode struct {
12
label string
13
nodeID string
14
componentName string
15
16
mut sync.RWMutex
17
block *ast.BlockStmt // Current River blocks to derive config from
18
eval *vm.Evaluator
19
value any
20
}
21
22
var _ BlockNode = (*ExportConfigNode)(nil)
23
24
// NewExportConfigNode creates a new ExportConfigNode from an initial ast.BlockStmt.
25
// The underlying config isn't applied until Evaluate is called.
26
func NewExportConfigNode(block *ast.BlockStmt, globals ComponentGlobals) *ExportConfigNode {
27
return &ExportConfigNode{
28
label: block.Label,
29
nodeID: BlockComponentID(block).String(),
30
componentName: block.GetBlockName(),
31
32
block: block,
33
eval: vm.New(block.Body),
34
}
35
}
36
37
type exportBlock struct {
38
Value any `river:"value,attr"`
39
}
40
41
// Evaluate implements BlockNode and updates the arguments for the managed config block
42
// by re-evaluating its River block with the provided scope. The managed config block
43
// will be built the first time Evaluate is called.
44
//
45
// Evaluate will return an error if the River block cannot be evaluated or if
46
// decoding to arguments fails.
47
func (cn *ExportConfigNode) Evaluate(scope *vm.Scope) error {
48
cn.mut.Lock()
49
defer cn.mut.Unlock()
50
51
var export exportBlock
52
if err := cn.eval.Evaluate(scope, &export); err != nil {
53
return fmt.Errorf("decoding River: %w", err)
54
}
55
cn.value = export.Value
56
return nil
57
}
58
59
func (cn *ExportConfigNode) Label() string { return cn.label }
60
61
// Value returns the value of the export.
62
func (cn *ExportConfigNode) Value() any {
63
cn.mut.RLock()
64
defer cn.mut.RUnlock()
65
return cn.value
66
}
67
68
// Block implements BlockNode and returns the current block of the managed config node.
69
func (cn *ExportConfigNode) Block() *ast.BlockStmt {
70
cn.mut.RLock()
71
defer cn.mut.RUnlock()
72
return cn.block
73
}
74
75
// NodeID implements dag.Node and returns the unique ID for the config node.
76
func (cn *ExportConfigNode) NodeID() string { return cn.nodeID }
77
78