-
Notifications
You must be signed in to change notification settings - Fork 0
/
publisher.go
50 lines (44 loc) · 1.1 KB
/
publisher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package beater
import (
"fmt"
"time"
"github.com/elastic/beats/libbeat/beat"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
)
type (
// Publisher is a interface responsible for publishing events to anywhere.
Publisher interface {
Publish(spec *commandSpec, v common.MapStr)
}
// elasticPublisher is a publisher publishes events to elasticsearch.
elasticPublisher struct {
client beat.Client
}
)
func newElasticPublisher(client beat.Client) *elasticPublisher {
return &elasticPublisher{client}
}
func (e *elasticPublisher) Publish(spec *commandSpec, v common.MapStr) {
var timestamp time.Time
if t, ok := v["@timestamp"]; ok {
timestamp = t.(time.Time)
delete(v, "@timestamp")
} else {
timestamp = time.Now()
}
v["type"] = spec.name
event := beat.Event{
Timestamp: timestamp,
Fields: v,
}
e.LogDebug(spec, "<event> %#v", event)
if !spec.debug {
e.client.Publish(event)
}
}
func (e *elasticPublisher) LogDebug(spec *commandSpec, msg string, args ...interface{}) {
if spec.debug {
logp.Info("[%s] %s", spec.name, fmt.Sprintf(msg, args...))
}
}