Path: blob/main/pkg/flow/internal/testcomponents/passthrough.go
4096 views
package testcomponents12import (3"context"45"github.com/go-kit/log"6"github.com/go-kit/log/level"7"github.com/grafana/agent/component"8)910func init() {11component.Register(component.Registration{12Name: "testcomponents.passthrough",13Args: PassthroughConfig{},14Exports: PassthroughExports{},1516Build: func(opts component.Options, args component.Arguments) (component.Component, error) {17return NewPassthrough(opts, args.(PassthroughConfig))18},19})20}2122// PassthroughConfig configures the testcomponents.passthrough component.23type PassthroughConfig struct {24Input string `river:"input,attr"`25}2627// PassthroughExports describes exported fields for the28// testcomponents.passthrough component.29type PassthroughExports struct {30Output string `river:"output,attr,optional"`31}3233// Passthrough implements the testcomponents.passthrough component, where it34// always emits its input as an output.35type Passthrough struct {36opts component.Options37log log.Logger38}3940// NewPassthrough creates a new passthrough component.41func NewPassthrough(o component.Options, cfg PassthroughConfig) (*Passthrough, error) {42t := &Passthrough{opts: o, log: o.Logger}43if err := t.Update(cfg); err != nil {44return nil, err45}46return t, nil47}4849var (50_ component.Component = (*Passthrough)(nil)51_ component.DebugComponent = (*Passthrough)(nil)52)5354// Run implements Component.55func (t *Passthrough) Run(ctx context.Context) error {56<-ctx.Done()57return nil58}5960// Update implements Component.61func (t *Passthrough) Update(args component.Arguments) error {62c := args.(PassthroughConfig)6364level.Info(t.log).Log("msg", "passing through value", "value", c.Input)65t.opts.OnStateChange(PassthroughExports{Output: c.Input})66return nil67}6869// DebugInfo implements DebugComponent.70func (t *Passthrough) DebugInfo() interface{} {71// Useless, but for demonstration purposes shows how to export debug72// information. Real components would want to use something interesting here73// which allow the user to investigate issues of the internal state of a74// component.75return passthroughDebugInfo{76ComponentVersion: "v0.1-beta.0",77}78}7980type passthroughDebugInfo struct {81ComponentVersion string `river:"component_version,attr"`82}838485