Debian Codegraph @

prometheus/process_collector_windows_test.go

raw
1 // Copyright 2019 The Prometheus Authors
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package prometheus
15
16 import (
17 "bytes"
18 "os"
19 "regexp"
20 "testing"
21
22 "github.com/prometheus/common/expfmt"
23 )
24
25 func TestWindowsProcessCollector(t *testing.T) {
26 registry := NewRegistry()
27 if err := registry.Register(NewProcessCollector(ProcessCollectorOpts{})); err != nil {
28 t.Fatal(err)
29 }
30 if err := registry.Register(NewProcessCollector(ProcessCollectorOpts{
31 PidFn: func() (int, error) { return os.Getpid(), nil },
32 Namespace: "foobar",
33 ReportErrors: true, // No errors expected, just to see if none are reported.
34 })); err != nil {
35 t.Fatal(err)
36 }
37
38 mfs, err := registry.Gather()
39 if err != nil {
40 t.Fatal(err)
41 }
42
43 var buf bytes.Buffer
44 for _, mf := range mfs {
45 if _, err := expfmt.MetricFamilyToText(&buf, mf); err != nil {
46 t.Fatal(err)
47 }
48 }
49
50 for _, re := range []*regexp.Regexp{
51 regexp.MustCompile("\nprocess_cpu_seconds_total [0-9]"),
52 regexp.MustCompile("\nprocess_max_fds [1-9]"),
53 regexp.MustCompile("\nprocess_open_fds [1-9]"),
54 regexp.MustCompile("\nprocess_virtual_memory_max_bytes (-1|[1-9])"),
55 regexp.MustCompile("\nprocess_virtual_memory_bytes [1-9]"),
56 regexp.MustCompile("\nprocess_resident_memory_bytes [1-9]"),
57 regexp.MustCompile("\nprocess_start_time_seconds [0-9.]{10,}"),
58 regexp.MustCompile("\nfoobar_process_cpu_seconds_total [0-9]"),
59 regexp.MustCompile("\nfoobar_process_max_fds [1-9]"),
60 regexp.MustCompile("\nfoobar_process_open_fds [1-9]"),
61 regexp.MustCompile("\nfoobar_process_virtual_memory_max_bytes (-1|[1-9])"),
62 regexp.MustCompile("\nfoobar_process_virtual_memory_bytes [1-9]"),
63 regexp.MustCompile("\nfoobar_process_resident_memory_bytes [1-9]"),
64 regexp.MustCompile("\nfoobar_process_start_time_seconds [0-9.]{10,}"),
65 } {
66 if !re.Match(buf.Bytes()) {
67 t.Errorf("want body to match %s\n%s", re, buf.String())
68 }
69 }
70 }
71
72 func TestWindowsDescribeAndCollectAlignment(t *testing.T) {
73 collector := &processCollector{
74 pidFn: getPIDFn(),
75 cpuTotal: NewDesc("cpu_total", "Total CPU usage", nil, nil),
76 openFDs: NewDesc("open_fds", "Number of open file descriptors", nil, nil),
77 maxFDs: NewDesc("max_fds", "Maximum file descriptors", nil, nil),
78 vsize: NewDesc("vsize", "Virtual memory size", nil, nil),
79 maxVsize: NewDesc("max_vsize", "Maximum virtual memory size", nil, nil),
80 rss: NewDesc("rss", "Resident Set Size", nil, nil),
81 startTime: NewDesc("start_time", "Process start time", nil, nil),
82 inBytes: NewDesc("in_bytes", "Input bytes", nil, nil),
83 outBytes: NewDesc("out_bytes", "Output bytes", nil, nil),
84 }
85
86 // Collect and get descriptors
87 descCh := make(chan *Desc, 15)
88 collector.describe(descCh)
89 close(descCh)
90
91 definedDescs := make(map[string]bool)
92 for desc := range descCh {
93 definedDescs[desc.String()] = true
94 }
95
96 // Collect and get metrics
97 metricsCh := make(chan Metric, 15)
98 collector.processCollect(metricsCh)
99 close(metricsCh)
100
101 collectedMetrics := make(map[string]bool)
102 for metric := range metricsCh {
103 collectedMetrics[metric.Desc().String()] = true
104 }
105
106 // Verify that all described metrics are collected
107 for desc := range definedDescs {
108 if !collectedMetrics[desc] {
109 t.Errorf("Metric %s described but not collected", desc)
110 }
111 }
112
113 // Verify that no extra metrics are collected
114 for desc := range collectedMetrics {
115 if !definedDescs[desc] {
116 t.Errorf("Metric %s collected but not described", desc)
117 }
118 }
119 }