-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpcp_exporter.go
136 lines (118 loc) · 3.55 KB
/
pcp_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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package main
import (
"flag"
"fmt"
"net/http"
"os"
"sync"
"time"
"github.com/HewlettPackard/pcp_exporter/sources"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/log"
"github.com/prometheus/common/version"
)
var (
scrapeDurations = prometheus.NewSummaryVec(
prometheus.SummaryOpts{
Namespace: sources.Namespace,
Subsystem: "exporter",
Name: "scrape_duration_seconds",
Help: "pcp_exporter: Duration of a scrape job.",
},
[]string{"source", "result"},
)
)
//PcpSource is a list of all sources that the user would like to collect.
type PcpSource struct {
sourceList map[string]sources.PcpSource
}
//Describe implements the prometheus.Describe interface
func (p PcpSource) Describe(ch chan<- *prometheus.Desc) {
scrapeDurations.Describe(ch)
}
//Collect implements the prometheus.Collect interface
func (p PcpSource) Collect(ch chan<- prometheus.Metric) {
wg := sync.WaitGroup{}
wg.Add(len(p.sourceList))
for name, c := range p.sourceList {
go func(name string, c sources.PcpSource) {
collectFromSource(name, c, ch)
wg.Done()
}(name, c)
}
wg.Wait()
scrapeDurations.Collect(ch)
}
func collectFromSource(name string, s sources.PcpSource, ch chan<- prometheus.Metric) {
result := "success"
begin := time.Now()
err := s.Update(ch)
duration := time.Since(begin)
if err != nil {
log.Errorf("ERROR: %q source failed after %f seconds: %s", name, duration.Seconds(), err)
result = "error"
} else {
log.Debugf("OK: %q source succeeded after %f seconds: %s", name, duration.Seconds(), err)
}
scrapeDurations.WithLabelValues(name, result).Observe(duration.Seconds())
}
func loadSources(list []string) (map[string]sources.PcpSource, error) {
sourceList := map[string]sources.PcpSource{}
for _, name := range list {
fn, ok := sources.Factories[name]
if !ok {
return nil, fmt.Errorf("source %q not available", name)
}
c, err := fn()
if err != nil {
return nil, err
}
sourceList[name] = c
}
return sourceList, nil
}
func init() {
prometheus.MustRegister(version.NewCollector("pcp_exporter"))
}
func main() {
var (
showVersion = flag.Bool("version", false, "Print version information.")
listenAddress = flag.String("web.listen-address", ":9259", "Address to use to expose pcp metrics.")
metricsPath = flag.String("web.telemetry-path", "/metrics", "Path to use to expose pcp metrics.")
)
flag.Parse()
if *showVersion {
fmt.Fprintln(os.Stdout, version.Print("pcp_exporter"))
os.Exit(0)
}
log.Infoln("Starting pcp_exporter", version.Info())
log.Infoln("Build context", version.BuildContext())
//expand to include more sources eventually (CLI, other?)
enabledSources := []string{"pmwebapi"}
sourceList, err := loadSources(enabledSources)
if err != nil {
log.Fatalf("Couldn't load sources: %q", err)
}
log.Infof("Enabled sources:")
for s := range sourceList {
log.Infof(" - %s", s)
}
prometheus.MustRegister(PcpSource{sourceList: sourceList})
handler := promhttp.HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{ErrorLog: log.NewErrorLogger()})
http.Handle(*metricsPath, prometheus.InstrumentHandler("prometheus", handler))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>PCP Exporter</title></head>
<body>
<h1>PCP Exporter</h1>
<p><a href="` + *metricsPath + `">Metrics</a></p>
</body>
</html>`))
})
log.Infoln("Listening on", *listenAddress)
err = http.ListenAndServe(*listenAddress, nil)
if err != nil {
log.Fatal(err)
}
}