Skip to content

Commit

Permalink
Merge pull request elastic#7 from ruflin/ticker-improvements
Browse files Browse the repository at this point in the history
Introduce ticker and period config
  • Loading branch information
Steffen Siering committed Jan 29, 2016
2 parents b7f294c + 8abd4e8 commit 5299182
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 9 deletions.
4 changes: 2 additions & 2 deletions {{cookiecutter.beat}}/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ make
git init
git add .
git commit
git remote set-url origin https://{{cookiecutter.beat_path}}/{{cookiecutter.beat}}
git remote set-url origin https://{{cookiecutter.beat_path}}{{cookiecutter.beat}}
git push origin master
```

Expand All @@ -74,7 +74,7 @@ git push origin master
```
mkdir -p ${GOPATH}/{{cookiecutter.beat_path}}
cd ${GOPATH}/{{cookiecutter.beat_path}}
git clone https://{{cookiecutter.beat_path}}/{{cookiecutter.beat}}
git clone https://{{cookiecutter.beat_path}}{{cookiecutter.beat}}
```


Expand Down
44 changes: 39 additions & 5 deletions {{cookiecutter.beat}}/beater/{{cookiecutter.beat}}.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@ package beater

import (
"fmt"
"time"

"github.com/elastic/beats/libbeat/beat"
"github.com/elastic/beats/libbeat/cfgfile"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"

"{{cookiecutter.beat_path}}{{cookiecutter.beat}}/config"
)

type {{cookiecutter.beat|capitalize}} struct {
done chan struct{}
BeaterConfig *config.{{cookiecutter.beat|capitalize}}Config
done chan struct{}
period time.Duration
}

// Creates beater
Expand All @@ -24,8 +29,8 @@ func New() *{{cookiecutter.beat|capitalize}} {

func (bt *{{cookiecutter.beat|capitalize}}) Config(b *beat.Beat) error {

cfg := &config.{{cookiecutter.beat|capitalize}}Config{}
err := cfgfile.Read(&cfg, "")
// Load beater configuration
err := cfgfile.Read(&bt.BeaterConfig, "")
if err != nil {
return fmt.Errorf("Error reading config file: %v", err)
}
Expand All @@ -34,13 +39,42 @@ func (bt *{{cookiecutter.beat|capitalize}}) Config(b *beat.Beat) error {
}

func (bt *{{cookiecutter.beat|capitalize}}) Setup(b *beat.Beat) error {

// Setting default period if not set
if bt.BeaterConfig.Period == "" {
bt.BeaterConfig.Period = "1s"
}

var err error
bt.period, err = time.ParseDuration(bt.BeaterConfig.Period)
if err != nil {
return err
}

return nil
}

func (bt *{{cookiecutter.beat|capitalize}}) Run(b *beat.Beat) error {
fmt.Println("{{cookiecutter.beat}} is running! Hit CTRL-C to stop it.")
logp.Info("demobeat is running! Hit CTRL-C to stop it.")

<-bt.done
ticker := time.NewTicker(bt.period)
counter := 1

for {
select {
case <-bt.done:
return nil
case <-ticker.C:
event := common.MapStr{
"@timestamp": common.Time(time.Now()),
"type": b.Name,
"counter": counter,
}
b.Events.PublishEvent(event)
logp.Info("Event sent")
counter++
}
}

return nil
}
Expand Down
2 changes: 1 addition & 1 deletion {{cookiecutter.beat}}/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
package config

type {{cookiecutter.beat|capitalize}}Config struct {

Period string `yaml:"period"`
}
2 changes: 2 additions & 0 deletions {{cookiecutter.beat}}/etc/beat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
############################# {{cookiecutter.beat|capitalize}} ######################################

{{cookiecutter.beat}}:
# Defines how often an event is sent to the output
period: 1s
7 changes: 6 additions & 1 deletion {{cookiecutter.beat}}/main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package main

import (
"os"

"github.com/elastic/beats/libbeat/beat"
"{{cookiecutter.beat_path}}{{cookiecutter.beat}}/beater"
)

func main() {
beat.Run("{{cookiecutter.beat}}", "", beater.New())
err := beat.Run("{{cookiecutter.beat}}", "", beater.New())
if err != nil {
os.Exit(1)
}
}

0 comments on commit 5299182

Please sign in to comment.