Skip to content

Instantly share code, notes, and snippets.

@xeoncross
Last active December 31, 2018 22:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xeoncross/d2a40cc9d34226f9a686f41c6bb944d7 to your computer and use it in GitHub Desktop.
Save xeoncross/d2a40cc9d34226f9a686f41c6bb944d7 to your computer and use it in GitHub Desktop.
Simple example of counting requests and submitting to datadog, statsd, or some other logging system.
package main
import (
"fmt"
"net/http"
"sync/atomic"
"time"
)
var requestsPerMinute uint64
func main() {
// Datadog example
// c, err := statsd.New("127.0.0.1:8125")
// if err != nil {
// log.Fatal(err)
// }
// // prefix every metric with the app name
// c.Namespace = "app.http.server."
go func() {
for {
select {
case <-time.After(time.Minute):
num := atomic.SwapUint64(&requestsPerMinute, 0)
fmt.Printf("Served %5d requests\n", num)
// Report to datadog
// c.Gauge("requests", float64(num), nil, 1)
}
}
}()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
atomic.AddUint64(&requestsPerMinute, 1)
w.Write([]byte("Hello World"))
})
http.ListenAndServe(":8080", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment