Skip to content

Commit

Permalink
Updates for PromStats
Browse files Browse the repository at this point in the history
Signed-off-by: Ian Robertson <[email protected]>
  • Loading branch information
IanRobertson-wpe committed Nov 13, 2020
1 parent 572c36e commit 955bf64
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 103 deletions.
2 changes: 1 addition & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func getConfig() *types.Configuration {
c.Opsgenie.MinimumPriority = checkPriority(c.Opsgenie.MinimumPriority)
c.Webhook.MinimumPriority = checkPriority(c.Webhook.MinimumPriority)
c.Azure.EventHub.MinimumPriority = checkPriority(c.Azure.EventHub.MinimumPriority)
c.GCP.PubSub.MinimumPriority = checkPriority(c.PubSub.EventHub.MinimumPriority)
c.GCP.PubSub.MinimumPriority = checkPriority(c.GCP.PubSub.MinimumPriority)

c.Slack.MessageFormatTemplate = getMessageFormatTemplate("Slack", c.Slack.MessageFormat)
c.Rocketchat.MessageFormatTemplate = getMessageFormatTemplate("Rocketchat", c.Rocketchat.MessageFormat)
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ github.com/prometheus/client_golang v1.7.1 h1:NTGy1Ja9pByO+xAeH/qiWnLrKtr3hJPNja
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90 h1:S/YWwWx/RA8rT8tKFRuGUZhuA90OyIBpPCXkcbwU8DE=
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4 h1:gQz4mCbXsO+nc9n1hCxHcGA3Zx3Eo+UHZoInFGUIXNM=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/common v0.0.0-20181113130724-41aa239b4cce/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro=
github.com/prometheus/common v0.4.0 h1:7etb9YClo3a6HjLzfl6rIQaU+FDfi0VSX39io3aQ+DM=
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ func init() {
}
if config.GCP.PubSub.ProjectID != "" && config.GCP.PubSub.Topic != "" && config.GCP.Credentials != "" {
var err error
gcpClient, err = outputs.NewGCPClient(config, stats, statsdClient, dogstatsdClient)
gcpClient, err = outputs.NewGCPClient(config, stats, promStats, statsdClient, dogstatsdClient)
if err != nil {
config.GCP.Credentials = ""
} else {
Expand Down
28 changes: 11 additions & 17 deletions outputs/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,17 @@ import (
"cloud.google.com/go/pubsub"
"context"
"encoding/base64"
"golang.org/x/oauth2/google"
"google.golang.org/api/option"
"log"
"os"
"errors"
"encoding/json"
"errors"
"github.com/DataDog/datadog-go/statsd"
"github.com/falcosecurity/falcosidekick/types"
"golang.org/x/oauth2/google"
"google.golang.org/api/option"
"log"
)

// NewGCPClient returns a new output.Client for accessing the GCP API.
func NewGCPClient(config *types.Configuration, stats *types.Statistics, statsdClient, dogstatsdClient *statsd.Client) (*Client, error) {

if config.GCP.Credentials != "" {
os.Setenv("GCP_CREDENTIALS", config.GCP.Credentials)
os.Setenv("GCP_PUBSUB_PROJECTID", config.GCP.PubSub.ProjectID)
os.Setenv("GCP_PUBSUB_TOPIC", config.GCP.PubSub.Topic)
}

func NewGCPClient(config *types.Configuration, stats *types.Statistics, promStats *types.PromStatistics, statsdClient, dogstatsdClient *statsd.Client) (*Client, error) {
base64decodedCredentialsData, err := base64.StdEncoding.DecodeString(config.GCP.Credentials)
if err != nil {
log.Printf("[ERROR] : GCP - %v\n", "Error while base64-decoding GCP Credentials")
Expand All @@ -49,6 +41,7 @@ func NewGCPClient(config *types.Configuration, stats *types.Statistics, statsdCl
Config: config,
GCPTopicClient: topicClient,
Stats: stats,
PromStats: promStats,
StatsdClient: statsdClient,
DogstatsdClient: dogstatsdClient,
}, nil
Expand All @@ -57,7 +50,6 @@ func NewGCPClient(config *types.Configuration, stats *types.Statistics, statsdCl
// GCPPublishTopic sends a message to a GCP PubSub Topic
func (c *Client) GCPPublishTopic(falcopayload types.FalcoPayload) {
c.Stats.GCPPubSub.Add("total", 1)
go c.CountMetric("outputs", 1, []string{"output:gcppubsub", "status:ok"})

payload, _ := json.Marshal(falcopayload)
message := &pubsub.Message{
Expand All @@ -66,12 +58,14 @@ func (c *Client) GCPPublishTopic(falcopayload types.FalcoPayload) {
result := c.GCPTopicClient.Publish(context.Background(), message)
id, err := result.Get(context.Background())
if err != nil {
log.Printf("[ERROR] : GCPPubSub - %v - %v\n", "Error while publishing message", err.Error())
log.Printf("[ERROR] : GCPPubSub - %v - %v\n", "Error while publishing message", err.Error())
c.Stats.GCPPubSub.Add("error", 1)
go c.CountMetric("outputs", 1, []string{"output:gcppubsub", "status:error"})
c.PromStats.Outputs.With(map[string]string{"destination": "gcppubsub", "status": "error"}).Inc()
return
}
log.Printf("[INFO] : GCPPubSub - Send to topic OK (%v)\n", id)
c.Stats.GCPPubSub.Add("ok", 1)
go c.CountMetric("outputs", 1, []string{"output:gcppubsub", "status:ok"})
c.Stats.GCPPubSub.Add("ok", 1)
go c.CountMetric("outputs", 1, []string{"output:gcppubsub", "status:ok"})
c.PromStats.Outputs.With(map[string]string{"destination": "gcppubsub", "status": "ok"}).Inc()
}
106 changes: 22 additions & 84 deletions stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,27 @@ func getInitStats() *types.Statistics {
FIFO: getInputNewMap("fifo"),
GRPC: getInputNewMap("grpc"),
Falco: expvar.NewMap("falco.priority"),
Slack: expvar.NewMap("outputs.slack"),
Rocketchat: expvar.NewMap("outputs.rocketchat"),
Mattermost: expvar.NewMap("outputs.mattermost"),
Teams: expvar.NewMap("outputs.teams"),
Datadog: expvar.NewMap("outputs.datadog"),
Discord: expvar.NewMap("outputs.discord"),
Alertmanager: expvar.NewMap("outputs.alertmanager"),
Elasticsearch: expvar.NewMap("outputs.elasticsearch"),
Loki: expvar.NewMap("outputs.loki"),
Nats: expvar.NewMap("outputs.nats"),
Influxdb: expvar.NewMap("outputs.influxdb"),
AWSLambda: expvar.NewMap("outputs.awslambda"),
AWSSQS: expvar.NewMap("outputs.awssqs"),
AWSSNS: expvar.NewMap("outputs.awssns"),
SMTP: expvar.NewMap("outputs.smtp"),
Opsgenie: expvar.NewMap("outputs.opsgenie"),
Statsd: expvar.NewMap("outputs.statsd"),
Dogstatsd: expvar.NewMap("outputs.dogstatsd"),
Webhook: expvar.NewMap("outputs.webhook"),
AzureEventHub: expvar.NewMap("outputs.azureeventhub"),
GCPPubSub: expvar.NewMap("outputs.gcppubsub"),
Slack: getOutputNewMap("slack"),
Rocketchat: getOutputNewMap("rocketchat"),
Mattermost: getOutputNewMap("mattermost"),
Teams: getOutputNewMap("teams"),
Datadog: getOutputNewMap("datadog"),
Discord: getOutputNewMap("discord"),
Alertmanager: getOutputNewMap("alertmanager"),
Elasticsearch: getOutputNewMap("elasticsearch"),
Loki: getOutputNewMap("loki"),
Nats: getOutputNewMap("nats"),
Influxdb: getOutputNewMap("influxdb"),
AWSLambda: getOutputNewMap("awslambda"),
AWSSQS: getOutputNewMap("awssqs"),
AWSSNS: getOutputNewMap("awssns"),
SMTP: getOutputNewMap("smtp"),
Opsgenie: getOutputNewMap("opsgenie"),
Statsd: getOutputNewMap("statsd"),
Dogstatsd: getOutputNewMap("dogstatsd"),
Webhook: getOutputNewMap("webhook"),
AzureEventHub: getOutputNewMap("azureeventhub"),
GCPPubSub: getOutputNewMap("gcppubsub"),
}
stats.Falco.Add("emergency", 0)
stats.Falco.Add("alert", 0)
Expand All @@ -52,69 +52,7 @@ func getInitStats() *types.Statistics {
stats.Falco.Add("informational", 0)
stats.Falco.Add("debug", 0)
stats.Falco.Add("unknown", 0)
stats.Slack.Add("total", 0)
stats.Slack.Add("error", 0)
stats.Slack.Add("ok", 0)
stats.Rocketchat.Add("total", 0)
stats.Rocketchat.Add("error", 0)
stats.Rocketchat.Add("ok", 0)
stats.Mattermost.Add("total", 0)
stats.Mattermost.Add("error", 0)
stats.Mattermost.Add("ok", 0)
stats.Teams.Add("total", 0)
stats.Teams.Add("error", 0)
stats.Teams.Add("ok", 0)
stats.Datadog.Add("total", 0)
stats.Datadog.Add("error", 0)
stats.Datadog.Add("ok", 0)
stats.Discord.Add("total", 0)
stats.Discord.Add("error", 0)
stats.Discord.Add("ok", 0)
stats.Alertmanager.Add("total", 0)
stats.Alertmanager.Add("error", 0)
stats.Alertmanager.Add("ok", 0)
stats.Elasticsearch.Add("total", 0)
stats.Elasticsearch.Add("error", 0)
stats.Elasticsearch.Add("ok", 0)
stats.Influxdb.Add("total", 0)
stats.Influxdb.Add("error", 0)
stats.Influxdb.Add("ok", 0)
stats.Loki.Add("total", 0)
stats.Loki.Add("error", 0)
stats.Loki.Add("ok", 0)
stats.Nats.Add("total", 0)
stats.Nats.Add("error", 0)
stats.Nats.Add("ok", 0)
stats.AWSLambda.Add("total", 0)
stats.AWSLambda.Add("error", 0)
stats.AWSLambda.Add("ok", 0)
stats.AWSSQS.Add("total", 0)
stats.AWSSQS.Add("error", 0)
stats.AWSSQS.Add("ok", 0)
stats.AWSSNS.Add("total", 0)
stats.AWSSNS.Add("error", 0)
stats.AWSSNS.Add("ok", 0)
stats.SMTP.Add("total", 0)
stats.SMTP.Add("error", 0)
stats.SMTP.Add("ok", 0)
stats.Opsgenie.Add("total", 0)
stats.Opsgenie.Add("error", 0)
stats.Opsgenie.Add("ok", 0)
stats.Statsd.Add("total", 0)
stats.Statsd.Add("error", 0)
stats.Statsd.Add("ok", 0)
stats.Dogstatsd.Add("total", 0)
stats.Dogstatsd.Add("error", 0)
stats.Dogstatsd.Add("ok", 0)
stats.Webhook.Add("total", 0)
stats.Webhook.Add("error", 0)
stats.Webhook.Add("ok", 0)
stats.AzureEventHub.Add("total", 0)
stats.AzureEventHub.Add("error", 0)
stats.AzureEventHub.Add("ok", 0)
stats.GCPPubSub.Add("total", 0)
stats.GCPPubSub.Add("error", 0)
stats.GCPPubSub.Add("ok", 0)

return stats
}

Expand Down

0 comments on commit 955bf64

Please sign in to comment.