File: base_test.go

package info (click to toggle)
prometheus-process-exporter 0.8.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 432 kB
  • sloc: makefile: 87; sh: 58
file content (105 lines) | stat: -rw-r--r-- 2,497 bytes parent folder | download | duplicates (3)
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
package proc

import (
	"fmt"
	"time"

	common "github.com/ncabatoff/process-exporter"
)

type msi map[string]int

// procinfo reads the ProcIdInfo for a proc and returns it or a zero value plus
// an error.
func procinfo(p Proc) (IDInfo, error) {
	id, err := p.GetProcID()
	if err != nil {
		return IDInfo{}, err
	}
	static, err := p.GetStatic()
	if err != nil {
		return IDInfo{}, err
	}
	metrics, _, err := p.GetMetrics()
	if err != nil {
		return IDInfo{}, err
	}
	return IDInfo{id, static, metrics, nil}, nil
}

// read everything in the iterator
func consumeIter(pi Iter) ([]IDInfo, error) {
	infos := []IDInfo{}
	for pi.Next() {
		info, err := procinfo(pi)
		if err != nil {
			return nil, err
		}
		infos = append(infos, info)
	}
	return infos, nil
}

type namer map[string]struct{}

func newNamer(names ...string) namer {
	nr := make(namer, len(names))
	for _, name := range names {
		nr[name] = struct{}{}
	}
	return nr
}

func (n namer) String() string {
	var ss = make([]string, 0, len(n))
	for s := range n {
		ss = append(ss, s)
	}
	return fmt.Sprintf("%v", ss)
}

func (n namer) MatchAndName(nacl common.ProcAttributes) (bool, string) {
	if _, ok := n[nacl.Name]; ok {
		return true, nacl.Name
	}
	return false, ""
}

func newProcIDStatic(pid, ppid int, startTime uint64, name string, cmdline []string) (ID, Static) {
	return ID{pid, startTime},
		Static{name, cmdline, []string{}, ppid, time.Unix(int64(startTime), 0).UTC(), 1000}
}

func newProc(pid int, name string, m Metrics) IDInfo {
	id, static := newProcIDStatic(pid, 0, 0, name, nil)
	return IDInfo{id, static, m, nil}
}

func newProcStart(pid int, name string, startTime uint64) IDInfo {
	id, static := newProcIDStatic(pid, 0, startTime, name, nil)
	return IDInfo{id, static, Metrics{}, nil}
}

func newProcParent(pid int, name string, ppid int) IDInfo {
	id, static := newProcIDStatic(pid, ppid, 0, name, nil)
	return IDInfo{id, static, Metrics{}, nil}
}

func piinfot(pid int, name string, c Counts, m Memory, f Filedesc, threads []Thread) IDInfo {
	pii := piinfo(pid, name, c, m, f, len(threads))
	pii.Threads = threads
	return pii
}

func piinfo(pid int, name string, c Counts, m Memory, f Filedesc, t int) IDInfo {
	return piinfost(pid, name, c, m, f, t, States{})
}

func piinfost(pid int, name string, c Counts, m Memory, f Filedesc, t int, s States) IDInfo {
	id, static := newProcIDStatic(pid, 0, 0, name, nil)
	return IDInfo{
		ID:      id,
		Static:  static,
		Metrics: Metrics{c, m, f, uint64(t), s, ""},
	}
}