Skip to content
This repository has been archived by the owner on Apr 2, 2020. It is now read-only.

Commit

Permalink
Merge pull request #20 from booster-proj/issue/19
Browse files Browse the repository at this point in the history
Add metrics.json endpoint. Add configurable prometheusPort config field
  • Loading branch information
dmorn authored Dec 24, 2018
2 parents 1a6d9d4 + 29497cd commit 44203aa
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 29 deletions.
9 changes: 0 additions & 9 deletions booster.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,6 @@ import (
"upspin.io/log"
)

type Config struct {
Version string `json:"version"`
Commit string `json:"commit"`
BuildTime string `json:"build_time"`

ProxyPort int `json:"proxy_port"`
ProxyProto string `json:"proxy_proto"`
}

// New returns an instance of a booster dialer.
func New(b *core.Balancer) core.Dialer {
return &dialer{b}
Expand Down
7 changes: 4 additions & 3 deletions cmd/booster/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ var (
rawProto = flag.String("proto", "socks5", "Proxy protocol used. Available protocols: http, socks5.")

// API configuration
apiPort = flag.Int("api-port", 7764, "API server listening port")
apiPort = flag.Int("api-port", 7764, "API server listening port")
promPort = flag.Int("prometheus-port", 9090, "Port of a local prometheus server. Used for forwarding")

// Log configuration
verbose = flag.Bool("verbose", false, "If set, makes the logger print also debug messages")
Expand All @@ -81,12 +82,13 @@ func main() {
log.Fatal(err)
}

config := booster.Config{
remote.StaticConf = remote.Config{
Version: version,
Commit: commit,
BuildTime: buildTime,
ProxyPort: *pPort,
ProxyProto: *rawProto,
PromPort: *promPort,
}

b := new(core.Balancer)
Expand All @@ -99,7 +101,6 @@ func main() {
d := booster.New(b)

router := remote.NewRouter()
router.Config = config
router.Store = rs
router.MetricsProvider = mb
router.SetupRoutes()
Expand Down
60 changes: 46 additions & 14 deletions remote/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,28 @@ package remote

import (
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"net/url"

"github.com/booster-proj/booster"
"github.com/booster-proj/booster/store"
"github.com/gorilla/mux"
"upspin.io/log"
)

func makeHealthCheckHandler(config booster.Config) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")

json.NewEncoder(w).Encode(struct {
Alive bool `json:"alive"`
booster.Config
}{
Alive: true,
Config: config,
})
}
func healthCheckHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")

json.NewEncoder(w).Encode(struct {
Alive bool `json:"alive"`
Config
}{
Alive: true,
Config: StaticConf,
})
}

func makeSourcesHandler(s *store.SourceStore) func(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -83,6 +83,8 @@ func makeBlockHandler(s *store.SourceStore) func(w http.ResponseWriter, r *http.
},
}

w.Header().Set("Content-Type", "application/json")

if r.Method == "POST" {
// Add a reason if available in the body.
var payload struct {
Expand All @@ -104,3 +106,33 @@ func makeBlockHandler(s *store.SourceStore) func(w http.ResponseWriter, r *http.
w.WriteHeader(http.StatusOK)
}
}

func metricsForwardHandler(w http.ResponseWriter, r *http.Request) {
oHost, _, err := net.SplitHostPort(r.Host)
if err != nil {
http.Error(w, err.Error(), http.StatusServiceUnavailable)
return
}

URL, _ := url.Parse(r.URL.String())
URL.Scheme = "http"
URL.Host = fmt.Sprintf("%s:%d", oHost, StaticConf.PromPort)
URL.Path = "api/v1/query"

req, err := http.NewRequest(r.Method, URL.String(), r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusServiceUnavailable)
return
}

req.Header = r.Header

resp, err := http.DefaultClient.Do(req)
if err != nil {
http.Error(w, err.Error(), http.StatusServiceUnavailable)
return
}

w.WriteHeader(http.StatusOK)
io.Copy(w, resp.Body)
}
17 changes: 14 additions & 3 deletions remote/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,25 @@ package remote
import (
"net/http"

"github.com/booster-proj/booster"
"github.com/booster-proj/booster/store"
"github.com/gorilla/mux"
)

type Config struct {
Version string `json:"version"`
Commit string `json:"commit"`
BuildTime string `json:"build_time"`

ProxyPort int `json:"proxy_port"`
ProxyProto string `json:"proxy_proto"`
PromPort int `json:"-"`
}

var StaticConf Config = Config{}

type Router struct {
r *mux.Router

Config booster.Config
Store *store.SourceStore
MetricsProvider http.Handler
}
Expand All @@ -43,7 +53,7 @@ func NewRouter() *Router {
// properly.
func (r *Router) SetupRoutes() {
router := r.r
router.HandleFunc("/health.json", makeHealthCheckHandler(r.Config))
router.HandleFunc("/health.json", healthCheckHandler)
if store := r.Store; store != nil {
router.HandleFunc("/sources.json", makeSourcesHandler(store))
router.HandleFunc("/sources/{name}/block.json", makeBlockHandler(store)).Methods("POST", "DELETE")
Expand All @@ -52,6 +62,7 @@ func (r *Router) SetupRoutes() {
if handler := r.MetricsProvider; handler != nil {
router.Handle("/metrics", handler)
}
router.HandleFunc("/metrics.json", metricsForwardHandler)
router.Use(loggingMiddleware)
}

Expand Down

0 comments on commit 44203aa

Please sign in to comment.