Skip to content

Commit 5d14ad7

Browse files
guseggertGus Eggert
authored and
Gus Eggert
committed
test: port prometheus test to Go
1 parent 46f9a15 commit 5d14ad7

File tree

4 files changed

+294
-2
lines changed

4 files changed

+294
-2
lines changed

cmd/ipfs/daemon.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,11 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment
437437
default:
438438
return fmt.Errorf("unrecognized routing option: %s", routingOption)
439439
}
440-
view.Register(dhtmetrics.DefaultViews...)
440+
441+
err = view.Register(dhtmetrics.DefaultViews...)
442+
if err != nil {
443+
return fmt.Errorf("registering Prometheus view for DHT: %w", err)
444+
}
441445

442446
agentVersionSuffixString, _ := req.Options[agentVersionSuffix].(string)
443447
if agentVersionSuffixString != "" {

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ require (
6565
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58
6666
github.com/pkg/errors v0.9.1
6767
github.com/prometheus/client_golang v1.14.0
68+
github.com/prometheus/common v0.42.0
6869
github.com/stretchr/testify v1.8.2
6970
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7
7071
github.com/tidwall/gjson v1.14.4
@@ -179,7 +180,6 @@ require (
179180
github.com/pmezard/go-difflib v1.0.0 // indirect
180181
github.com/polydawn/refmt v0.89.0 // indirect
181182
github.com/prometheus/client_model v0.3.0 // indirect
182-
github.com/prometheus/common v0.42.0 // indirect
183183
github.com/prometheus/procfs v0.9.0 // indirect
184184
github.com/prometheus/statsd_exporter v0.22.7 // indirect
185185
github.com/quic-go/qpack v0.4.0 // indirect

test/cli/prometheus_test.go

Lines changed: 277 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,277 @@
1+
package cli
2+
3+
import (
4+
"strings"
5+
"testing"
6+
"time"
7+
8+
"github.com/ipfs/kubo/config"
9+
"github.com/ipfs/kubo/test/cli/harness"
10+
"github.com/ipfs/kubo/test/cli/testutils"
11+
"github.com/prometheus/common/expfmt"
12+
"github.com/stretchr/testify/assert"
13+
"github.com/stretchr/testify/require"
14+
)
15+
16+
var metricsRcmgrFamilies = map[string]bool{
17+
"libp2p_rcmgr_memory_allocations_allowed_total": true,
18+
"libp2p_rcmgr_memory_allocations_blocked_total": true,
19+
"libp2p_rcmgr_peer_blocked_total": true,
20+
"libp2p_rcmgr_peers_allowed_total": true,
21+
}
22+
23+
var metricsDefaultFamilies = map[string]bool{
24+
"flatfs_datastore_batchcommit_errors_total": true,
25+
"flatfs_datastore_batchcommit_latency_seconds": true,
26+
"flatfs_datastore_batchcommit_total": true,
27+
"flatfs_datastore_batchdelete_errors_total": true,
28+
"flatfs_datastore_batchdelete_latency_seconds": true,
29+
"flatfs_datastore_batchdelete_total": true,
30+
"flatfs_datastore_batchput_errors_total": true,
31+
"flatfs_datastore_batchput_latency_seconds": true,
32+
"flatfs_datastore_batchput_size_bytes": true,
33+
"flatfs_datastore_batchput_total": true,
34+
"flatfs_datastore_check_errors_total": true,
35+
"flatfs_datastore_check_latency_seconds": true,
36+
"flatfs_datastore_check_total": true,
37+
"flatfs_datastore_delete_errors_total": true,
38+
"flatfs_datastore_delete_latency_seconds": true,
39+
"flatfs_datastore_delete_total": true,
40+
"flatfs_datastore_du_errors_total": true,
41+
"flatfs_datastore_du_latency_seconds": true,
42+
"flatfs_datastore_du_total": true,
43+
"flatfs_datastore_gc_errors_total": true,
44+
"flatfs_datastore_gc_latency_seconds": true,
45+
"flatfs_datastore_gc_total": true,
46+
"flatfs_datastore_get_errors_total": true,
47+
"flatfs_datastore_get_latency_seconds": true,
48+
"flatfs_datastore_get_size_bytes": true,
49+
"flatfs_datastore_get_total": true,
50+
"flatfs_datastore_getsize_errors_total": true,
51+
"flatfs_datastore_getsize_latency_seconds": true,
52+
"flatfs_datastore_getsize_total": true,
53+
"flatfs_datastore_has_errors_total": true,
54+
"flatfs_datastore_has_latency_seconds": true,
55+
"flatfs_datastore_has_total": true,
56+
"flatfs_datastore_put_errors_total": true,
57+
"flatfs_datastore_put_latency_seconds": true,
58+
"flatfs_datastore_put_size_bytes": true,
59+
"flatfs_datastore_put_total": true,
60+
"flatfs_datastore_query_errors_total": true,
61+
"flatfs_datastore_query_latency_seconds": true,
62+
"flatfs_datastore_query_total": true,
63+
"flatfs_datastore_scrub_errors_total": true,
64+
"flatfs_datastore_scrub_latency_seconds": true,
65+
"flatfs_datastore_scrub_total": true,
66+
"flatfs_datastore_sync_errors_total": true,
67+
"flatfs_datastore_sync_latency_seconds": true,
68+
"flatfs_datastore_sync_total": true,
69+
"go_gc_duration_seconds": true,
70+
"go_goroutines": true,
71+
"go_info": true,
72+
"go_memstats_alloc_bytes": true,
73+
"go_memstats_alloc_bytes_total": true,
74+
"go_memstats_buck_hash_sys_bytes": true,
75+
"go_memstats_frees_total": true,
76+
"go_memstats_gc_sys_bytes": true,
77+
"go_memstats_heap_alloc_bytes": true,
78+
"go_memstats_heap_idle_bytes": true,
79+
"go_memstats_heap_inuse_bytes": true,
80+
"go_memstats_heap_objects": true,
81+
"go_memstats_heap_released_bytes": true,
82+
"go_memstats_heap_sys_bytes": true,
83+
"go_memstats_last_gc_time_seconds": true,
84+
"go_memstats_lookups_total": true,
85+
"go_memstats_mallocs_total": true,
86+
"go_memstats_mcache_inuse_bytes": true,
87+
"go_memstats_mcache_sys_bytes": true,
88+
"go_memstats_mspan_inuse_bytes": true,
89+
"go_memstats_mspan_sys_bytes": true,
90+
"go_memstats_next_gc_bytes": true,
91+
"go_memstats_other_sys_bytes": true,
92+
"go_memstats_stack_inuse_bytes": true,
93+
"go_memstats_stack_sys_bytes": true,
94+
"go_memstats_sys_bytes": true,
95+
"go_threads": true,
96+
"ipfs_bitswap_active_block_tasks": true,
97+
"ipfs_bitswap_active_tasks": true,
98+
"ipfs_bitswap_pending_block_tasks": true,
99+
"ipfs_bitswap_pending_tasks": true,
100+
"ipfs_bitswap_recv_all_blocks_bytes": true,
101+
"ipfs_bitswap_recv_dup_blocks_bytes": true,
102+
"ipfs_bitswap_send_times": true,
103+
"ipfs_bitswap_sent_all_blocks_bytes": true,
104+
"ipfs_bitswap_want_blocks_total": true,
105+
"ipfs_bitswap_wantlist_total": true,
106+
"ipfs_bs_cache_arc_hits_total": true,
107+
"ipfs_bs_cache_arc_total": true,
108+
"ipfs_fsrepo_datastore_batchcommit_errors_total": true,
109+
"ipfs_fsrepo_datastore_batchcommit_latency_seconds": true,
110+
"ipfs_fsrepo_datastore_batchcommit_total": true,
111+
"ipfs_fsrepo_datastore_batchdelete_errors_total": true,
112+
"ipfs_fsrepo_datastore_batchdelete_latency_seconds": true,
113+
"ipfs_fsrepo_datastore_batchdelete_total": true,
114+
"ipfs_fsrepo_datastore_batchput_errors_total": true,
115+
"ipfs_fsrepo_datastore_batchput_latency_seconds": true,
116+
"ipfs_fsrepo_datastore_batchput_size_bytes": true,
117+
"ipfs_fsrepo_datastore_batchput_total": true,
118+
"ipfs_fsrepo_datastore_check_errors_total": true,
119+
"ipfs_fsrepo_datastore_check_latency_seconds": true,
120+
"ipfs_fsrepo_datastore_check_total": true,
121+
"ipfs_fsrepo_datastore_delete_errors_total": true,
122+
"ipfs_fsrepo_datastore_delete_latency_seconds": true,
123+
"ipfs_fsrepo_datastore_delete_total": true,
124+
"ipfs_fsrepo_datastore_du_errors_total": true,
125+
"ipfs_fsrepo_datastore_du_latency_seconds": true,
126+
"ipfs_fsrepo_datastore_du_total": true,
127+
"ipfs_fsrepo_datastore_gc_errors_total": true,
128+
"ipfs_fsrepo_datastore_gc_latency_seconds": true,
129+
"ipfs_fsrepo_datastore_gc_total": true,
130+
"ipfs_fsrepo_datastore_get_errors_total": true,
131+
"ipfs_fsrepo_datastore_get_latency_seconds": true,
132+
"ipfs_fsrepo_datastore_get_size_bytes": true,
133+
"ipfs_fsrepo_datastore_get_total": true,
134+
"ipfs_fsrepo_datastore_getsize_errors_total": true,
135+
"ipfs_fsrepo_datastore_getsize_latency_seconds": true,
136+
"ipfs_fsrepo_datastore_getsize_total": true,
137+
"ipfs_fsrepo_datastore_has_errors_total": true,
138+
"ipfs_fsrepo_datastore_has_latency_seconds": true,
139+
"ipfs_fsrepo_datastore_has_total": true,
140+
"ipfs_fsrepo_datastore_put_errors_total": true,
141+
"ipfs_fsrepo_datastore_put_latency_seconds": true,
142+
"ipfs_fsrepo_datastore_put_size_bytes": true,
143+
"ipfs_fsrepo_datastore_put_total": true,
144+
"ipfs_fsrepo_datastore_query_errors_total": true,
145+
"ipfs_fsrepo_datastore_query_latency_seconds": true,
146+
"ipfs_fsrepo_datastore_query_total": true,
147+
"ipfs_fsrepo_datastore_scrub_errors_total": true,
148+
"ipfs_fsrepo_datastore_scrub_latency_seconds": true,
149+
"ipfs_fsrepo_datastore_scrub_total": true,
150+
"ipfs_fsrepo_datastore_sync_errors_total": true,
151+
"ipfs_fsrepo_datastore_sync_latency_seconds": true,
152+
"ipfs_fsrepo_datastore_sync_total": true,
153+
"ipfs_http_request_duration_seconds": true,
154+
"ipfs_http_request_size_bytes": true,
155+
"ipfs_http_requests_total": true,
156+
"ipfs_http_response_size_bytes": true,
157+
"ipfs_info": true,
158+
"leveldb_datastore_batchcommit_errors_total": true,
159+
"leveldb_datastore_batchcommit_latency_seconds": true,
160+
"leveldb_datastore_batchcommit_total": true,
161+
"leveldb_datastore_batchdelete_errors_total": true,
162+
"leveldb_datastore_batchdelete_latency_seconds": true,
163+
"leveldb_datastore_batchdelete_total": true,
164+
"leveldb_datastore_batchput_errors_total": true,
165+
"leveldb_datastore_batchput_latency_seconds": true,
166+
"leveldb_datastore_batchput_size_bytes": true,
167+
"leveldb_datastore_batchput_total": true,
168+
"leveldb_datastore_check_errors_total": true,
169+
"leveldb_datastore_check_latency_seconds": true,
170+
"leveldb_datastore_check_total": true,
171+
"leveldb_datastore_delete_errors_total": true,
172+
"leveldb_datastore_delete_latency_seconds": true,
173+
"leveldb_datastore_delete_total": true,
174+
"leveldb_datastore_du_errors_total": true,
175+
"leveldb_datastore_du_latency_seconds": true,
176+
"leveldb_datastore_du_total": true,
177+
"leveldb_datastore_gc_errors_total": true,
178+
"leveldb_datastore_gc_latency_seconds": true,
179+
"leveldb_datastore_gc_total": true,
180+
"leveldb_datastore_get_errors_total": true,
181+
"leveldb_datastore_get_latency_seconds": true,
182+
"leveldb_datastore_get_size_bytes": true,
183+
"leveldb_datastore_get_total": true,
184+
"leveldb_datastore_getsize_errors_total": true,
185+
"leveldb_datastore_getsize_latency_seconds": true,
186+
"leveldb_datastore_getsize_total": true,
187+
"leveldb_datastore_has_errors_total": true,
188+
"leveldb_datastore_has_latency_seconds": true,
189+
"leveldb_datastore_has_total": true,
190+
"leveldb_datastore_put_errors_total": true,
191+
"leveldb_datastore_put_latency_seconds": true,
192+
"leveldb_datastore_put_size_bytes": true,
193+
"leveldb_datastore_put_total": true,
194+
"leveldb_datastore_query_errors_total": true,
195+
"leveldb_datastore_query_latency_seconds": true,
196+
"leveldb_datastore_query_total": true,
197+
"leveldb_datastore_scrub_errors_total": true,
198+
"leveldb_datastore_scrub_latency_seconds": true,
199+
"leveldb_datastore_scrub_total": true,
200+
"leveldb_datastore_sync_errors_total": true,
201+
"leveldb_datastore_sync_latency_seconds": true,
202+
"leveldb_datastore_sync_total": true,
203+
"libp2p_autonat_next_probe_timestamp": true,
204+
"libp2p_autonat_reachability_status": true,
205+
"libp2p_autonat_reachability_status_confidence": true,
206+
"libp2p_autorelay_candidate_loop_state": true,
207+
"libp2p_autorelay_candidates_circuit_v2_support_total": true,
208+
"libp2p_autorelay_desired_reservations": true,
209+
"libp2p_autorelay_relay_addresses_count": true,
210+
"libp2p_autorelay_relay_addresses_updated_total": true,
211+
"libp2p_autorelay_reservation_requests_outcome_total": true,
212+
"libp2p_autorelay_reservations_closed_total": true,
213+
"libp2p_autorelay_reservations_opened_total": true,
214+
"libp2p_autorelay_status": true,
215+
"libp2p_eventbus_events_emitted_total": true,
216+
"libp2p_eventbus_subscriber_event_queued": true,
217+
"libp2p_eventbus_subscriber_queue_full": true,
218+
"libp2p_eventbus_subscriber_queue_length": true,
219+
"libp2p_eventbus_subscribers_total": true,
220+
"libp2p_identify_addrs_count": true,
221+
"libp2p_identify_addrs_received": true,
222+
"libp2p_identify_identify_pushes_triggered_total": true,
223+
"libp2p_identify_protocols_count": true,
224+
"libp2p_identify_protocols_received": true,
225+
"libp2p_relaysvc_connection_duration_seconds": true,
226+
"libp2p_relaysvc_data_transferred_bytes_total": true,
227+
"libp2p_relaysvc_status": true,
228+
}
229+
230+
func TestPrometheusMetrics(t *testing.T) {
231+
fetchMetricFamilies := func(n *harness.Node) map[string]bool {
232+
resp := n.APIClient().Get("/debug/metrics/prometheus")
233+
parser := &expfmt.TextParser{}
234+
fams, err := parser.TextToMetricFamilies(strings.NewReader(resp.Body))
235+
require.NoError(t, err)
236+
names := map[string]bool{}
237+
for k := range fams {
238+
names[k] = true
239+
}
240+
return names
241+
}
242+
243+
assertMetricsEventuallyContainOnly := func(n *harness.Node, m map[string]bool) {
244+
missing := &[]string{}
245+
assert.Eventuallyf(t, func() bool {
246+
*missing = make([]string, 0)
247+
fams := fetchMetricFamilies(n)
248+
for fam := range m {
249+
_, ok := fams[fam]
250+
if !ok {
251+
*missing = append(*missing, fam)
252+
}
253+
}
254+
if len(*missing) > 0 {
255+
return false
256+
}
257+
return len(fams) == len(m)
258+
}, 10*time.Second, 100*time.Millisecond, "expected metrics: %v", missing)
259+
}
260+
261+
t.Run("default configuration", func(t *testing.T) {
262+
t.Parallel()
263+
node := harness.NewT(t).NewNode().Init().StartDaemon()
264+
expectedFams := testutils.SetUnion(metricsDefaultFamilies, metricsRcmgrFamilies)
265+
assertMetricsEventuallyContainOnly(node, expectedFams)
266+
})
267+
268+
t.Run("resource manager disabled, should not contain rcmgr metrics", func(t *testing.T) {
269+
t.Parallel()
270+
node := harness.NewT(t).NewNode().Init()
271+
node.UpdateConfig(func(cfg *config.Config) {
272+
cfg.Swarm.ResourceMgr.Enabled = config.False
273+
})
274+
node.StartDaemon()
275+
assertMetricsEventuallyContainOnly(node, metricsDefaultFamilies)
276+
})
277+
}

test/cli/testutils/set.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package testutils
2+
3+
func SetUnion[T comparable](sets ...map[T]bool) map[T]bool {
4+
newM := map[T]bool{}
5+
for _, s := range sets {
6+
for k, v := range s {
7+
newM[k] = v
8+
}
9+
}
10+
return newM
11+
}

0 commit comments

Comments
 (0)