Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/component/component.go
4093 views
1
// Package component describes the interfaces which Flow components implement.
2
//
3
// A Flow component is a distinct piece of business logic that accepts inputs
4
// (Arguments) for its configuration and can optionally export a set of outputs
5
// (Exports).
6
//
7
// Arguments and Exports do not need to be static for the lifetime of a
8
// component. A component will be given a new Config if the runtime
9
// configuration changes. A component may also update its Exports throughout
10
// its lifetime, such as a component which outputs the current day of the week.
11
//
12
// Components are built by users with River, where they can use River
13
// expressions to refer to any input or exported field from other components.
14
// This allows users to connect components together to declaratively form a
15
// pipeline.
16
//
17
// # Defining Arguments and Exports structs
18
//
19
// Arguments and Exports implemented by new components must be able to be
20
// encoded to and from River. "river" struct field tags are used for encoding;
21
// refer to the package documentation at pkg/river for a description of how to
22
// write these tags.
23
//
24
// The set of River element names of a given component's Arguments and Exports
25
// types must not overlap. Additionally, the following River field and block
26
// names are reserved for use by the Flow controller:
27
//
28
// - for_each
29
// - enabled
30
// - health
31
// - debug
32
//
33
// Default values for Arguments may be provided by implementing
34
// river.Unmarshaler.
35
//
36
// # Arguments and Exports immutability
37
//
38
// Arguments passed to a component should be treated as immutable, as memory
39
// can be shared between components as an optimization. Components should make
40
// copies for fields they need to modify. An exception to this is for fields
41
// which are expected to be mutable (e.g., interfaces which expose a
42
// goroutine-safe API).
43
//
44
// Similarly, Exports and the fields within Exports must be considered
45
// immutable after they are written for the same reason.
46
//
47
// # Mapping River strings to custom types
48
//
49
// Custom encoding and decoding of fields is available by implementing
50
// encoding.TextMarshaler and encoding.TextUnmarshaler. Types implementing
51
// these interfaces will be represented as strings in River.
52
//
53
// # Component registration
54
//
55
// Components are registered globally by calling Register. These components are
56
// then made available by including them in the import path. The "all" child
57
// package imports all known component packages and should be updated when
58
// creating a new one.
59
package component
60
61
import (
62
"context"
63
"net/http"
64
)
65
66
// The Arguments contains the input fields for a specific component, which is
67
// unmarshaled from River.
68
//
69
// Refer to the package documentation for details around how to build proper
70
// Arguments implementations.
71
type Arguments interface{}
72
73
// Exports contains the current set of outputs for a specific component, which
74
// is then marshaled to River.
75
//
76
// Refer to the package documentation for details around how to build proper
77
// Exports implementations.
78
type Exports interface{}
79
80
// Component is the base interface for a Flow component. Components may
81
// implement extension interfaces (named <Extension>Component) to implement
82
// extra known behavior.
83
type Component interface {
84
// Run starts the component, blocking until ctx is canceled or the component
85
// suffers a fatal error. Run is guaranteed to be called exactly once per
86
// Component.
87
//
88
// Implementations of Component should perform any necessary cleanup before
89
// returning from Run.
90
Run(ctx context.Context) error
91
92
// Update provides a new Config to the component. The type of newConfig will
93
// always match the struct type which the component registers.
94
//
95
// Update will be called concurrently with Run. The component must be able to
96
// gracefully handle updating its config while still running.
97
//
98
// An error may be returned if the provided config is invalid.
99
Update(args Arguments) error
100
}
101
102
// DebugComponent is an extension interface for components which can report
103
// debugging information upon request.
104
type DebugComponent interface {
105
Component
106
107
// DebugInfo returns the current debug information of the component. May
108
// return nil if there is no debug info to currently report. The result of
109
// DebugInfo must be encodable to River like Arguments and Exports.
110
//
111
// Values from DebugInfo are not exposed to other components for use in
112
// expressions.
113
//
114
// DebugInfo must be safe for calling concurrently.
115
DebugInfo() interface{}
116
}
117
118
// HTTPComponent is an extension interface for components which contain their own HTTP handlers.
119
type HTTPComponent interface {
120
Component
121
122
// Handler should return a valid HTTP handler for the component.
123
// All requests to the component will have the path trimmed such that the component is at the root.
124
// For example, f a request is made to `/component/{id}/metrics`, the component
125
// will receive a request to just `/metrics`.
126
Handler() http.Handler
127
}
128
129
// ClusteredComponent is an extension interface for components which implement
130
// clustering-specific behavior.
131
type ClusteredComponent interface {
132
Component
133
134
ClusterUpdatesRegistration() bool
135
}
136
137