Skip to content

Commit

Permalink
wip: removed statsd + hystrix client/instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
mefellows committed Jul 2, 2017
1 parent a2907be commit a945b67
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 68 deletions.
58 changes: 11 additions & 47 deletions examples/hystrix/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ package main
import (
"fmt"
"io/ioutil"
"net"
"net/http"
"os"

"github.com/afex/hystrix-go/hystrix"
"github.com/quipo/statsd"
"github.com/zenazn/goji"
"github.com/zenazn/goji/web"
Expand All @@ -16,62 +14,28 @@ import (
var s *statsd.StatsdClient

func ping(c web.C, w http.ResponseWriter, r *http.Request) {
resultChan := make(chan string, 1)
res, err := http.Get(os.Getenv("API_HOST"))

errChan := hystrix.Go("call_backend", func() error {
res, err := http.Get(os.Getenv("API_HOST"))
if err == nil && res != nil {
if res.StatusCode > 400 {
err = fmt.Errorf("Error: %d", res.StatusCode)
} else {

if err == nil && res != nil {
if res.StatusCode > 400 {
return fmt.Errorf("Error: %d", res.StatusCode)
}
body, err := ioutil.ReadAll(res.Body)

if err == nil {
resultChan <- string(body)
fmt.Fprint(w, string(string(body)))
w.WriteHeader(http.StatusOK)
return
}
}

return err
}, nil)

// Block until we have a result or an error.
select {
case result := <-resultChan:
s.Incr("ok", 1)
fmt.Fprint(w, string(result))
w.WriteHeader(http.StatusOK)
case <-errChan:
s.Incr("err", 1)
w.WriteHeader(http.StatusServiceUnavailable)
}

w.WriteHeader(http.StatusServiceUnavailable)
fmt.Fprint(w, err)
}

func main() {
// Setup Statsd client
fmt.Println("Connecting to Statsd server...")

prefix := "muxy."
statsdclient := statsd.NewStatsdClient(os.Getenv("STATSD_HOST"), prefix)
s = statsdclient
statsdclient.CreateSocket()

if s == nil {
fmt.Println("Could not connect to statsd server, exiting")
os.Exit(1)
}

// Setup hystrix streams
hystrixStreamHandler := hystrix.NewStreamHandler()
hystrixStreamHandler.Start()
go http.ListenAndServe(net.JoinHostPort("", "8181"), hystrixStreamHandler)

hystrix.ConfigureCommand("call_backend", hystrix.CommandConfig{
Timeout: 25,
MaxConcurrentRequests: 100,
ErrorPercentThreshold: 25,
})

goji.Get("/*", ping)
goji.Serve()
}
15 changes: 0 additions & 15 deletions examples/hystrix/backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"fmt"
"net/http"
"os"

"github.com/quipo/statsd"
"github.com/zenazn/goji"
Expand All @@ -13,25 +12,11 @@ import (
var s *statsd.StatsdClient

func ping(c web.C, w http.ResponseWriter, r *http.Request) {
s.Incr("backup", 1)
fmt.Fprintf(w, "Hello from real API")
w.WriteHeader(http.StatusOK)
}

func main() {
// Setup Statsd client
fmt.Println("Connecting to Statsd server...")

prefix := "muxy."
statsdclient := statsd.NewStatsdClient(os.Getenv("STATSD_HOST"), prefix)
s = statsdclient
statsdclient.CreateSocket()

if s == nil {
fmt.Println("Could not connect to statsd server, exiting")
os.Exit(1)
}

goji.Get("/*", ping)
goji.Serve()
}
20 changes: 14 additions & 6 deletions examples/hystrix/muxy/conf/config.local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,18 @@ middleware:
##
## Useful for messing with the HTTP protocol
##
- name: http_tamperer
# - name: http_tamperer
# config:
# response:
# status: 500 # Override HTTP Status code
# body: "my new body" # Override response body
# matching_rules:
# - probability: 5 # Probability tamperer will fire


# Message Delay request/response plugin
- name: delay
config:
response:
status: 500 # Override HTTP Status code
body: "my new body" # Override response body
matching_rules:
- probability: 5 # Probability tamperer will fire
response_delay: 5000
matching_rules:
- probability: 10 # Probability tamperer will fire

0 comments on commit a945b67

Please sign in to comment.