Merge pull request #711 from prymitive/test-metrics

chore(tests): add more asserts in metrics tests
This commit is contained in:
Łukasz Mierzwa
2019-05-12 12:48:58 +01:00
committed by GitHub
4 changed files with 28 additions and 13 deletions

View File

@@ -28,8 +28,8 @@ const (
)
type alertmanagerMetrics struct {
cycles float64
errors map[string]float64
Cycles float64
Errors map[string]float64
}
type alertmanagerStatus struct {
@@ -61,7 +61,7 @@ type Alertmanager struct {
lastError string
status alertmanagerStatus
// metrics tracked per alertmanager instance
metrics alertmanagerMetrics
Metrics alertmanagerMetrics
// headers to send with each AlertManager request
HTTPHeaders map[string]string
}
@@ -389,7 +389,7 @@ func (am *Alertmanager) pullAlerts(version string) error {
// Pull data from upstream Alertmanager instance
func (am *Alertmanager) Pull() error {
am.metrics.cycles++
am.Metrics.Cycles++
version := am.probeVersion()
@@ -399,7 +399,7 @@ func (am *Alertmanager) Pull() error {
if err != nil {
am.clearData()
am.setError(err.Error())
am.metrics.errors[labelValueErrorsSilences]++
am.Metrics.Errors[labelValueErrorsSilences]++
return err
}
@@ -407,7 +407,7 @@ func (am *Alertmanager) Pull() error {
if err != nil {
am.clearData()
am.setError(err.Error())
am.metrics.errors[labelValueErrorsAlerts]++
am.Metrics.Errors[labelValueErrorsAlerts]++
return err
}

View File

@@ -32,8 +32,8 @@ func NewAlertmanager(name, upstreamURI string, opts ...Option) (*Alertmanager, e
autocomplete: []models.Autocomplete{},
knownLabels: []string{},
HTTPHeaders: map[string]string{},
metrics: alertmanagerMetrics{
errors: map[string]float64{
Metrics: alertmanagerMetrics{
Errors: map[string]float64{
labelValueErrorsAlerts: 0,
labelValueErrorsSilences: 0,
},

View File

@@ -3,6 +3,7 @@ package main
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/prymitive/karma/internal/config"
@@ -39,4 +40,15 @@ func TestMetrics(t *testing.T) {
if resp.Code != http.StatusOK {
t.Errorf("GET /metrics returned status %d", resp.Code)
}
body := resp.Body.String()
for _, s := range []string{
"karma_collected_alerts_count",
"karma_collected_alerts_count",
"karma_collect_cycles_total",
"karma_alertmanager_errors_total",
} {
if !strings.Contains(body, s) {
t.Errorf("Metric '%s' missing from /metrics response", s)
}
}
}

View File

@@ -1,6 +1,9 @@
package alertmanager
package main
import "github.com/prometheus/client_golang/prometheus"
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prymitive/karma/internal/alertmanager"
)
type karmaCollector struct {
collectedAlerts *prometheus.Desc
@@ -46,17 +49,17 @@ func (c *karmaCollector) Describe(ch chan<- *prometheus.Desc) {
}
func (c *karmaCollector) Collect(ch chan<- prometheus.Metric) {
upstreams := GetAlertmanagers()
upstreams := alertmanager.GetAlertmanagers()
for _, am := range upstreams {
ch <- prometheus.MustNewConstMetric(
c.cyclesTotal,
prometheus.CounterValue,
am.metrics.cycles,
am.Metrics.Cycles,
am.Name,
)
for key, val := range am.metrics.errors {
for key, val := range am.Metrics.Errors {
ch <- prometheus.MustNewConstMetric(
c.errorsTotal,
prometheus.CounterValue,