Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add prometheus endpoint and metrics #45

Merged
merged 1 commit into from
Jan 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Godeps/Readme

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (
"github.com/buoyantio/slow_cooker/ring"
"github.com/buoyantio/slow_cooker/window"
"github.com/codahale/hdrhistogram"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

// DayInMs 1 day in milliseconds
Expand Down Expand Up @@ -101,6 +103,7 @@ func sendRequest(
} else {
if sz, err := io.CopyBuffer(ioutil.Discard, response.Body, bodyBuffer); err == nil {
response.Body.Close()

received <- &MeasuredResponse{
uint64(sz),
response.StatusCode,
Expand Down Expand Up @@ -182,6 +185,32 @@ func loadData(data string) []byte {
return requestData
}

var (
promRequests = prometheus.NewCounter(prometheus.CounterOpts{
Name: "requests",
Help: "Number of requests",
})

promSuccesses = prometheus.NewCounter(prometheus.CounterOpts{
Name: "successes",
Help: "Number of successful requests",
})

promLatencyHistogram = prometheus.NewHistogram(prometheus.HistogramOpts{
Name: "latency_ms",
Help: "RPC latency distributions in milliseconds.",
// 50 exponential buckets ranging from 0.5 ms to 3 minutes
// TODO: make this tunable
Buckets: prometheus.ExponentialBuckets(0.5, 1.3, 50),
})
)

func registerMetrics() {
prometheus.MustRegister(promRequests)
prometheus.MustRegister(promSuccesses)
prometheus.MustRegister(promLatencyHistogram)
}

func main() {
qps := flag.Int("qps", 1, "QPS to send to backends per request thread")
concurrency := flag.Int("concurrency", 1, "Number of request threads")
Expand All @@ -198,6 +227,7 @@ func main() {
headers := make(headerSet)
flag.Var(&headers, "header", "HTTP request header. (can be repeated.)")
data := flag.String("data", "", "HTTP request data")
metricAddr := flag.String("metric-addr", "", "port to serve metrics on")

flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s <url> [flags]\n", path.Base(os.Args[0]))
Expand Down Expand Up @@ -286,6 +316,14 @@ func main() {
interrupted := make(chan os.Signal, 2)
signal.Notify(interrupted, syscall.SIGINT)

if *metricAddr != "" {
registerMetrics()
go func() {
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(*metricAddr, nil)
}()
}

for {
select {
// If we get a SIGINT, then start the shutdown process.
Expand Down Expand Up @@ -357,13 +395,16 @@ func main() {
}
case managedResp := <-received:
count++
promRequests.Inc()
if managedResp.err != nil {
fmt.Fprintln(os.Stderr, managedResp.err)
failed++
} else {
size += managedResp.sz
if managedResp.code >= 200 && managedResp.code < 500 {
good++
promSuccesses.Inc()
promLatencyHistogram.Observe(float64(managedResp.latency))
} else {
bad++
}
Expand Down
20 changes: 20 additions & 0 deletions vendor/github.com/beorn7/perks/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading