mirror of
https://github.com/prymitive/karma
synced 2026-05-07 03:26:52 +00:00
Merge pull request #711 from prymitive/test-metrics
chore(tests): add more asserts in metrics tests
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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,
|
||||
},
|
||||
|
||||
12
main_test.go
12
main_test.go
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
Reference in New Issue
Block a user