Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix metrics path to be /metrics not /metrics/ #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 53 additions & 63 deletions pgme.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
package main

import (
"bytes"
"context"
"encoding/csv"
"fmt"
"html/template"
"net/http"
"log"
"os"
"os/exec"
"strings"
"syscall"
"bytes"
"context"
"encoding/csv"
"fmt"
"html/template"
"log"
"net/http"
"os"
"os/exec"
"os/signal"
"path"
"strings"
"syscall"
)


type PageVariables struct {
PageTitle string
Metrics []string
VersionInfo map[string]string
PageTitle string
Metrics []string
VersionInfo map[string]string
}


var (
// BuildTime is a time label of the moment when the binary was built
BuildTime = "unset"
Expand All @@ -42,18 +40,16 @@ func getEnv(key, fallback string) string {
return value
}


// healthz is a liveness probe.
func healthz(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
}


// name, index, temperature.gpu, utilization.gpu,
// utilization.memory, memory.total, memory.free, memory.used
func home(w http.ResponseWriter, r *http.Request) {

metricList := []string {
metricList := []string{
"temperature.gpu", "utilization.gpu",
"utilization.memory", "memory.total", "memory.free", "memory.used"}

Expand All @@ -62,7 +58,6 @@ func home(w http.ResponseWriter, r *http.Request) {
verInfo["Commit"] = Commit
verInfo["Release"] = Release


pv := PageVariables{
PageTitle: "Prometheus nVidia GPU Metrics Exporter",
Metrics: metricList,
Expand All @@ -83,63 +78,59 @@ func home(w http.ResponseWriter, r *http.Request) {

}


func metrics(response http.ResponseWriter, request *http.Request) {
out, err := exec.Command(
"nvidia-smi",
"--query-gpu=name,index,temperature.gpu,utilization.gpu,utilization.memory,memory.total,memory.free,memory.used",
"--format=csv,noheader,nounits").Output()

if err != nil {
log.Printf("ERROR: %s\n", err)
return
}

csvReader := csv.NewReader(bytes.NewReader(out))
csvReader.TrimLeadingSpace = true
records, err := csvReader.ReadAll()

if err != nil {
log.Printf("%s\n", err)
return
}

metricList := []string {
"temperature.gpu", "utilization.gpu",
"utilization.memory", "memory.total", "memory.free", "memory.used"}

result := ""
for _, row := range records {
name := fmt.Sprintf("%s[%s]", row[0], row[1])
for idx, value := range row[2:] {
result = fmt.Sprintf("%s%s{gpu=\"%s\"} %s\n", result, metricList[idx], name, value)
}
}

fmt.Fprintf(response, strings.Replace(result, ".", "_", -1))
}
out, err := exec.Command(
"nvidia-smi",
"--query-gpu=name,index,temperature.gpu,utilization.gpu,utilization.memory,memory.total,memory.free,memory.used",
"--format=csv,noheader,nounits").Output()

if err != nil {
log.Printf("ERROR: %s\n", err)
return
}

csvReader := csv.NewReader(bytes.NewReader(out))
csvReader.TrimLeadingSpace = true
records, err := csvReader.ReadAll()

func main() {
log.Print("Starting the service...")
port := getEnv("PORT", "9101");
addr := ":"+port
if err != nil {
log.Printf("%s\n", err)
return
}

log.Print("- PORT set to "+ port +". If environment variable PORT is not set the default is 9101")
metricList := []string{
"temperature.gpu", "utilization.gpu",
"utilization.memory", "memory.total", "memory.free", "memory.used"}

result := ""
for _, row := range records {
name := fmt.Sprintf("%s[%s]", row[0], row[1])
for idx, value := range row[2:] {
result = fmt.Sprintf("%s%s{gpu=\"%s\"} %s\n", result, metricList[idx], name, value)
}
}

fmt.Fprintf(response, strings.Replace(result, ".", "_", -1))
}

func main() {
log.Print("Starting the service...")
port := getEnv("PORT", "9101")
addr := ":" + port

log.Print("- PORT set to " + port + ". If environment variable PORT is not set the default is 9101")

interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM)


srv := &http.Server{
Addr: addr,
}

go func() {
http.HandleFunc("/", home)
http.HandleFunc("/healthz", healthz)
http.HandleFunc("/metrics/", metrics)
http.HandleFunc("/metrics", metrics)
err := srv.ListenAndServe()

if err != nil {
Expand All @@ -148,7 +139,7 @@ func main() {

}()

log.Print("The service is listening on ", port)
log.Print("The service is listening on ", port)

killSignal := <-interrupt
switch killSignal {
Expand All @@ -162,5 +153,4 @@ func main() {
srv.Shutdown(context.Background())
log.Print("Done")


}