Skip to content

Commit

Permalink
dogstatsd Gauge
Browse files Browse the repository at this point in the history
  • Loading branch information
yuriabrito committed Sep 18, 2017
1 parent 8f668e1 commit 057b3f1
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
2 changes: 1 addition & 1 deletion models/room.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func (r *Room) reportStatus(redisClient interfaces.RedisClient, status string) e
"game": "",
"scheduler": r.SchedulerName,
"status": status,
"gauge": fmt.Sprint(int64(nStatus.Val())),
"gauge": fmt.Sprint(float64(nStatus.Val())),
})
}

Expand Down
26 changes: 24 additions & 2 deletions reporters/dogstatsd/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ package dogstatsd

import (
"fmt"
"strconv"

"github.com/topfreegames/extensions/dogstatsd"
"github.com/topfreegames/maestro/reporters/constants"
)

var handlers = map[string]interface{}{
constants.EventGruNew: GruIncrHandler,
constants.EventGruDelete: GruIncrHandler,
constants.EventGruNew: GruIncrHandler,
constants.EventGruDelete: GruIncrHandler,
constants.EventRoomStatus: GruStatusHandler,
}

// Find looks for a matching handler to a given event
Expand All @@ -33,10 +35,30 @@ func createTags(opts map[string]string) []string {
return tags
}

func createAllowedTags(opts map[string]string, allowed []string) []string {
var tags []string
for _, tag := range allowed {
tags = append(tags, fmt.Sprintf("%s:%s", tag, opts[tag]))
}
return tags
}

// GruIncrHandler calls dogstatsd.Client.Incr with tags formatted as key:value
func GruIncrHandler(c dogstatsd.Client, event string,
opts map[string]string) error {
tags := createTags(opts)
c.Incr(event, tags, 1)
return nil
}

// GruStatusHandler calls dogstatsd.Client.Incr with tags formatted as key:value
func GruStatusHandler(c dogstatsd.Client, event string,
opts map[string]string) error {
tags := createAllowedTags(opts, []string{"game", "scheduler"})
gauge, err := strconv.ParseFloat(opts["gauge"], 64)
if err != nil {
return err
}
c.Gauge(fmt.Sprintf("gru.%s", opts["status"]), gauge, tags, 1)
return nil
}
18 changes: 18 additions & 0 deletions reporters/dogstatsd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,29 @@ var _ = Describe("DogStatsD", func() {
Expect(c.Counts["gru.new"]).To(Equal(int64(1)))
})

It("GruStatusHandler should send Gauge of given status", func() {
Expect(c.Gauges["gru.terminating"]).To(Equal(float64(0)))
opts["status"] = "terminating"
opts["gauge"] = "42"
handlers.GruStatusHandler(c, "gru.status", opts)
Expect(c.Gauges["gru.terminating"]).To(Equal(float64(42)))
})

It("Report(gru.new, opts) should Incr gru.new", func() {
d := reporters.NewDogStatsDFromClient(c)
Expect(c.Counts["gru.new"]).To(Equal(int64(0)))
err := d.Report("gru.new", opts)
Expect(err).NotTo(HaveOccurred())
Expect(c.Counts["gru.new"]).To(Equal(int64(1)))
})

It("Report(gru.status, opts) should send Gauge of given status", func() {
d := reporters.NewDogStatsDFromClient(c)
Expect(c.Gauges["gru.creating"]).To(Equal(float64(0)))
opts["status"] = "creating"
opts["gauge"] = "5"
err := d.Report("gru.status", opts)
Expect(err).NotTo(HaveOccurred())
Expect(c.Gauges["gru.creating"]).To(Equal(float64(5)))
})
})

0 comments on commit 057b3f1

Please sign in to comment.