Skip to content
This repository has been archived by the owner on Mar 27, 2024. It is now read-only.

Commit

Permalink
move collection funcs to a separate file + minor (#224)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyam8 authored May 23, 2019
1 parent 19f748b commit 4d72e49
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 60 deletions.
60 changes: 6 additions & 54 deletions modules/wmi/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,6 @@ import (
"github.com/prometheus/prometheus/pkg/labels"
)

const (
// defaults are cpu,cs,logical_disk,net,os,service,system,textfile
collectorCPU = "cpu"
collectorLogDisks = "logical_disk"
collectorNet = "net"
collectorOS = "os"
collectorSystem = "system"
collectorMemory = "memory"

metricCollectorDuration = "wmi_exporter_collector_duration_seconds"
metricCollectorSuccess = "wmi_exporter_collector_success"
)

func (w *WMI) collect() (map[string]int64, error) {
scraped, err := w.prom.Scrape()
if err != nil {
Expand All @@ -35,54 +22,19 @@ func (w *WMI) collect() (map[string]int64, error) {

func (w *WMI) collectScraped(mx *metrics, scraped prometheus.Metrics) {
collectCollection(mx, scraped)

w.collectCPU(mx, scraped)
w.collectOS(mx, scraped)
w.collectMemory(mx, scraped)
w.collectSystem(mx, scraped)
w.collectNet(mx, scraped)
w.collectLogicalDisk(mx, scraped)
collectCPU(mx, scraped)
collectOS(mx, scraped)
collectMemory(mx, scraped)
collectSystem(mx, scraped)
collectNet(mx, scraped)
collectLogicalDisk(mx, scraped)

if mx.hasOS() && mx.hasMem() {
v := sum(mx.OS.VisibleMemoryBytes, -mx.Memory.AvailableBytes)
mx.Memory.UsedBytes = &v
}
}

func collectCollection(mx *metrics, pms prometheus.Metrics) {
mx.Collectors = &collectors{}
collectCollectionDuration(mx, pms)
collectCollectionSuccess(mx, pms)
}

func collectCollectionDuration(mx *metrics, pms prometheus.Metrics) {
cr := newCollector("")
for _, pm := range pms.FindByName(metricCollectorDuration) {
name := pm.Labels.Get("collector")
if name == "" {
continue
}
if cr.ID != name {
cr = mx.Collectors.get(name, true)
}
cr.Duration = pm.Value
}
}

func collectCollectionSuccess(mx *metrics, pms prometheus.Metrics) {
cr := newCollector("")
for _, pm := range pms.FindByName(metricCollectorSuccess) {
name := pm.Labels.Get("collector")
if name == "" {
continue
}
if cr.ID != name {
cr = mx.Collectors.get(name, true)
}
cr.Success = pm.Value == 1
}
}

func checkCollector(pms prometheus.Metrics, name string) (enabled, success bool) {
m, err := labels.NewMatcher(labels.MatchEqual, "collector", name)
if err != nil {
Expand Down
42 changes: 42 additions & 0 deletions modules/wmi/collect_collection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package wmi

import "github.com/netdata/go.d.plugin/pkg/prometheus"

const (
metricCollectorDuration = "wmi_exporter_collector_duration_seconds"
metricCollectorSuccess = "wmi_exporter_collector_success"
)

func collectCollection(mx *metrics, pms prometheus.Metrics) {
mx.Collectors = &collectors{}
collectCollectionDuration(mx, pms)
collectCollectionSuccess(mx, pms)
}

func collectCollectionDuration(mx *metrics, pms prometheus.Metrics) {
cr := newCollector("")
for _, pm := range pms.FindByName(metricCollectorDuration) {
name := pm.Labels.Get("collector")
if name == "" {
continue
}
if cr.ID != name {
cr = mx.Collectors.get(name, true)
}
cr.Duration = pm.Value
}
}

func collectCollectionSuccess(mx *metrics, pms prometheus.Metrics) {
cr := newCollector("")
for _, pm := range pms.FindByName(metricCollectorSuccess) {
name := pm.Labels.Get("collector")
if name == "" {
continue
}
if cr.ID != name {
cr = mx.Collectors.get(name, true)
}
cr.Success = pm.Value == 1
}
}
4 changes: 3 additions & 1 deletion modules/wmi/collect_cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package wmi
import "github.com/netdata/go.d.plugin/pkg/prometheus"

const (
collectorCPU = "cpu"

metricCPUCstateTotal = "wmi_cpu_cstate_seconds_total"
metricCPUDPCsTotal = "wmi_cpu_dpcs_total"
metricCPUInterruptsTotal = "wmi_cpu_interrupts_total"
metricCPUTimeTotal = "wmi_cpu_time_total"
)

func (w *WMI) collectCPU(mx *metrics, pms prometheus.Metrics) bool {
func collectCPU(mx *metrics, pms prometheus.Metrics) bool {
enabled, success := checkCollector(pms, collectorCPU)
if !(enabled && success) {
return false
Expand Down
4 changes: 3 additions & 1 deletion modules/wmi/collect_logical_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
)

const (
collectorLogDisks = "logical_disk"

metricLDReadBytesTotal = "wmi_logical_disk_read_bytes_total"
metricLDWriteBytesTotal = "wmi_logical_disk_write_bytes_total"
metricLDReadsTotal = "wmi_logical_disk_reads_total"
Expand All @@ -16,7 +18,7 @@ const (
metricLDFreeBytes = "wmi_logical_disk_free_bytes"
)

func (w *WMI) collectLogicalDisk(mx *metrics, pms prometheus.Metrics) bool {
func collectLogicalDisk(mx *metrics, pms prometheus.Metrics) bool {
enabled, success := checkCollector(pms, collectorLogDisks)
if !(enabled && success) {
return false
Expand Down
4 changes: 3 additions & 1 deletion modules/wmi/collect_memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
)

const (
collectorMemory = "memory"

metricMemAvailBytes = "wmi_memory_available_bytes"
metricMemCacheBytes = "wmi_memory_cache_bytes"
metricMemCacheBytesPeak = "wmi_memory_cache_bytes_peak"
Expand Down Expand Up @@ -41,7 +43,7 @@ const (
metricMemWriteCopiesTotal = "wmi_memory_write_copies_total"
)

func (w *WMI) collectMemory(mx *metrics, pms prometheus.Metrics) bool {
func collectMemory(mx *metrics, pms prometheus.Metrics) bool {
enabled, success := checkCollector(pms, collectorMemory)
if !(enabled && success) {
return false
Expand Down
4 changes: 3 additions & 1 deletion modules/wmi/collect_net.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
)

const (
collectorNet = "net"

metricNetBytesReceivedTotal = "wmi_net_bytes_received_total"
metricNetBytesSentTotal = "wmi_net_bytes_sent_total"
metricNetBytesTotal = "wmi_net_bytes_total"
Expand All @@ -22,7 +24,7 @@ const (
metricNetCurrentBandwidth = "wmi_net_current_bandwidth"
)

func (w *WMI) collectNet(mx *metrics, pms prometheus.Metrics) bool {
func collectNet(mx *metrics, pms prometheus.Metrics) bool {
enabled, success := checkCollector(pms, collectorNet)
if !(enabled && success) {
return false
Expand Down
4 changes: 3 additions & 1 deletion modules/wmi/collect_os.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
)

const (
collectorOS = "os"

metricOSPhysicalMemoryFreeBytes = "wmi_os_physical_memory_free_bytes"
metricOSPagingFreeBytes = "wmi_os_paging_free_bytes"
metricOSVirtualMemoryFreeBytes = "wmi_os_virtual_memory_free_bytes"
Expand All @@ -20,7 +22,7 @@ const (
metricOSTime = "wmi_os_time"
)

func (w *WMI) collectOS(mx *metrics, pms prometheus.Metrics) bool {
func collectOS(mx *metrics, pms prometheus.Metrics) bool {
enabled, success := checkCollector(pms, collectorOS)
if !(enabled && success) {
return false
Expand Down
4 changes: 3 additions & 1 deletion modules/wmi/collect_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
)

const (
collectorSystem = "system"

metricSysContextSwitchesTotal = "wmi_system_context_switches_total"
metricSysExceptionDispatchesTotal = "wmi_system_exception_dispatches_total"
metricSysProcessorQueueLength = "wmi_system_processor_queue_length"
Expand All @@ -16,7 +18,7 @@ const (
metricSysThreads = "wmi_system_threads"
)

func (w *WMI) collectSystem(mx *metrics, pms prometheus.Metrics) bool {
func collectSystem(mx *metrics, pms prometheus.Metrics) bool {
enabled, success := checkCollector(pms, collectorSystem)
if !(enabled && success) {
return false
Expand Down

0 comments on commit 4d72e49

Please sign in to comment.