Skip to content

Commit

Permalink
Added cumulative counter test
Browse files Browse the repository at this point in the history
  • Loading branch information
Skeen committed May 1, 2020
1 parent 3302b13 commit 3722d12
Showing 1 changed file with 37 additions and 5 deletions.
42 changes: 37 additions & 5 deletions exporter/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,29 @@ func TestCounter(t *testing.T) {
}
}

func TestCounterValue(t *testing.T) {
regex := initCumulativeRegex(t)
counterCfg := newMetricConfig(t, &configuration.MetricConfig{
Name: "rainfall",
Value: "{{.rainfall}}",
})
counter := NewCounterMetric(counterCfg, regex, nil)

counter.ProcessMatch("Rainfall in Berlin: 32", nil)
counter.ProcessMatch("Rainfall in Berlin: 5", nil)

switch c := counter.Collector().(type) {
case prometheus.Counter:
m := io_prometheus_client.Metric{}
c.Write(&m)
if *m.Counter.Value != float64(37) {
t.Errorf("Expected 37 as counter value, but got %v.", *m.Counter.Value)
}
default:
t.Errorf("Unexpected type of metric: %v", reflect.TypeOf(c))
}
}

func TestLogfileLabel(t *testing.T) {
regex := initCounterRegex(t)
counterCfg := newMetricConfig(t, &configuration.MetricConfig{
Expand Down Expand Up @@ -163,16 +186,16 @@ func TestGauge(t *testing.T) {
}

func TestGaugeCumulative(t *testing.T) {
regex := initGaugeRegex(t)
regex := initCumulativeRegex(t)
gaugeCfg := newMetricConfig(t, &configuration.MetricConfig{
Name: "temperature",
Value: "{{.temperature}}",
Name: "rainfall",
Value: "{{.rainfall}}",
Cumulative: true,
})
gauge := NewGaugeMetric(gaugeCfg, regex, nil)

gauge.ProcessMatch("Temperature in Berlin: 32", nil)
gauge.ProcessMatch("Temperature in Moscow: -5", nil)
gauge.ProcessMatch("Rainfall in Berlin: 32", nil)
gauge.ProcessMatch("Rainfall in Moscow: -5", nil)

switch c := gauge.Collector().(type) {
case prometheus.Gauge:
Expand Down Expand Up @@ -226,6 +249,15 @@ func initGaugeRegex(t *testing.T) *oniguruma.Regex {
return regex
}

func initCumulativeRegex(t *testing.T) *oniguruma.Regex {
patterns := loadPatternDir(t)
regex, err := Compile("Rainfall in %{WORD:city}: %{INT:rainfall}", patterns)
if err != nil {
t.Error(err)
}
return regex
}

func newMetricConfig(t *testing.T, cfg *configuration.MetricConfig) *configuration.MetricConfig {
// Handle default for counter's value
if cfg.Type == "counter" && len(cfg.Value) == 0 {
Expand Down

0 comments on commit 3722d12

Please sign in to comment.