mirror of
https://github.com/prometheus/node_exporter.git
synced 2025-02-02 08:42:31 -08:00
Add test
This commit is contained in:
parent
ee37a27d91
commit
450fe0f3ba
|
@ -1,4 +1,4 @@
|
||||||
// Copyright 2015 The Prometheus Authors
|
// Copyright 2016 The Prometheus Authors
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
// you may not use this file except in compliance with the License.
|
// you may not use this file except in compliance with the License.
|
||||||
// You may obtain a copy of the License at
|
// You may obtain a copy of the License at
|
||||||
|
@ -103,9 +103,7 @@ func NewStatCollector() (Collector, error) {
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Expose CPU stats using sysctl.
|
func getDragonFlyCPUTimes() ([]float64, error) {
|
||||||
func (c *statCollector) Update(ch chan<- prometheus.Metric) error {
|
|
||||||
|
|
||||||
// We want time spent per-cpu per CPUSTATE.
|
// We want time spent per-cpu per CPUSTATE.
|
||||||
// CPUSTATES (number of CPUSTATES) is defined as 5U.
|
// CPUSTATES (number of CPUSTATES) is defined as 5U.
|
||||||
// States: CP_USER | CP_NICE | CP_SYS | CP_IDLE | CP_INTR
|
// States: CP_USER | CP_NICE | CP_SYS | CP_IDLE | CP_INTR
|
||||||
|
@ -115,23 +113,39 @@ func (c *statCollector) Update(ch chan<- prometheus.Metric) error {
|
||||||
//
|
//
|
||||||
// Look into sys/kern/kern_clock.c for details.
|
// Look into sys/kern/kern_clock.c for details.
|
||||||
|
|
||||||
var cpuTimesC *C.uint64_t
|
var (
|
||||||
var cpuTimerFreq C.long
|
cpuTimesC *C.uint64_t
|
||||||
var cpuTimesLength C.size_t
|
cpuTimerFreq C.long
|
||||||
var fieldsCount = 5
|
cpuTimesLength C.size_t
|
||||||
|
)
|
||||||
|
|
||||||
if C.getCPUTimes(&cpuTimesC, &cpuTimesLength, &cpuTimerFreq) == -1 {
|
if C.getCPUTimes(&cpuTimesC, &cpuTimesLength, &cpuTimerFreq) == -1 {
|
||||||
return errors.New("could not retrieve CPU times")
|
return nil, errors.New("could not retrieve CPU times")
|
||||||
}
|
}
|
||||||
|
defer C.free(unsafe.Pointer(cpuTimesC))
|
||||||
|
|
||||||
cpuTimes := (*[maxCPUTimesLen]C.uint64_t)(unsafe.Pointer(cpuTimesC))[:cpuTimesLength:cpuTimesLength]
|
cput := (*[maxCPUTimesLen]C.uint64_t)(unsafe.Pointer(cpuTimesC))[:cpuTimesLength:cpuTimesLength]
|
||||||
C.free(unsafe.Pointer(cpuTimesC))
|
|
||||||
|
cpuTimes := make([]float64, cpuTimesLength)
|
||||||
|
for i, value := range cput {
|
||||||
|
cpuTimes[i] = float64(value) / float64(cpuTimerFreq)
|
||||||
|
}
|
||||||
|
return cpuTimes, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Expose CPU stats using sysctl.
|
||||||
|
func (c *statCollector) Update(ch chan<- prometheus.Metric) error {
|
||||||
|
var fieldsCount = 5
|
||||||
|
cpuTimes, err := getDragonFlyCPUTimes()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// Export order: user nice sys intr idle
|
// Export order: user nice sys intr idle
|
||||||
cpuFields := []string{"user", "nice", "sys", "interrupt", "idle"}
|
cpuFields := []string{"user", "nice", "sys", "interrupt", "idle"}
|
||||||
for i, value := range cpuTimes {
|
for i, value := range cpuTimes {
|
||||||
cpux := fmt.Sprintf("cpu%d", i/fieldsCount)
|
cpux := fmt.Sprintf("cpu%d", i/fieldsCount)
|
||||||
ch <- prometheus.MustNewConstMetric(c.cpu, prometheus.CounterValue, float64(value)/float64(cpuTimerFreq), cpux, cpuFields[i%fieldsCount])
|
ch <- prometheus.MustNewConstMetric(c.cpu, prometheus.CounterValue, value, cpux, cpuFields[i%fieldsCount])
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
41
collector/cpu_dragonfly_test.go
Normal file
41
collector/cpu_dragonfly_test.go
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
// Copyright 2016 The Prometheus Authors
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
// +build !nocpu
|
||||||
|
|
||||||
|
package collector
|
||||||
|
|
||||||
|
import (
|
||||||
|
"runtime"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCPU(t *testing.T) {
|
||||||
|
var (
|
||||||
|
fieldsCount = 5
|
||||||
|
times, err = getDragonFlyCPUTimes()
|
||||||
|
)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("expected no error, got %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(times) == 0 {
|
||||||
|
t.Fatalf("no cputimes found")
|
||||||
|
}
|
||||||
|
|
||||||
|
want := runtime.NumCPU() * fieldsCount
|
||||||
|
if len(times) != want {
|
||||||
|
t.Fatalf("should have %d cpuTimes: got %d", want, len(times))
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue