-
Notifications
You must be signed in to change notification settings - Fork 245
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
227 additions
and
190 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package testcomponents | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"sync" | ||
"time" | ||
|
||
"github.com/go-kit/log" | ||
"github.com/grafana/alloy/internal/component" | ||
"github.com/grafana/alloy/internal/featuregate" | ||
"github.com/grafana/alloy/internal/runtime/logging/level" | ||
) | ||
|
||
func init() { | ||
component.Register(component.Registration{ | ||
Name: "testcomponents.pulse", | ||
Stability: featuregate.StabilityPublicPreview, | ||
Args: PulseConfig{}, | ||
|
||
Build: func(opts component.Options, args component.Arguments) (component.Component, error) { | ||
return NewPulse(opts, args.(PulseConfig)) | ||
}, | ||
}) | ||
} | ||
|
||
type PulseConfig struct { | ||
Frequency time.Duration `alloy:"frequency,attr"` | ||
Max int `alloy:"max,attr"` | ||
ForwardTo []IntReceiver `alloy:"forward_to,attr,optional"` | ||
} | ||
|
||
type Pulse struct { | ||
opts component.Options | ||
log log.Logger | ||
|
||
cfgMut sync.Mutex | ||
cfg PulseConfig | ||
count int | ||
} | ||
|
||
func NewPulse(o component.Options, cfg PulseConfig) (*Pulse, error) { | ||
t := &Pulse{opts: o, log: o.Logger} | ||
if err := t.Update(cfg); err != nil { | ||
return nil, err | ||
} | ||
return t, nil | ||
} | ||
|
||
var ( | ||
_ component.Component = (*Pulse)(nil) | ||
) | ||
|
||
func (p *Pulse) Run(ctx context.Context) error { | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return nil | ||
case <-time.After(p.getNextPulse()): | ||
p.cfgMut.Lock() | ||
if p.cfg.Max == 0 || p.count < p.cfg.Max { | ||
for _, r := range p.cfg.ForwardTo { | ||
r.ReceiveInt(1) | ||
} | ||
p.count++ | ||
} | ||
p.cfgMut.Unlock() | ||
} | ||
} | ||
} | ||
|
||
func (t *Pulse) getNextPulse() time.Duration { | ||
t.cfgMut.Lock() | ||
defer t.cfgMut.Unlock() | ||
return t.cfg.Frequency | ||
} | ||
|
||
// Update implements Component. | ||
func (t *Pulse) Update(args component.Arguments) error { | ||
t.cfgMut.Lock() | ||
defer t.cfgMut.Unlock() | ||
|
||
cfg := args.(PulseConfig) | ||
if cfg.Frequency == 0 { | ||
return fmt.Errorf("frequency must not be 0") | ||
} | ||
|
||
level.Info(t.log).Log("msg", "setting count frequency", "freq", cfg.Frequency) | ||
t.cfg = cfg | ||
return nil | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.