Skip to content

Commit

Permalink
InternetProxy config for bosun
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathieu Payeur Levallois committed Feb 12, 2016
1 parent 6e5c0a1 commit 3098748
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 27 deletions.
3 changes: 3 additions & 0 deletions cmd/bosun/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type Conf struct {
Quiet bool
NoSleep bool
ShortURLKey string
InternetProxy string
MinGroupSize int

TSDBHost string // OpenTSDB relay and query destination: ny-devtsdb04:4242
Expand Down Expand Up @@ -549,6 +550,8 @@ func (c *Conf) loadGlobal(p *parse.PairNode) {
}
case "shortURLKey":
c.ShortURLKey = v
case "internetProxy":
c.InternetProxy = v
case "ledisDir":
c.LedisDir = v
case "redisHost":
Expand Down
9 changes: 8 additions & 1 deletion cmd/bosun/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,17 @@ func main() {
c.TSDBHost = tsdbHost.Host
}
}
var shortenProxy *url.URL
if c.InternetProxy != "" {
shortenProxy, err = url.Parse(c.InternetProxy)
if err != nil {
slog.Fatalf("InternetProxy error: %s", err)
}
}
if *flagQuiet {
c.Quiet = true
}
go func() { slog.Fatal(web.Listen(c.HTTPListen, *flagDev, c.TSDBHost)) }()
go func() { slog.Fatal(web.Listen(c.HTTPListen, *flagDev, c.TSDBHost, shortenProxy)) }()
go func() {
if !*flagNoChecks {
sched.Run()
Expand Down
68 changes: 42 additions & 26 deletions cmd/bosun/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"html/template"
"io"
"io/ioutil"
"net"
"net/http"
"net/http/httputil"
"net/url"
Expand Down Expand Up @@ -58,7 +59,7 @@ func init() {
"HTTP response codes from the backend server for request relayed through Bosun.")
}

func Listen(listenAddr string, devMode bool, tsdbHost string) error {
func Listen(listenAddr string, devMode bool, tsdbHost string, internetProxy *url.URL) error {
if devMode {
slog.Infoln("using local web assets")
}
Expand Down Expand Up @@ -106,7 +107,7 @@ func Listen(listenAddr string, devMode bool, tsdbHost string) error {
router.Handle("/api/metric/{tagk}", JSON(MetricsByTagKey))
router.Handle("/api/metric/{tagk}/{tagv}", JSON(MetricsByTagPair))
router.Handle("/api/rule", JSON(Rule))
router.HandleFunc("/api/shorten", Shorten)
router.HandleFunc("/api/shorten", ProxyShorten(internetProxy))
router.Handle("/api/silence/clear", JSON(SilenceClear))
router.Handle("/api/silence/get", JSON(SilenceGet))
router.Handle("/api/silence/set", JSON(SilenceSet))
Expand Down Expand Up @@ -267,31 +268,46 @@ func JSON(h func(miniprofiler.Timer, http.ResponseWriter, *http.Request) (interf
})
}

func Shorten(w http.ResponseWriter, r *http.Request) {
u := url.URL{
Scheme: "https",
Host: "www.googleapis.com",
Path: "/urlshortener/v1/url",
}
if schedule.Conf.ShortURLKey != "" {
u.RawQuery = "key=" + schedule.Conf.ShortURLKey
}
j, err := json.Marshal(struct {
LongURL string `json:"longUrl"`
}{
r.Referer(),
})
if err != nil {
serveError(w, err)
return
}
req, err := http.Post(u.String(), "application/json", bytes.NewBuffer(j))
if err != nil {
serveError(w, err)
return
func ProxyShorten(proxyURL *url.URL) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
u := url.URL{
Scheme: "https",
Host: "www.googleapis.com",
Path: "/urlshortener/v1/url",
}
if schedule.Conf.ShortURLKey != "" {
u.RawQuery = "key=" + schedule.Conf.ShortURLKey
}
j, err := json.Marshal(struct {
LongURL string `json:"longUrl"`
}{
r.Referer(),
})
if err != nil {
serveError(w, err)
return
}

transport := &http.Transport{
Dial: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).Dial,
TLSHandshakeTimeout: 10 * time.Second,
}
if proxyURL != nil {
transport.Proxy = http.ProxyURL(proxyURL)
}
c := http.Client{Transport: transport}

req, err := c.Post(u.String(), "application/json", bytes.NewBuffer(j))
if err != nil {
serveError(w, err)
return
}
io.Copy(w, req.Body)
req.Body.Close()
}
io.Copy(w, req.Body)
req.Body.Close()
}

type Health struct {
Expand Down

0 comments on commit 3098748

Please sign in to comment.