Path: blob/main/pkg/flow/internal/controller/config_logging.go
4095 views
package controller12import (3"fmt"4"sync"56"github.com/grafana/agent/pkg/flow/logging"7"github.com/grafana/agent/pkg/river/ast"8"github.com/grafana/agent/pkg/river/vm"9)1011type LoggingConfigNode struct {12nodeID string13componentName string14logSink *logging.Sink // Sink used for Logging.1516mut sync.RWMutex17block *ast.BlockStmt // Current River blocks to derive config from18eval *vm.Evaluator19}2021// NewLoggingConfigNode creates a new LoggingConfigNode from an initial ast.BlockStmt.22// The underlying config isn't applied until Evaluate is called.23func NewLoggingConfigNode(block *ast.BlockStmt, globals ComponentGlobals) *LoggingConfigNode {24return &LoggingConfigNode{25nodeID: BlockComponentID(block).String(),26componentName: block.GetBlockName(),27logSink: globals.LogSink,2829block: block,30eval: vm.New(block.Body),31}32}3334// NewDefaultLoggingConfigNode creates a new LoggingConfigNode with nil block and eval.35// This will force evaluate to use the default logging options for this node.36func NewDefaultLoggingConfigNode(globals ComponentGlobals) *LoggingConfigNode {37return &LoggingConfigNode{38nodeID: loggingBlockID,39componentName: loggingBlockID,40logSink: globals.LogSink,4142block: nil,43eval: nil,44}45}4647// Evaluate implements BlockNode and updates the arguments for the managed config block48// by re-evaluating its River block with the provided scope. The managed config block49// will be built the first time Evaluate is called.50//51// Evaluate will return an error if the River block cannot be evaluated or if52// decoding to arguments fails.53func (cn *LoggingConfigNode) Evaluate(scope *vm.Scope) error {54cn.mut.RLock()55defer cn.mut.RUnlock()56args := logging.DefaultSinkOptions57if cn.eval != nil {58if err := cn.eval.Evaluate(scope, &args); err != nil {59return fmt.Errorf("decoding River: %w", err)60}61}6263if err := cn.logSink.Update(args); err != nil {64return fmt.Errorf("could not update logger: %w", err)65}6667return nil68}6970// Block implements BlockNode and returns the current block of the managed config node.71func (cn *LoggingConfigNode) Block() *ast.BlockStmt {72cn.mut.RLock()73defer cn.mut.RUnlock()74return cn.block75}7677// NodeID implements dag.Node and returns the unique ID for the config node.78func (cn *LoggingConfigNode) NodeID() string { return cn.nodeID }798081