Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/flow/config.go
4093 views
1
package flow
2
3
import (
4
"fmt"
5
"strings"
6
7
"github.com/grafana/agent/pkg/river/ast"
8
"github.com/grafana/agent/pkg/river/diag"
9
"github.com/grafana/agent/pkg/river/parser"
10
)
11
12
// An Argument is an input to a Flow module.
13
type Argument struct {
14
// Name of the argument.
15
Name string `river:",label"`
16
17
// Whether the Argument must be provided when evaluating the file.
18
Optional bool `river:"optional,attr,optional"`
19
20
// Description for the Argument.
21
Comment string `river:"comment,attr,optional"`
22
23
// Default value for the argument.
24
Default any `river:"default,attr,optional"`
25
}
26
27
// File holds the contents of a parsed Flow file.
28
type File struct {
29
Name string // File name given to ReadFile.
30
Node *ast.File // Raw File node.
31
32
// Components holds the list of raw River AST blocks describing components.
33
// The Flow controller can interpret them.
34
Components []*ast.BlockStmt
35
ConfigBlocks []*ast.BlockStmt
36
}
37
38
// ReadFile parses the River file specified by bb into a File. name should be
39
// the name of the file used for reporting errors.
40
func ReadFile(name string, bb []byte) (*File, error) {
41
node, err := parser.ParseFile(name, bb)
42
if err != nil {
43
return nil, err
44
}
45
46
// Look for predefined non-components blocks (i.e., logging), and store
47
// everything else into a list of components.
48
//
49
// TODO(rfratto): should this code be brought into a helper somewhere? Maybe
50
// in ast?
51
var (
52
components []*ast.BlockStmt
53
configs []*ast.BlockStmt
54
)
55
56
for _, stmt := range node.Body {
57
switch stmt := stmt.(type) {
58
case *ast.AttributeStmt:
59
return nil, diag.Diagnostic{
60
Severity: diag.SeverityLevelError,
61
StartPos: ast.StartPos(stmt.Name).Position(),
62
EndPos: ast.EndPos(stmt.Name).Position(),
63
Message: "unrecognized attribute " + stmt.Name.Name,
64
}
65
66
case *ast.BlockStmt:
67
fullName := strings.Join(stmt.Name, ".")
68
switch fullName {
69
case "logging":
70
configs = append(configs, stmt)
71
case "tracing":
72
configs = append(configs, stmt)
73
case "argument":
74
configs = append(configs, stmt)
75
case "export":
76
configs = append(configs, stmt)
77
default:
78
components = append(components, stmt)
79
}
80
81
default:
82
return nil, diag.Diagnostic{
83
Severity: diag.SeverityLevelError,
84
StartPos: ast.StartPos(stmt).Position(),
85
EndPos: ast.EndPos(stmt).Position(),
86
Message: fmt.Sprintf("unsupported statement type %T", stmt),
87
}
88
}
89
}
90
91
return &File{
92
Name: name,
93
Node: node,
94
Components: components,
95
ConfigBlocks: configs,
96
}, nil
97
}
98
99