Merge pull request #500 from martinlindhe/update-template

Update collector generator template
This commit is contained in:
Calle Pettersson 2020-05-24 20:03:44 +02:00 committed by GitHub
commit 5052f4d00e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 13 deletions

View File

@ -5,7 +5,7 @@ import (
"github.com/prometheus/common/log" "github.com/prometheus/common/log"
) )
func init() { func init() {
Factories["{{ .CollectorName | toLower }}"] = New{{ .CollectorName }}Collector registerCollector("{{ .CollectorName | toLower }}", new{{ .CollectorName }}Collector) // TODO: Add any perflib dependencies here
} }
// A {{ .CollectorName }}Collector is a Prometheus collector for WMI {{ .Class }} metrics // A {{ .CollectorName }}Collector is a Prometheus collector for WMI {{ .Class }} metrics
type {{ .CollectorName }}Collector struct { type {{ .CollectorName }}Collector struct {
@ -13,8 +13,8 @@ type {{ .CollectorName }}Collector struct {
{{ $m.Name }} *prometheus.Desc {{ $m.Name }} *prometheus.Desc
{{- end }} {{- end }}
} }
// New{{ .CollectorName }}Collector ...
func New{{ .CollectorName }}Collector() (Collector, error) { func new{{ .CollectorName }}Collector() (Collector, error) {
const subsystem = "{{ .CollectorName | toLower }}" const subsystem = "{{ .CollectorName | toLower }}"
return &{{ .CollectorName }}Collector{ return &{{ .CollectorName }}Collector{
{{- range $m := .Members }} {{- range $m := .Members }}
@ -27,15 +27,7 @@ func New{{ .CollectorName }}Collector() (Collector, error) {
{{- end }} {{- end }}
}, nil }, nil
} }
// Collect sends the metric values for each metric
// to the provided prometheus Metric channel.
func (c *{{ .CollectorName }}Collector) Collect(ctx *ScrapeContext, 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: // {{ .Class }} docs:
// - <add link to documentation here> // - <add link to documentation here>
type {{ .Class }} struct { type {{ .Class }} struct {
@ -44,7 +36,10 @@ type {{ .Class }} struct {
{{ $m.Name }} {{ $m.Type }} {{ $m.Name }} {{ $m.Type }}
{{- end }} {{- end }}
} }
func (c *{{ .CollectorName }}Collector) collect(ch chan<- prometheus.Metric) (*prometheus.Desc, error) {
// Collect sends the metric values for each metric
// to the provided prometheus Metric channel.
func (c *{{ .CollectorName }}Collector) Collect(ctx *ScrapeContext, ch chan<- prometheus.Metric) error {
var dst []{{ .Class }} var dst []{{ .Class }}
q := queryAll(&dst) q := queryAll(&dst)
if err := wmi.Query(q, &dst); err != nil { if err := wmi.Query(q, &dst); err != nil {