-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
API server and Prometheus Metrics (#10)
* feat(api): introduce a new api server * feat(cmd): wire and start the API * feat(notifier) add a metrics middleware * feat(election): add leader election metrics * feat(helm): expose api on prometheus-elector, add readiness and liveness * feat(cmd): optionally export go runtime metrics * feat(example): demonstrate scrapping agent elector * chore(README): prometheus metrics are already here * fix(worflows): turn off cache for lint job
- Loading branch information
Showing
14 changed files
with
382 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,7 +54,6 @@ jobs: | |
with: | ||
go-version-file: './go.mod' | ||
check-latest: true | ||
cache: true | ||
- name: Install Ko | ||
uses: imjasonh/[email protected] | ||
- name: Release a New Version | ||
|
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,60 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
"k8s.io/klog/v2" | ||
) | ||
|
||
type Server struct { | ||
httpSrv http.Server | ||
|
||
shutdownGraceDelay time.Duration | ||
} | ||
|
||
func NewServer(listenAddr string, shutdownGraceDelay time.Duration, metricsRegistry prometheus.Gatherer) *Server { | ||
var mux http.ServeMux | ||
|
||
mux.HandleFunc("/healthz", func(rw http.ResponseWriter, r *http.Request) { rw.WriteHeader(http.StatusOK) }) | ||
mux.Handle("/metrics", promhttp.HandlerFor( | ||
metricsRegistry, | ||
promhttp.HandlerOpts{EnableOpenMetrics: true}, | ||
)) | ||
|
||
return &Server{ | ||
shutdownGraceDelay: shutdownGraceDelay, | ||
httpSrv: http.Server{ | ||
Addr: listenAddr, | ||
Handler: &mux, | ||
}, | ||
} | ||
} | ||
|
||
func (s *Server) Serve(ctx context.Context) error { | ||
shutdownDone := make(chan error) | ||
|
||
go func() { | ||
<-ctx.Done() | ||
|
||
shutdownCtx, cancel := context.WithTimeout(context.Background(), s.shutdownGraceDelay) | ||
defer cancel() | ||
|
||
err := s.httpSrv.Shutdown(shutdownCtx) | ||
if err != nil { | ||
klog.Info("Server shutdown reported an error, forcing close") | ||
err = s.httpSrv.Close() | ||
} | ||
|
||
shutdownDone <- err | ||
}() | ||
|
||
if err := s.httpSrv.ListenAndServe(); err != http.ErrServerClosed { | ||
return err | ||
} | ||
|
||
return <-shutdownDone | ||
} |
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,58 @@ | ||
package api_test | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"testing" | ||
"time" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/jlevesy/prometheus-elector/api" | ||
) | ||
|
||
func TestServer_Serve(t *testing.T) { | ||
var ( | ||
ctx, cancel = context.WithCancel(context.Background()) | ||
|
||
srv = api.NewServer( | ||
":63549", | ||
15*time.Second, | ||
prometheus.NewRegistry(), | ||
) | ||
|
||
srvDone = make(chan struct{}) | ||
) | ||
|
||
go func() { | ||
err := srv.Serve(ctx) | ||
require.NoError(t, err) | ||
|
||
close(srvDone) | ||
}() | ||
|
||
var attempt = 0 | ||
|
||
for { | ||
time.Sleep(200 * time.Millisecond) | ||
attempt += 1 | ||
|
||
if attempt == 5 { | ||
t.Fatal("Exausted max retries getting the /healthz endpoint") | ||
} | ||
|
||
resp, err := http.Get("http://localhost:63549/healthz") | ||
if err != nil { | ||
continue | ||
} | ||
if resp.StatusCode != http.StatusOK { | ||
continue | ||
} | ||
|
||
break | ||
} | ||
|
||
cancel() | ||
<-srvDone | ||
} |
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
Oops, something went wrong.