Skip to content

Commit

Permalink
consolidate utility endpoints (#164)
Browse files Browse the repository at this point in the history
* consolidate utility endpoints

profiling & heartbeat can be served together as utilities

* cleanup
  • Loading branch information
rocketbitz authored Dec 8, 2018
1 parent 0c2dd33 commit b241cd7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
10 changes: 3 additions & 7 deletions cmd/start/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,9 @@ func executeStart(cmd *cobra.Command, args []string) error {
InitializeTriggers()
RunBgWorkers()

// Start heartbeat endpoint.
log.Info("launching heartbeat service...")
go frontend.Heartbeat(utils.InstanceConfig.ListenPort)

// Start profiling endpoint.
log.Info("launching profiling service...")
go frontend.Profile(utils.InstanceConfig.ListenPort)
// Start utility endpoints.
log.Info("launching utility service...")
go frontend.Utilities(utils.InstanceConfig.ListenPort)

log.Info("enabling query access...")
atomic.StoreUint32(&frontend.Queryable, 1)
Expand Down
20 changes: 17 additions & 3 deletions frontend/heartbeat.go → frontend/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package frontend
import (
"encoding/json"
"net/http"
"net/http/pprof"
"sync/atomic"
"time"

Expand All @@ -23,12 +24,25 @@ func init() {
Queryable = uint32(0)
}

func Heartbeat(address string) {
http.HandleFunc("/heartbeat", handler)
func Utilities(address string) {
// heartbeat
http.HandleFunc("/heartbeat", heartbeat)

// profiling
http.HandleFunc("/pprof/", pprof.Index)
http.HandleFunc("/pprof/cmdline", pprof.Cmdline)
http.HandleFunc("/pprof/profile", pprof.Profile)
http.HandleFunc("/pprof/symbol", pprof.Symbol)
http.HandleFunc("/pprof/trace", pprof.Trace)
http.Handle("/pprof/heap", pprof.Handler("heap"))
http.Handle("/pprof/goroutine", pprof.Handler("goroutine"))
http.Handle("/pprof/threadcreate", pprof.Handler("threadcreate"))
http.Handle("/pprof/block", pprof.Handler("block"))

http.ListenAndServe(address, nil)
}

func handler(rw http.ResponseWriter, r *http.Request) {
func heartbeat(rw http.ResponseWriter, r *http.Request) {
uptime := time.Since(utils.InstanceConfig.StartTime).String()
queryable := atomic.LoadUint32(&Queryable)
if queryable > 0 {
Expand Down
4 changes: 2 additions & 2 deletions frontend/heartbeat_test.go → frontend/utilities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (s *HeartbeatTestSuite) TestHandler(c *C) {
switch key {
case "Success":
atomic.StoreUint32(&Queryable, uint32(1))
handler(val.Recorder, nil)
heartbeat(val.Recorder, nil)
hm := HeartbeatMessage{}
err := json.NewDecoder(val.Recorder.Body).Decode(&hm)
if err != nil {
Expand All @@ -51,7 +51,7 @@ func (s *HeartbeatTestSuite) TestHandler(c *C) {
c.Assert(val.Recorder.Code, Equals, http.StatusOK)
case "Failure":
atomic.StoreUint32(&Queryable, uint32(0))
handler(val.Recorder, nil)
heartbeat(val.Recorder, nil)
hm := HeartbeatMessage{}
err := json.NewDecoder(val.Recorder.Body).Decode(&hm)
if err != nil {
Expand Down

0 comments on commit b241cd7

Please sign in to comment.