forked from goss-org/goss
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'improbable-eng-prometheus_exporter'
- Loading branch information
Showing
11 changed files
with
658 additions
and
8 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,106 @@ | ||
package outputs | ||
|
||
import ( | ||
"io" | ||
"strings" | ||
"time" | ||
|
||
"github.com/aelsabbahy/goss/resource" | ||
"github.com/aelsabbahy/goss/util" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promauto" | ||
"github.com/prometheus/common/expfmt" | ||
) | ||
|
||
const ( | ||
labelType = "type" | ||
labelOutcome = "outcome" | ||
) | ||
|
||
var ( | ||
testOutcomes = promauto.NewCounterVec(prometheus.CounterOpts{ | ||
Namespace: "goss", | ||
Subsystem: "tests", | ||
Name: "outcomes_total", | ||
Help: "The number of test-outcomes from this run.", | ||
}, []string{labelType, labelOutcome}) | ||
testDurations = promauto.NewCounterVec(prometheus.CounterOpts{ | ||
Namespace: "goss", | ||
Subsystem: "tests", | ||
Name: "outcomes_duration_milliseconds", | ||
Help: "The duration of tests from this run. Note; tests run concurrently.", | ||
}, []string{labelType, labelOutcome}) | ||
runOutcomes = promauto.NewCounterVec(prometheus.CounterOpts{ | ||
Namespace: "goss", | ||
Subsystem: "tests", | ||
Name: "run_outcomes_total", | ||
Help: "The outcomes of this run as a whole.", | ||
}, []string{labelOutcome}) | ||
runDuration = promauto.NewCounterVec(prometheus.CounterOpts{ | ||
Namespace: "goss", | ||
Subsystem: "tests", | ||
Name: "run_duration_milliseconds", | ||
Help: "The end-to-end duration of this run.", | ||
}, []string{labelOutcome}) | ||
) | ||
|
||
// Prometheus renders metrics in prometheus.io text-format https://prometheus.io/docs/instrumenting/exposition_formats/#text-based-format | ||
type Prometheus struct{} | ||
|
||
// NewPrometheus creates and initialises a new Prometheus Outputer (to avoid missing metrics) | ||
func NewPrometheus() *Prometheus { | ||
outputer := &Prometheus{} | ||
outputer.init() | ||
return outputer | ||
} | ||
|
||
func (r *Prometheus) init() { | ||
// Avoid missing metrics: https://prometheus.io/docs/practices/instrumentation/#avoid-missing-metrics | ||
for resourceType := range resource.Resources() { | ||
for _, outcome := range resource.HumanOutcomes() { | ||
testOutcomes.WithLabelValues(resourceType, outcome).Add(0) | ||
testDurations.WithLabelValues(resourceType, outcome).Add(0) | ||
} | ||
} | ||
runOutcomes.WithLabelValues(labelOutcome).Add(0) | ||
runDuration.WithLabelValues(labelOutcome).Add(0) | ||
} | ||
|
||
// ValidOptions is a list of valid format options for prometheus | ||
func (r Prometheus) ValidOptions() []*formatOption { | ||
return []*formatOption{} | ||
} | ||
|
||
// Output converts the results into the prometheus text-format. | ||
func (r Prometheus) Output(w io.Writer, results <-chan []resource.TestResult, | ||
startTime time.Time, outConfig util.OutputConfig) (exitCode int) { | ||
overallOutcome := resource.OutcomeUnknown | ||
for resultGroup := range results { | ||
for _, tr := range resultGroup { | ||
resType := strings.ToLower(tr.ResourceType) | ||
outcome := tr.ToOutcome() | ||
testOutcomes.WithLabelValues(resType, outcome).Inc() | ||
testDurations.WithLabelValues(resType, outcome).Add(float64(tr.Duration.Milliseconds())) | ||
if tr.Result != resource.SUCCESS { | ||
overallOutcome = tr.ToOutcome() | ||
} | ||
} | ||
} | ||
|
||
runOutcomes.WithLabelValues(overallOutcome).Inc() | ||
runDuration.WithLabelValues(overallOutcome).Add(float64(time.Since(startTime).Milliseconds())) | ||
|
||
metricsFamilies, err := prometheus.DefaultGatherer.Gather() | ||
if err != nil { | ||
return -1 | ||
} | ||
encoder := expfmt.NewEncoder(w, expfmt.FmtText) | ||
for _, mf := range metricsFamilies { | ||
err := encoder.Encode(mf) | ||
if err != nil { | ||
return -1 | ||
} | ||
} | ||
|
||
return 0 | ||
} |
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,76 @@ | ||
package outputs | ||
|
||
import ( | ||
"bytes" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/aelsabbahy/goss/resource" | ||
|
||
"github.com/aelsabbahy/goss/util" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestPrometheusOutput(t *testing.T) { | ||
buf := &bytes.Buffer{} | ||
outputer := &Prometheus{} | ||
injectedResults := []resource.TestResult{ | ||
{ | ||
ResourceType: "Command", | ||
Duration: 10 * time.Millisecond, | ||
Result: resource.SUCCESS, | ||
}, | ||
{ | ||
ResourceType: "Command", | ||
Duration: 10 * time.Millisecond, | ||
Result: resource.SUCCESS, | ||
}, | ||
{ | ||
ResourceType: "Command", | ||
Duration: 10 * time.Millisecond, | ||
Result: resource.FAIL, | ||
}, | ||
{ | ||
ResourceType: "File", | ||
Duration: 10 * time.Millisecond, | ||
Result: resource.SKIP, | ||
}, | ||
} | ||
|
||
exitCode := outputer.Output(buf, makeResults(injectedResults...), time.Now().Add(-1*time.Minute), util.OutputConfig{}) | ||
|
||
assert.Equal(t, 0, exitCode) | ||
output := buf.String() | ||
t.Logf(output) | ||
assert.Contains(t, output, `goss_tests_outcomes_duration_milliseconds{outcome="pass",type="command"} 20`) | ||
assert.Contains(t, output, `goss_tests_outcomes_duration_milliseconds{outcome="fail",type="command"} 10`) | ||
assert.Contains(t, output, `goss_tests_outcomes_duration_milliseconds{outcome="skip",type="command"} 0`) | ||
assert.Contains(t, output, `goss_tests_outcomes_duration_milliseconds{outcome="pass",type="file"} 0`) | ||
assert.Contains(t, output, `goss_tests_outcomes_duration_milliseconds{outcome="fail",type="file"} 0`) | ||
assert.Contains(t, output, `goss_tests_outcomes_duration_milliseconds{outcome="skip",type="file"} 10`) | ||
assert.Contains(t, output, `goss_tests_outcomes_total{outcome="pass",type="command"} 2`) | ||
assert.Contains(t, output, `goss_tests_outcomes_total{outcome="fail",type="command"} 1`) | ||
assert.Contains(t, output, `goss_tests_outcomes_total{outcome="skip",type="command"} 0`) | ||
assert.Contains(t, output, `goss_tests_outcomes_total{outcome="pass",type="file"} 0`) | ||
assert.Contains(t, output, `goss_tests_outcomes_total{outcome="fail",type="file"} 0`) | ||
assert.Contains(t, output, `goss_tests_outcomes_total{outcome="skip",type="file"} 1`) | ||
assert.Contains(t, output, `goss_tests_run_duration_milliseconds{outcome="skip"} 60000`) | ||
assert.Contains(t, output, `goss_tests_run_outcomes_total{outcome="skip"} 1`) | ||
} | ||
|
||
func makeResults(results ...resource.TestResult) <-chan []resource.TestResult { | ||
out := make(chan []resource.TestResult) | ||
var wg sync.WaitGroup | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
out <- append([]resource.TestResult{}, results...) | ||
}() | ||
|
||
go func() { | ||
wg.Wait() | ||
close(out) | ||
}() | ||
return out | ||
} |
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