-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from boinkor-net/prometheus
Serve prometheus metrics
- Loading branch information
Showing
6 changed files
with
109 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
sha256-s/BwC3UXRO9jffeM91Xm8HojVCpuLkwFuwsumkCt2SQ= | ||
sha256-Q3uOv8iG6qGlSlM27wmeXxtD20KnWROqe4tTFS0c9cI= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package hoopsnake | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"os" | ||
"time" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promauto" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
"tailscale.com/tsnet" | ||
) | ||
|
||
func (s *TailnetSSH) setupPrometheus(srv *tsnet.Server) error { | ||
if s.prometheusAddr == "" { | ||
return nil | ||
} | ||
mux := http.NewServeMux() | ||
mux.Handle("/metrics", promhttp.Handler()) | ||
listener, err := srv.Listen("tcp", s.prometheusAddr) | ||
if err != nil { | ||
return fmt.Errorf("could not listen on prometheus address %v: %w", s.prometheusAddr, err) | ||
} | ||
go func() { | ||
server := http.Server{ | ||
Handler: mux, | ||
ReadHeaderTimeout: 1 * time.Second, | ||
} | ||
log.Printf("Failed to listen on prometheus address: %v", server.Serve(listener)) | ||
os.Exit(20) | ||
}() | ||
|
||
v4, v6 := srv.TailscaleIPs() | ||
promauto.NewGaugeFunc(prometheus.GaugeOpts{ | ||
Name: "hoopsnake_running", | ||
Help: "A counter set to 1.0 if hoopsnake is running.", | ||
ConstLabels: prometheus.Labels{ | ||
"ipv4": v4.String(), | ||
"ipv6": v6.String(), | ||
"hostname": srv.Hostname, | ||
}, | ||
}, func() float64 { return 1.0 }) | ||
listenAddr := s.prometheusAddr | ||
if listenAddr[0] == ':' { | ||
listenAddr = v4.String() + listenAddr | ||
} | ||
|
||
log.Printf("Serving prometheus metrics at http://%s/metrics", listenAddr) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters