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