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