Browse Source

Add statsd support

master
Teran McKinney 2 years ago
parent
commit
6ad3bf8863
  1. 5
      go.mod
  2. 2
      go.sum
  3. 40
      rantbin.go

5
go.mod

@ -0,0 +1,5 @@
module github.com/teran-mckinney/rantbin
go 1.12
require gopkg.in/alexcesaro/statsd.v2 v2.0.0

2
go.sum

@ -0,0 +1,2 @@
gopkg.in/alexcesaro/statsd.v2 v2.0.0 h1:FXkZSCZIH17vLCO5sO2UucTHsH9pc+17F6pl3JVCwMc=
gopkg.in/alexcesaro/statsd.v2 v2.0.0/go.mod h1:i0ubccKGzBVNBpdGV5MocxyA/XlLUJzA7SLonnE4drU=

40
rantbin.go

@ -16,6 +16,8 @@ import (
"log"
"net/http"
"os"
"gopkg.in/alexcesaro/statsd.v2"
)
// 8197 works out to be the same thing as 4096 y's. 4096 regular letters will be larger, but roughly gives us 4KiB max file size or less.
@ -61,9 +63,7 @@ func pageTooLong(page string) error {
return err
}
func tooManyFeedbacks() error {
// Make sure we don't fill up disk with too many feedbacks.
// Return error string if too many feedbacks, nil if not.
func countFeedbacks() (int, error) {
var err error
var feedbacks []os.FileInfo
@ -72,13 +72,18 @@ func tooManyFeedbacks() error {
log.Print("EXCEPTION")
log.Print(err)
err = errors.New("Issue totaling up feedbacks. Please contact the administrator.")
return err
return 9000, err
}
return len(feedbacks), err
}
number_of_feedbacks := len(feedbacks)
if number_of_feedbacks >= MAX_FILES {
func tooManyFeedbacks(numberOfFeedbacks int) error {
// Make sure we don't fill up disk with too many feedbacks.
// Return error string if too many feedbacks, nil if not.
var err error
if numberOfFeedbacks >= MAX_FILES {
log.Print("EXCEPTION")
log.Printf("%d feedbacks detected, max is %d", number_of_feedbacks, MAX_FILES)
log.Printf("%d feedbacks detected, max is %d", numberOfFeedbacks, MAX_FILES)
err = errors.New("Too many feedbacks. Please contact the administrator.")
}
return err
@ -86,7 +91,20 @@ func tooManyFeedbacks() error {
func main() {
var err error
/* Statsd statistics. This works fine with or without. */
s, err := statsd.New(statsd.Prefix("rantbin"))
if err != nil {
log.Print("rantbin connection to statsd failed. This is not a problem unless you want statsd.")
// This should be non-fatal.
log.Print(err)
} else {
log.Print("rantbin connected to statsd.")
}
defer s.Close()
http.HandleFunc("/feedback", func(w http.ResponseWriter, r *http.Request) {
s.Increment("feedback.hit")
defer s.NewTiming().Send("feedback.time")
// https://stackoverflow.com/questions/28282370/is-it-advisable-to-further-limit-the-size-of-forms-when-using-golang
r.Body = http.MaxBytesReader(w, r.Body, MAX_POST)
err = r.ParseForm()
@ -110,7 +128,12 @@ func main() {
return
}
if err = tooManyFeedbacks(); err != nil {
numberOfFeedbacks, err := countFeedbacks()
if err != nil {
http.Error(w, err.Error(), 500)
}
if err = tooManyFeedbacks(numberOfFeedbacks); err != nil {
http.Error(w, err.Error(), 500)
return
}
@ -141,6 +164,7 @@ func main() {
log.Printf("write: %s\n", path)
fmt.Fprint(w, "Feedback received.")
s.Gauge("feedback_count", numberOfFeedbacks+1)
return
})
if len(os.Args) != 2 {

Loading…
Cancel
Save