summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/prometheus/client_golang/prometheus/process_collector_test.go
blob: d3362dae72e5dd4615137418d4f2b997f637145d (plain)
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
package prometheus

import (
	"bytes"
	"os"
	"regexp"
	"testing"

	"github.com/prometheus/common/expfmt"
	"github.com/prometheus/procfs"
)

func TestProcessCollector(t *testing.T) {
	if _, err := procfs.Self(); err != nil {
		t.Skipf("skipping TestProcessCollector, procfs not available: %s", err)
	}

	registry := NewRegistry()
	if err := registry.Register(NewProcessCollector(os.Getpid(), "")); err != nil {
		t.Fatal(err)
	}
	if err := registry.Register(NewProcessCollectorPIDFn(
		func() (int, error) { return os.Getpid(), nil }, "foobar"),
	); err != nil {
		t.Fatal(err)
	}

	mfs, err := registry.Gather()
	if err != nil {
		t.Fatal(err)
	}

	var buf bytes.Buffer
	for _, mf := range mfs {
		if _, err := expfmt.MetricFamilyToText(&buf, mf); err != nil {
			t.Fatal(err)
		}
	}

	for _, re := range []*regexp.Regexp{
		regexp.MustCompile("process_cpu_seconds_total [0-9]"),
		regexp.MustCompile("process_max_fds [1-9]"),
		regexp.MustCompile("process_open_fds [1-9]"),
		regexp.MustCompile("process_virtual_memory_bytes [1-9]"),
		regexp.MustCompile("process_resident_memory_bytes [1-9]"),
		regexp.MustCompile("process_start_time_seconds [0-9.]{10,}"),
		regexp.MustCompile("foobar_process_cpu_seconds_total [0-9]"),
		regexp.MustCompile("foobar_process_max_fds [1-9]"),
		regexp.MustCompile("foobar_process_open_fds [1-9]"),
		regexp.MustCompile("foobar_process_virtual_memory_bytes [1-9]"),
		regexp.MustCompile("foobar_process_resident_memory_bytes [1-9]"),
		regexp.MustCompile("foobar_process_start_time_seconds [0-9.]{10,}"),
	} {
		if !re.Match(buf.Bytes()) {
			t.Errorf("want body to match %s\n%s", re, buf.String())
		}
	}
}