Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/flow/internal/testcomponents/passthrough.go
4096 views
1
package testcomponents
2
3
import (
4
"context"
5
6
"github.com/go-kit/log"
7
"github.com/go-kit/log/level"
8
"github.com/grafana/agent/component"
9
)
10
11
func init() {
12
component.Register(component.Registration{
13
Name: "testcomponents.passthrough",
14
Args: PassthroughConfig{},
15
Exports: PassthroughExports{},
16
17
Build: func(opts component.Options, args component.Arguments) (component.Component, error) {
18
return NewPassthrough(opts, args.(PassthroughConfig))
19
},
20
})
21
}
22
23
// PassthroughConfig configures the testcomponents.passthrough component.
24
type PassthroughConfig struct {
25
Input string `river:"input,attr"`
26
}
27
28
// PassthroughExports describes exported fields for the
29
// testcomponents.passthrough component.
30
type PassthroughExports struct {
31
Output string `river:"output,attr,optional"`
32
}
33
34
// Passthrough implements the testcomponents.passthrough component, where it
35
// always emits its input as an output.
36
type Passthrough struct {
37
opts component.Options
38
log log.Logger
39
}
40
41
// NewPassthrough creates a new passthrough component.
42
func NewPassthrough(o component.Options, cfg PassthroughConfig) (*Passthrough, error) {
43
t := &Passthrough{opts: o, log: o.Logger}
44
if err := t.Update(cfg); err != nil {
45
return nil, err
46
}
47
return t, nil
48
}
49
50
var (
51
_ component.Component = (*Passthrough)(nil)
52
_ component.DebugComponent = (*Passthrough)(nil)
53
)
54
55
// Run implements Component.
56
func (t *Passthrough) Run(ctx context.Context) error {
57
<-ctx.Done()
58
return nil
59
}
60
61
// Update implements Component.
62
func (t *Passthrough) Update(args component.Arguments) error {
63
c := args.(PassthroughConfig)
64
65
level.Info(t.log).Log("msg", "passing through value", "value", c.Input)
66
t.opts.OnStateChange(PassthroughExports{Output: c.Input})
67
return nil
68
}
69
70
// DebugInfo implements DebugComponent.
71
func (t *Passthrough) DebugInfo() interface{} {
72
// Useless, but for demonstration purposes shows how to export debug
73
// information. Real components would want to use something interesting here
74
// which allow the user to investigate issues of the internal state of a
75
// component.
76
return passthroughDebugInfo{
77
ComponentVersion: "v0.1-beta.0",
78
}
79
}
80
81
type passthroughDebugInfo struct {
82
ComponentVersion string `river:"component_version,attr"`
83
}
84
85