forked from falcosecurity/falcosidekick
/
stan.go
52 lines (43 loc) · 1.46 KB
/
stan.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
51
52
package outputs
import (
"encoding/json"
"log"
"strings"
stan "github.com/nats-io/stan.go"
"github.com/ir0njaw/falcosidekick/types"
)
// StanPublish publishes event to NATS Streaming
func (c *Client) StanPublish(falcopayload types.FalcoPayload) {
c.Stats.Stan.Add(Total, 1)
nc, err := stan.Connect(c.Config.Stan.ClusterID, c.Config.Stan.ClientID, stan.NatsURL(c.EndpointURL.String()))
if err != nil {
c.setStanErrorMetrics()
log.Printf("[ERROR] : STAN - %v\n", err.Error())
return
}
defer nc.Close()
r := strings.Trim(slugRegularExpression.ReplaceAllString(strings.ToLower(falcopayload.Rule), "_"), "_")
j, err := json.Marshal(falcopayload)
if err != nil {
c.setStanErrorMetrics()
log.Printf("[ERROR] : STAN - %v\n", err.Error())
return
}
err = nc.Publish("falco."+strings.ToLower(falcopayload.Priority.String())+"."+r, j)
if err != nil {
c.setStanErrorMetrics()
log.Printf("[ERROR] : STAN - %v\n", err)
return
}
// Setting the success status
go c.CountMetric(Outputs, 1, []string{"output:stan", "status:ok"})
c.Stats.Stan.Add(OK, 1)
c.PromStats.Outputs.With(map[string]string{"destination": "stan", "status": OK}).Inc()
log.Printf("[INFO] : STAN - Publish OK\n")
}
// setStanErrorMetrics set the error stats
func (c *Client) setStanErrorMetrics() {
go c.CountMetric(Outputs, 1, []string{"output:stan", "status:error"})
c.Stats.Stan.Add(Error, 1)
c.PromStats.Outputs.With(map[string]string{"destination": "stan", "status": Error}).Inc()
}