Last active
December 31, 2018 22:26
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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