Path: blob/main/pkg/flow/internal/controller/config_tracing.go
4095 views
package controller12import (3"fmt"4"sync"56"github.com/grafana/agent/pkg/flow/tracing"7"github.com/grafana/agent/pkg/river/ast"8"github.com/grafana/agent/pkg/river/vm"9"go.opentelemetry.io/otel/trace"10)1112type TracingConfigNode struct {13nodeID string14componentName string15traceProvider trace.TracerProvider // Tracer shared between all managed components.1617mut sync.RWMutex18block *ast.BlockStmt // Current River blocks to derive config from19eval *vm.Evaluator20}2122// NewTracingConfigNode creates a new TracingConfigNode from an initial ast.BlockStmt.23// The underlying config isn't applied until Evaluate is called.24func NewTracingConfigNode(block *ast.BlockStmt, globals ComponentGlobals) *TracingConfigNode {25return &TracingConfigNode{26nodeID: BlockComponentID(block).String(),27componentName: block.GetBlockName(),28traceProvider: globals.TraceProvider,2930block: block,31eval: vm.New(block.Body),32}33}3435// NewDefaulTracingConfigNode creates a new TracingConfigNode with nil block and eval.36// This will force evaluate to use the default tracing options for this node.37func NewDefaulTracingConfigNode(globals ComponentGlobals) *TracingConfigNode {38return &TracingConfigNode{39nodeID: tracingBlockID,40componentName: tracingBlockID,41traceProvider: globals.TraceProvider,4243block: nil,44eval: nil,45}46}4748// Evaluate implements BlockNode and updates the arguments for the managed config block49// by re-evaluating its River block with the provided scope. The managed config block50// will be built the first time Evaluate is called.51//52// Evaluate will return an error if the River block cannot be evaluated or if53// decoding to arguments fails.54func (cn *TracingConfigNode) Evaluate(scope *vm.Scope) error {55cn.mut.RLock()56defer cn.mut.RUnlock()57args := tracing.DefaultOptions58if cn.eval != nil {59if err := cn.eval.Evaluate(scope, &args); err != nil {60return fmt.Errorf("decoding River: %w", err)61}62}6364t, ok := cn.traceProvider.(*tracing.Tracer)65if ok {66err := t.Update(args)67if err != nil {68return fmt.Errorf("could not update tracer: %v", err)69}70}7172return nil73}7475// Block implements BlockNode and returns the current block of the managed config node.76func (cn *TracingConfigNode) Block() *ast.BlockStmt {77cn.mut.RLock()78defer cn.mut.RUnlock()79return cn.block80}8182// NodeID implements dag.Node and returns the unique ID for the config node.83func (cn *TracingConfigNode) NodeID() string { return cn.nodeID }848586