Skip to content

Commit

Permalink
Sending statsd per game
Browse files Browse the repository at this point in the history
  • Loading branch information
Guilherme Souza committed Sep 25, 2017
1 parent 7861e9e commit ad4be2a
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 60 deletions.
6 changes: 3 additions & 3 deletions extensions/apns_message_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func (a *APNSMessageHandler) sendMessage(message interfaces.KafkaMessage) error
}
return nil
}
statsReporterHandleNotificationSent(a.StatsReporters)
statsReporterHandleNotificationSent(a.StatsReporters, message.Game, "apns")
a.PushQueue.Push(n.DeviceToken, h, payload)
if n.Metadata == nil {
n.Metadata = map[string]interface{}{}
Expand Down Expand Up @@ -309,7 +309,7 @@ func (a *APNSMessageHandler) handleAPNSResponse(res push.Response) error {
}
reason := pushError.Reason
pErr := errors.NewPushError(a.mapErrorReason(reason), pushError.Error())
statsReporterHandleNotificationFailure(a.StatsReporters, pErr)
statsReporterHandleNotificationFailure(a.StatsReporters, parsedTopic.Game, "apns", pErr)

err = pErr
switch reason {
Expand Down Expand Up @@ -361,7 +361,7 @@ func (a *APNSMessageHandler) handleAPNSResponse(res push.Response) error {
apnsResMutex.Lock()
a.successesReceived++
apnsResMutex.Unlock()
statsReporterHandleNotificationSuccess(a.StatsReporters)
statsReporterHandleNotificationSuccess(a.StatsReporters, parsedTopic.Game, "apns")
return nil
}

Expand Down
20 changes: 9 additions & 11 deletions extensions/apns_message_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,17 +462,15 @@ var _ = Describe("APNS Message Handler", func() {
It("should call HandleNotificationSent upon message sent to queue", func() {
Expect(handler).NotTo(BeNil())
Expect(handler.StatsReporters).To(Equal(statsClients))

handler.sendMessage(interfaces.KafkaMessage{
Topic: "push-game_apns",
Value: []byte(`{ "aps" : { "alert" : "Hello HTTP/2" } }`),
})
handler.sendMessage(interfaces.KafkaMessage{
kafkaMessage := interfaces.KafkaMessage{
Game: "game",
Topic: "push-game_apns",
Value: []byte(`{ "aps" : { "alert" : "Hello HTTP/2" } }`),
})
}
handler.sendMessage(kafkaMessage)
handler.sendMessage(kafkaMessage)

Expect(mockStatsDClient.Count["sent"]).To(Equal(2))
Expect(mockStatsDClient.Count["apns.game.sent"]).To(Equal(2))
})

It("should call HandleNotificationSuccess upon message response received", func() {
Expand All @@ -486,7 +484,7 @@ var _ = Describe("APNS Message Handler", func() {
handler.handleAPNSResponse(res)
handler.handleAPNSResponse(res)

Expect(mockStatsDClient.Count["ack"]).To(Equal(2))
Expect(mockStatsDClient.Count["apns..ack"]).To(Equal(2))
})

It("should call HandleNotificationFailure upon message response received", func() {
Expand All @@ -504,8 +502,8 @@ var _ = Describe("APNS Message Handler", func() {
handler.handleAPNSResponse(res)
handler.handleAPNSResponse(res)

Expect(mockStatsDClient.Count["failed"]).To(Equal(2))
Expect(mockStatsDClient.Count["missing-device-token"]).To(Equal(2))
Expect(mockStatsDClient.Count["apns..failed"]).To(Equal(2))
Expect(mockStatsDClient.Count["apns..missing-device-token"]).To(Equal(2))
})
})

Expand Down
12 changes: 6 additions & 6 deletions extensions/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,20 @@ func sendToFeedbackReporters(feedbackReporters []interfaces.FeedbackReporter, re
return nil
}

func statsReporterHandleNotificationSent(statsReporters []interfaces.StatsReporter) {
func statsReporterHandleNotificationSent(statsReporters []interfaces.StatsReporter, game string, platform string) {
for _, statsReporter := range statsReporters {
statsReporter.HandleNotificationSent()
statsReporter.HandleNotificationSent(game, platform)
}
}

func statsReporterHandleNotificationSuccess(statsReporters []interfaces.StatsReporter) {
func statsReporterHandleNotificationSuccess(statsReporters []interfaces.StatsReporter, game string, platform string) {
for _, statsReporter := range statsReporters {
statsReporter.HandleNotificationSuccess()
statsReporter.HandleNotificationSuccess(game, platform)
}
}

func statsReporterHandleNotificationFailure(statsReporters []interfaces.StatsReporter, err *errors.PushError) {
func statsReporterHandleNotificationFailure(statsReporters []interfaces.StatsReporter, game string, platform string, err *errors.PushError) {
for _, statsReporter := range statsReporters {
statsReporter.HandleNotificationFailure(err)
statsReporter.HandleNotificationFailure(game, platform, err)
}
}
6 changes: 3 additions & 3 deletions extensions/gcm_message_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func (g *GCMMessageHandler) handleGCMResponse(cm gcm.CCSMessage) error {
g.failuresReceived++
gcmResMutex.Unlock()
pErr := errors.NewPushError(strings.ToLower(cm.Error), cm.ErrorDescription)
statsReporterHandleNotificationFailure(g.StatsReporters, pErr)
statsReporterHandleNotificationFailure(g.StatsReporters, parsedTopic.Game, "gcm", pErr)

err = pErr
switch cm.Error {
Expand Down Expand Up @@ -279,7 +279,7 @@ func (g *GCMMessageHandler) handleGCMResponse(cm gcm.CCSMessage) error {
gcmResMutex.Lock()
g.successesReceived++
gcmResMutex.Unlock()
statsReporterHandleNotificationSuccess(g.StatsReporters)
statsReporterHandleNotificationSuccess(g.StatsReporters, parsedTopic.Game, "gcm")

return nil
}
Expand Down Expand Up @@ -340,7 +340,7 @@ func (g *GCMMessageHandler) sendMessage(message interfaces.KafkaMessage) error {
g.inflightMessagesMetadataLock.Unlock()
}

statsReporterHandleNotificationSent(g.StatsReporters)
statsReporterHandleNotificationSent(g.StatsReporters, message.Game, "gcm")
g.sentMessages++
l.WithFields(log.Fields{
"messageID": messageID,
Expand Down
22 changes: 9 additions & 13 deletions extensions/gcm_message_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,28 +482,24 @@ var _ = Describe("GCM Message Handler", func() {
}
msgBytes, err := json.Marshal(msg)
Expect(err).NotTo(HaveOccurred())

err = handler.sendMessage(interfaces.KafkaMessage{
kafkaMessage := interfaces.KafkaMessage{
Game: "game",
Topic: "push-game_gcm",
Value: msgBytes,
})
}
err = handler.sendMessage(kafkaMessage)
Expect(err).NotTo(HaveOccurred())

err = handler.sendMessage(interfaces.KafkaMessage{
Topic: "push-game_gcm",
Value: msgBytes,
})
err = handler.sendMessage(kafkaMessage)
Expect(err).NotTo(HaveOccurred())

Expect(mockStatsDClient.Count["sent"]).To(Equal(2))
Expect(mockStatsDClient.Count["gcm.game.sent"]).To(Equal(2))
})

It("should call HandleNotificationSuccess upon message response received", func() {
res := gcm.CCSMessage{}
handler.handleGCMResponse(res)
handler.handleGCMResponse(res)

Expect(mockStatsDClient.Count["ack"]).To(Equal(2))
Expect(mockStatsDClient.Count["gcm..ack"]).To(Equal(2))
})

It("should call HandleNotificationFailure upon message response received", func() {
Expand All @@ -513,8 +509,8 @@ var _ = Describe("GCM Message Handler", func() {
handler.handleGCMResponse(res)
handler.handleGCMResponse(res)

Expect(mockStatsDClient.Count["failed"]).To(Equal(2))
Expect(mockStatsDClient.Count["device_unregistered"]).To(Equal(2))
Expect(mockStatsDClient.Count["gcm..failed"]).To(Equal(2))
Expect(mockStatsDClient.Count["gcm..device_unregistered"]).To(Equal(2))
})
})

Expand Down
16 changes: 8 additions & 8 deletions extensions/statsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ package extensions
import (
"time"

"github.com/sirupsen/logrus"
"github.com/alexcesaro/statsd"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/topfreegames/pusher/errors"
"github.com/topfreegames/pusher/interfaces"
Expand Down Expand Up @@ -89,19 +89,19 @@ func (s *StatsD) configure(client interfaces.StatsDClient) error {
}

//HandleNotificationSent stores notification count in StatsD
func (s *StatsD) HandleNotificationSent() {
s.Client.Increment("sent")
func (s *StatsD) HandleNotificationSent(game string, platform string) {
s.Client.Increment(platform + "." + game + "." + "sent")
}

//HandleNotificationSuccess stores notifications success in StatsD
func (s *StatsD) HandleNotificationSuccess() {
s.Client.Increment("ack")
func (s *StatsD) HandleNotificationSuccess(game string, platform string) {
s.Client.Increment(platform + "." + game + "." + "ack")
}

//HandleNotificationFailure stores each type of failure
func (s *StatsD) HandleNotificationFailure(err *errors.PushError) {
s.Client.Increment("failed")
s.Client.Increment(err.Key)
func (s *StatsD) HandleNotificationFailure(game string, platform string, err *errors.PushError) {
s.Client.Increment(platform + "." + game + "." + "failed")
s.Client.Increment(platform + "." + game + "." + err.Key)
}

//ReportGoStats reports go stats in statsd
Expand Down
24 changes: 11 additions & 13 deletions extensions/statsd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
package extensions

import (
"github.com/sirupsen/logrus/hooks/test"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/sirupsen/logrus/hooks/test"
"github.com/spf13/viper"
"github.com/topfreegames/pusher/errors"
"github.com/topfreegames/pusher/mocks"
Expand All @@ -51,10 +51,9 @@ var _ = Describe("StatsD Extension", func() {
Expect(err).NotTo(HaveOccurred())
defer statsd.Cleanup()

statsd.HandleNotificationSent()
statsd.HandleNotificationSent()

Expect(mockClient.Count["sent"]).To(Equal(2))
statsd.HandleNotificationSent("game", "apns")
statsd.HandleNotificationSent("game", "apns")
Expect(mockClient.Count["apns.game.sent"]).To(Equal(2))
})
})

Expand All @@ -64,10 +63,9 @@ var _ = Describe("StatsD Extension", func() {
Expect(err).NotTo(HaveOccurred())
defer statsd.Cleanup()

statsd.HandleNotificationSuccess()
statsd.HandleNotificationSuccess()

Expect(mockClient.Count["ack"]).To(Equal(2))
statsd.HandleNotificationSuccess("game", "apns")
statsd.HandleNotificationSuccess("game", "apns")
Expect(mockClient.Count["apns.game.ack"]).To(Equal(2))
})
})

Expand Down Expand Up @@ -96,11 +94,11 @@ var _ = Describe("StatsD Extension", func() {

pErr := errors.NewPushError("some-key", "some description")

statsd.HandleNotificationFailure(pErr)
statsd.HandleNotificationFailure(pErr)
statsd.HandleNotificationFailure("game", "apns", pErr)
statsd.HandleNotificationFailure("game", "apns", pErr)

Expect(mockClient.Count["failed"]).To(Equal(2))
Expect(mockClient.Count["some-key"]).To(Equal(2))
Expect(mockClient.Count["apns.game.failed"]).To(Equal(2))
Expect(mockClient.Count["apns.game.some-key"]).To(Equal(2))
})
})
})
Expand Down
6 changes: 3 additions & 3 deletions interfaces/stats_reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import "github.com/topfreegames/pusher/errors"

// StatsReporter interface for making stats reporters pluggable easily
type StatsReporter interface {
HandleNotificationSent()
HandleNotificationSuccess()
HandleNotificationFailure(*errors.PushError)
HandleNotificationSent(game string, platform string)
HandleNotificationSuccess(game string, platform string)
HandleNotificationFailure(game string, platform string, err *errors.PushError)
ReportGoStats(numGoRoutines int, allocatedAndNotFreed, heapObjects, nextGCBytes, pauseGCNano uint64)
}

0 comments on commit ad4be2a

Please sign in to comment.