1
0
Fork 0
mirror of https://github.com/postmannen/ctrl.git synced 2024-12-14 12:37:31 +00:00

rewrote starting of startMetrics

This commit is contained in:
postmannen 2021-08-03 13:57:29 +02:00
parent d2846007bd
commit e5f995cf62
2 changed files with 20 additions and 9 deletions

View file

@ -1,10 +1,9 @@
package steward
import (
"log"
"fmt"
"net"
"net/http"
"os"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
@ -29,16 +28,21 @@ func newMetrics(hostAndPort string) *metrics {
return &m
}
func (s *server) startMetrics() {
func (s *server) startMetrics() error {
//http.Handle("/metrics", promhttp.Handler())
//http.ListenAndServe(":2112", nil)
n, err := net.Listen("tcp", s.metrics.hostAndPort)
if err != nil {
log.Printf("error: failed to open prometheus listen port: %v\n", err)
os.Exit(1)
return fmt.Errorf("error: startMetrics: failed to open prometheus listen port: %v", err)
}
m := http.NewServeMux()
m.Handle("/metrics", promhttp.Handler())
http.Serve(n, m)
err = http.Serve(n, m)
if err != nil {
return fmt.Errorf("error: startMetrics: failed to start http.Serve: %v", err)
}
return nil
}

View file

@ -245,7 +245,7 @@ func (s *server) Start() {
log.Printf("info: stopping the main server context with ctxCancelFunc()\n")
}()
// Start the error kernel that will do all the error handling
// not done within a process.
// that is not done within a process.
{
s.errorKernel = newErrorKernel()
ctx, cancel := context.WithCancel(s.ctx)
@ -260,7 +260,14 @@ func (s *server) Start() {
}
// Start collecting the metrics
go s.startMetrics()
go func() {
err := s.startMetrics()
if err != nil {
log.Printf("%v\n", err)
os.Exit(1)
}
}()
// Start the checking the input socket for new messages from operator.
go s.readSocket(s.toRingbufferCh)
@ -309,7 +316,7 @@ func (s *server) Start() {
// Adding a safety function here so we can make sure that all processes
// are stopped after a given time if the context cancelation below hangs.
go func() {
time.Sleep(time.Second * 5)
time.Sleep(time.Second * 10)
log.Printf("error: doing a non graceful shutdown of all processes..\n")
os.Exit(1)
}()