Skip to content

Commit

Permalink
Merge pull request #3 from astappiev/exporter-metrics
Browse files Browse the repository at this point in the history
feat: add `--web.disable-exporter-metrics` flag
  • Loading branch information
plazonic authored Dec 14, 2024
2 parents f42653a + cbe4e32 commit 9ad318c
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
Expand All @@ -20,7 +19,8 @@ const (
)

var (
addr = flag.String("web.listen-address", ":9445", "Address to listen on for web interface and telemetry.")
addr = flag.String("web.listen-address", ":9445", "Address to listen on for web interface and telemetry.")
disableExporterMetrics = flag.Bool("web.disable-exporter-metrics", false, "Exclude metrics about the exporter itself (promhttp_*, process_*, go_*)")

labels = []string{"ordinal", "minor_number", "uuid", "name"}
eccLabels = []string{"ordinal", "minor_number", "uuid", "name", "error", "counter"}
Expand Down Expand Up @@ -329,7 +329,7 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) {
}
}
if slurmInfo != "" {
content, err := ioutil.ReadFile(slurmInfo)
content, err := os.ReadFile(slurmInfo)
if err == nil {
fmt.Sscanf(string(content), "%d %d", &jobId, &jobUid)
}
Expand All @@ -351,6 +351,23 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) {
c.jobUid.Collect(ch)
}

func metricsHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
registry := prometheus.NewRegistry()

registry.MustRegister(NewCollector())

gatherers := prometheus.Gatherers{registry}
if !*disableExporterMetrics {
gatherers = append(gatherers, prometheus.DefaultGatherer)
}

// Delegate http serving to Prometheus client library, which will call collector.Collect.
h := promhttp.HandlerFor(gatherers, promhttp.HandlerOpts{})
h.ServeHTTP(w, r)
}
}

func main() {
flag.Parse()

Expand All @@ -365,8 +382,9 @@ func main() {
log.Printf("SystemGetDriverVersion(): %v", driverVersion)
}

prometheus.MustRegister(NewCollector())
metricsEndpoint := "/metrics"
http.Handle(metricsEndpoint, metricsHandler())

// Serve on all paths under addr
log.Fatalf("ListenAndServe error: %v", http.ListenAndServe(*addr, promhttp.Handler()))
log.Fatalf("ListenAndServe error: %v", http.ListenAndServe(*addr, nil))
}

0 comments on commit 9ad318c

Please sign in to comment.