-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding prometheus output with pushgateway option
- Loading branch information
Showing
12 changed files
with
429 additions
and
5 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
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
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,93 @@ | ||
/** | ||
* Copyright 2022 Cisco and its affiliates | ||
* All rights reserved. | ||
**/ | ||
|
||
package printer | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
|
||
config "github.com/get-woke/woke/pkg/config" | ||
"github.com/get-woke/woke/pkg/result" | ||
"github.com/prometheus/client_golang/prometheus" | ||
push "github.com/prometheus/client_golang/prometheus/push" | ||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
// Prometheus is a output format with prometheus metrics | ||
type Prometheus struct { | ||
writer io.Writer | ||
labels map[string]string | ||
pushgatewayURL string | ||
} | ||
|
||
// NewPrometheus returns a Prometheus Printer with color optionally disabled | ||
func NewPrometheus(w io.Writer, c *config.Prometheus) *Prometheus { | ||
return &Prometheus{ | ||
writer: w, | ||
labels: c.Labels, | ||
pushgatewayURL: c.PushgatewayURL, | ||
} | ||
} | ||
|
||
func (t *Prometheus) PrintSuccessExitMessage() bool { | ||
return true | ||
} | ||
|
||
// Print prints the file results or send it to pushgateway if URL provided | ||
func (t *Prometheus) Print(fs *result.FileResults) error { | ||
|
||
for _, r := range fs.Results { | ||
pos := fmt.Sprintf("%d:%d-%d", | ||
r.GetStartPosition().Line, | ||
r.GetStartPosition().Column, | ||
r.GetEndPosition().Column) | ||
|
||
labelString := "" | ||
|
||
if len(t.labels) > 0 { | ||
first := true | ||
for k, v := range t.labels { | ||
if first { | ||
labelString += ", " | ||
first = false | ||
} | ||
labelString += k + "=" + v + ", " | ||
} | ||
labelString = labelString[:len(labelString)-2] | ||
} | ||
fmt.Fprintf(t.writer, "woke_result{file=\"%s:%s\", term=\"%s\"%s} 1 \n", | ||
fs.Filename, pos, r.GetRuleName(), labelString) | ||
|
||
if t.pushgatewayURL != "" { | ||
pusher := push.New(t.pushgatewayURL, "woke") | ||
woke_result := prometheus.NewGauge(prometheus.GaugeOpts{ | ||
Name: "woke_result", | ||
Help: "Inclusive woke result", | ||
}) | ||
for k, v := range t.labels { | ||
pusher = pusher.Grouping(k, v) | ||
} | ||
pusher = pusher.Grouping("term", r.GetRuleName()). | ||
Grouping("instance", fs.Filename+":"+pos). | ||
Collector(woke_result) | ||
|
||
if err := pusher.Push(); err != nil { | ||
log.Error().Err(err).Msg("Could not push woke_result to Pushgateway") | ||
} else { | ||
log.Debug().Msg("push woke_result to Pushgateway done") | ||
} | ||
} | ||
|
||
} | ||
|
||
return nil | ||
} | ||
|
||
func (t *Prometheus) Start() { | ||
} | ||
|
||
func (t *Prometheus) End() { | ||
} |
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,48 @@ | ||
/** | ||
* Copyright 2022 Cisco and its affiliates | ||
* All rights reserved. | ||
**/ | ||
|
||
package printer | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
config "github.com/get-woke/woke/pkg/config" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestPrometheus_Print(t *testing.T) { | ||
buf := new(bytes.Buffer) | ||
p := NewPrometheus(buf, new(config.Config)) | ||
res := generateFileResult() | ||
assert.NoError(t, p.Print(res)) | ||
got := buf.String() | ||
fmt.Printf("buf: %s", buf) | ||
fmt.Printf("res: %s", res.Results[0].String()) | ||
expected := fmt.Sprintf("woke_result{file=\"foo.txt:1:6-15\", term=\"%s\"} 1 \n", res.Results[0].GetRuleName()) | ||
assert.Equal(t, expected, got) | ||
} | ||
|
||
func TestPrometheus_Start(t *testing.T) { | ||
buf := new(bytes.Buffer) | ||
p := NewPrometheus(buf, new(config.Config)) | ||
p.Start() | ||
got := buf.String() | ||
assert.Equal(t, ``, got) | ||
} | ||
|
||
func TestPrometheus_End(t *testing.T) { | ||
buf := new(bytes.Buffer) | ||
p := NewPrometheus(buf, new(config.Config)) | ||
p.End() | ||
got := buf.String() | ||
assert.Equal(t, ``, got) | ||
} | ||
|
||
func TestPrometheus_PrintSuccessExitMessage(t *testing.T) { | ||
buf := new(bytes.Buffer) | ||
p := NewPrometheus(buf, new(config.Config)) | ||
assert.Equal(t, true, p.PrintSuccessExitMessage()) | ||
} |
Oops, something went wrong.