From 99d64f8879bc98fac31cab2ffbd7aa1de9aaf594 Mon Sep 17 00:00:00 2001 From: Tobias Schmidt Date: Wed, 2 Nov 2016 14:09:24 -0400 Subject: [PATCH] Fix namespace of process collector metrics --- prometheus/process_collector.go | 17 +++++++++++------ prometheus/process_collector_test.go | 24 ++++++++++++------------ 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/prometheus/process_collector.go b/prometheus/process_collector.go index 9274437..94b2553 100644 --- a/prometheus/process_collector.go +++ b/prometheus/process_collector.go @@ -44,37 +44,42 @@ func NewProcessCollectorPIDFn( pidFn func() (int, error), namespace string, ) Collector { + ns := "" + if len(namespace) > 0 { + ns = namespace + "_" + } + c := processCollector{ pidFn: pidFn, collectFn: func(chan<- Metric) {}, cpuTotal: NewDesc( - namespace+"_process_cpu_seconds_total", + ns+"process_cpu_seconds_total", "Total user and system CPU time spent in seconds.", nil, nil, ), openFDs: NewDesc( - namespace+"_process_open_fds", + ns+"process_open_fds", "Number of open file descriptors.", nil, nil, ), maxFDs: NewDesc( - namespace+"_process_max_fds", + ns+"process_max_fds", "Maximum number of open file descriptors.", nil, nil, ), vsize: NewDesc( - namespace+"_process_virtual_memory_bytes", + ns+"process_virtual_memory_bytes", "Virtual memory size in bytes.", nil, nil, ), rss: NewDesc( - namespace+"_process_resident_memory_bytes", + ns+"process_resident_memory_bytes", "Resident memory size in bytes.", nil, nil, ), startTime: NewDesc( - namespace+"_process_start_time_seconds", + ns+"process_start_time_seconds", "Start time of the process since unix epoch in seconds.", nil, nil, ), diff --git a/prometheus/process_collector_test.go b/prometheus/process_collector_test.go index d3362da..c7acb47 100644 --- a/prometheus/process_collector_test.go +++ b/prometheus/process_collector_test.go @@ -38,18 +38,18 @@ func TestProcessCollector(t *testing.T) { } 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,}"), + regexp.MustCompile("\nprocess_cpu_seconds_total [0-9]"), + regexp.MustCompile("\nprocess_max_fds [1-9]"), + regexp.MustCompile("\nprocess_open_fds [1-9]"), + regexp.MustCompile("\nprocess_virtual_memory_bytes [1-9]"), + regexp.MustCompile("\nprocess_resident_memory_bytes [1-9]"), + regexp.MustCompile("\nprocess_start_time_seconds [0-9.]{10,}"), + regexp.MustCompile("\nfoobar_process_cpu_seconds_total [0-9]"), + regexp.MustCompile("\nfoobar_process_max_fds [1-9]"), + regexp.MustCompile("\nfoobar_process_open_fds [1-9]"), + regexp.MustCompile("\nfoobar_process_virtual_memory_bytes [1-9]"), + regexp.MustCompile("\nfoobar_process_resident_memory_bytes [1-9]"), + regexp.MustCompile("\nfoobar_process_start_time_seconds [0-9.]{10,}"), } { if !re.Match(buf.Bytes()) { t.Errorf("want body to match %s\n%s", re, buf.String())