-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.go
46 lines (35 loc) · 1.37 KB
/
server.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
package main
import (
"fmt"
"net/http"
"time"
"github.com/GusAntoniassi/prometheus-html-exporter/internal/pkg/types"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
)
func getExporterMetricsRegistry() (*prometheus.Registry, error) {
metricRegistry := prometheus.NewRegistry()
err := metricRegistry.Register(collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}))
if err != nil {
return nil, fmt.Errorf("error registering process collector: %s", err.Error())
}
err = metricRegistry.Register(collectors.NewGoCollector())
if err != nil {
return nil, fmt.Errorf("error registering Go collector: %s", err.Error())
}
return metricRegistry, nil
}
func probeHandler(w http.ResponseWriter, r *http.Request, config types.ExporterConfig) {
// @TODO: gather some configs from query parameters, passed from Prometheus
start := time.Now()
collector := collector{config: config}
registry := prometheus.NewPedanticRegistry()
registry.MustRegister(collector)
h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
h.ServeHTTP(w, r)
duration := time.Since(start).Seconds()
// @TODO: expose metrics about duration
log.Debugf("scrape of endpoint %s finished in %0.2f seconds", config.ScrapeConfig.Address, duration)
}