windows_exporter/tools/collector-generator/collector.template

62 lines
1.9 KiB
Plaintext

package collector
import (
"github.com/StackExchange/wmi"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
)
func init() {
Factories["{{ .CollectorName | toLower }}"] = New{{ .CollectorName }}Collector
}
// A {{ .CollectorName }}Collector is a Prometheus collector for WMI {{ .Class }} metrics
type {{ .CollectorName }}Collector struct {
{{- range $m := .Members }}
{{ $m.Name }} *prometheus.Desc
{{- end }}
}
// New{{ .CollectorName }}Collector ...
func New{{ .CollectorName }}Collector() (Collector, error) {
const subsystem = "{{ .CollectorName | toLower }}"
return &{{ .CollectorName }}Collector{
{{- range $m := .Members }}
{{ $m.Name }}: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, subsystem, "{{ $m.Name | toSnakeCase }}"),
"({{ $m.Name }})",
nil,
nil,
),
{{- end }}
}, nil
}
// Collect sends the metric values for each metric
// to the provided prometheus Metric channel.
func (c *{{ .CollectorName }}Collector) Collect(ch chan<- prometheus.Metric) error {
if desc, err := c.collect(ch); err != nil {
log.Error("failed collecting {{ .CollectorName | toLower }} metrics:", desc, err)
return err
}
return nil
}
// {{ .Class }} docs:
// - <add link to documentation here>
type {{ .Class }} struct {
Name string
{{ range $m := .Members }}
{{ $m.Name }} {{ $m.Type }}
{{- end }}
}
func (c *{{ .CollectorName }}Collector) collect(ch chan<- prometheus.Metric) (*prometheus.Desc, error) {
var dst []{{ .Class }}
q := queryAll(&dst)
if err := wmi.Query(q, &dst); err != nil {
return nil, err
}
{{ range $m := .Members }}
ch <- prometheus.MustNewConstMetric(
c.{{ $m.Name }},
prometheus.GaugeValue,
float64(dst[0].{{ $m.Name }}),
)
{{ end }}
return nil, nil
}