This repository has been archived by the owner on Feb 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
collector_test.go
136 lines (115 loc) · 3.04 KB
/
collector_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package main
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/prometheus/client_golang/prometheus"
)
func TestExporterError(t *testing.T) {
var delay time.Duration
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
<-time.After(delay)
fmt.Fprintf(w, "malformed JSON content")
}))
defer ts.Close()
delay = 10 * time.Millisecond
e := NewExporter(ts.URL, 5*time.Millisecond)
e.scraping = true
e.scrapeAll()
if e.scraping {
t.Fatalf("scraping status not reset on timeout")
}
delay = 0
e = NewExporter(ts.URL, 0)
e.scraping = true
e.scrapeAll()
if e.scraping {
t.Fatalf("scraping not reset on decoding error")
}
}
func TestExporterFastCollect(t *testing.T) {
var (
block = make(chan bool)
reqs = make(chan bool, 10)
)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
reqs <- true
<-block
}))
defer ts.Close()
// check if calling collect multiple before a scrape can finish accumulates scrapes
e := NewExporter(ts.URL, 50*time.Millisecond)
cch := make(chan prometheus.Metric, 100)
e.Collect(cch)
e.Collect(cch)
<-reqs
select {
case <-reqs:
t.Fatalf("unexpected scrape request came through")
case <-time.After(100 * time.Millisecond):
}
close(block)
}
func TestCollector(t *testing.T) {
c := newCollector("test")
c.gaugeVec("test_gauge", "test_help", nil)
if v, ok := c.gaugeVecs["test_gauge"]; !ok {
t.Fatalf("registered gauge vector missing")
} else {
v.With(nil).Set(1)
}
c.summaryVec("test_summary", "test_help", nil)
if v, ok := c.summaryVecs["test_summary"]; !ok {
t.Fatalf("registered summary vector missing")
} else {
v.With(nil).Observe(1)
}
c.counterVec("test_counter", "test_help", nil)
if v, ok := c.counterVecs["test_counter"]; !ok {
t.Fatalf("registered counter vector missing")
} else {
v.With(nil).Inc()
}
dch := make(chan *prometheus.Desc, 10)
c.Describe(dch)
if len(dch) != 3 {
t.Fatalf("inserted %d metrics but %d were described", 3, len(dch))
}
cch := make(chan prometheus.Metric, 10)
c.Collect(cch)
if len(cch) != 3 {
t.Fatalf("inserted %d metrics but %d were collected", 3, len(cch))
}
}
func TestTime3339(t *testing.T) {
ts := "2014-01-01T15:26:24.96569404Z"
var tmp Time3339
err := json.Unmarshal([]byte("\""+ts+"\""), &tmp)
if err != nil {
t.Fatalf("error decoding JSON timestamp: %s", err)
}
tmc, err := time.Parse(time.RFC3339Nano, ts)
if err != nil {
t.Fatalf("error parsing timestamp: %s", err)
}
if !tmc.Equal(time.Time(tmp)) {
t.Fatalf("decoded time mismatch: got %s, expected %s", tmp, tmc)
}
err = json.Unmarshal([]byte("null"), &tmp)
if err != nil {
t.Fatalf("error decoding JSON timestamp: %s", err)
}
if !time.Time(tmp).Equal(time.Time{}) {
t.Fatalf("null not parsed to zero time, got %s", tmp)
}
err = json.Unmarshal([]byte("\"\""), &tmp)
if err != nil {
t.Fatalf("error decoding JSON timestamp: %s", err)
}
if !time.Time(tmp).Equal(time.Time{}) {
t.Fatalf("empty string not parsed to zero time, got %s", tmp)
}
}