-
Notifications
You must be signed in to change notification settings - Fork 61
Adding prometheus and pushgateway outputs #230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jgarnier
wants to merge
1
commit into
get-woke:main
Choose a base branch
from
jgarnier:prometheus
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic is duplicated across prometheus.go and pushgateway.go. Can they be consolidated? I would also recommend creating a test for this functionality specifically