This repository has been archived by the owner on Mar 5, 2024. It is now read-only.
forked from monitoring-tools/prom-puppet-agent-exporter
-
Notifications
You must be signed in to change notification settings - Fork 2
/
exporter.go
69 lines (56 loc) · 1.66 KB
/
exporter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"time"
"github.com/prometheus/client_golang/prometheus"
)
// PuppetExporter stats exporter
type PuppetExporter struct {
namespace string
reportScraper PuppetYamlReportScraper
scrapesSummary prometheus.Summary
failedScrapes prometheus.Counter
}
const subsystem = "exporter"
// NewPuppetExporter creates puppet stats exporter
func NewPuppetExporter(
namespace string,
reportScraper PuppetYamlReportScraper,
) *PuppetExporter {
scrapesSummary := prometheus.NewSummary(prometheus.SummaryOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "scrape_duration_seconds",
Help: "The scrapes durations summary and total count.",
})
failedScrapes := prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "scrapes_failed",
Help: "Current failed puppet scrapes.",
})
exporter := &PuppetExporter{
namespace: namespace,
reportScraper: reportScraper,
scrapesSummary: scrapesSummary,
failedScrapes: failedScrapes,
}
return exporter
}
// Describe implements prometheus.Collector interface
func (exp *PuppetExporter) Describe(ch chan<- *prometheus.Desc) {
ch <- exp.scrapesSummary.Desc()
ch <- exp.failedScrapes.Desc()
}
// Collect implements prometheus.Collector interface
func (exp *PuppetExporter) Collect(ch chan<- prometheus.Metric) {
if err := exp.metrics(ch); err != nil {
exp.failedScrapes.Inc()
}
ch <- exp.scrapesSummary
ch <- exp.failedScrapes
}
func (exp *PuppetExporter) metrics(ch chan<- prometheus.Metric) error {
now := time.Now()
defer exp.scrapesSummary.Observe(time.Since(now).Seconds())
return exp.reportScraper.CollectMetrics(ch)
}