Skip to content

Commit

Permalink
Merge pull request #61 from topfreegames/fix/latency-metric
Browse files Browse the repository at this point in the history
Fix labels on metric and add timing metric
  • Loading branch information
gmurayama authored Jun 3, 2024
2 parents 50e410d + 9a8eee7 commit a5fa115
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
9 changes: 9 additions & 0 deletions e2e/fcm_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ func (s *FcmE2ETestSuite) TestSimpleNotification() {
Timing("send_notification_latency", gomock.Any(), gomock.Any(), gomock.Any()).
Return(nil)

statsdClientMock.EXPECT().
Timing("firebase_latency", gomock.Any(), gomock.Any(), gomock.Any()).
Return(nil)

err = producer.Produce(&kafka.Message{
TopicPartition: kafka.TopicPartition{
Topic: &topic,
Expand Down Expand Up @@ -186,6 +190,11 @@ func (s *FcmE2ETestSuite) TestMultipleNotifications() {
Times(notificationsToSend).
Return(nil)

statsdClientMock.EXPECT().
Timing("firebase_latency", gomock.Any(), gomock.Any(), gomock.Any()).
Times(notificationsToSend).
Return(nil)

for i := 0; i < notificationsToSend; i++ {
err = producer.Produce(&kafka.Message{
TopicPartition: kafka.TopicPartition{
Expand Down
15 changes: 14 additions & 1 deletion extensions/datadog_statsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (s *StatsD) ReportGoStats(
func (s *StatsD) ReportSendNotificationLatency(latencyMs time.Duration, game string, platform string, labels ...string) {
metricLabels := []string{fmt.Sprintf("platform:%s", platform), fmt.Sprintf("game:%s", game)}
for i := 0; i < len(labels); i += 2 {
metricLabels = append(labels, fmt.Sprintf("%s:%s", labels[i], labels[i+1]))
metricLabels = append(metricLabels, fmt.Sprintf("%s:%s", labels[i], labels[i+1]))
}
s.Client.Timing(
"send_notification_latency",
Expand All @@ -142,6 +142,19 @@ func (s *StatsD) ReportSendNotificationLatency(latencyMs time.Duration, game str
)
}

func (s *StatsD) ReportFirebaseLatency(latencyMs time.Duration, game string, labels ...string) {
metricLabels := []string{fmt.Sprintf("game:%s", game)}
for i := 0; i < len(labels); i += 2 {
metricLabels = append(metricLabels, fmt.Sprintf("%s:%s", labels[i], labels[i+1]))
}
s.Client.Timing(
"firebase_latency",
latencyMs,
metricLabels,
1,
)
}

// ReportMetricGauge reports a metric as a Gauge with hostname, game and platform
// as tags
func (s *StatsD) ReportMetricGauge(
Expand Down
9 changes: 9 additions & 0 deletions extensions/handler/message_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@ func (h *messageHandler) sendPush(ctx context.Context, msg interfaces.Message) {
h.sendPushConcurrencyControl <- l
}()

before := time.Now()
err := h.client.SendPush(ctx, msg)
h.reportFirebaseLatency(time.Since(before))

h.handleNotificationSent()

h.responsesChannel <- struct {
Expand Down Expand Up @@ -228,6 +231,12 @@ func (h *messageHandler) reportLatency(latency time.Duration) {
}
}

func (h *messageHandler) reportFirebaseLatency(latency time.Duration) {
for _, statsReporter := range h.statsReporters {
statsReporter.ReportFirebaseLatency(latency, h.app)
}
}

func translateToPushError(err error) *pushErrors.PushError {
if pusherError, ok := err.(*pushErrors.PushError); ok {
return pusherError
Expand Down
1 change: 1 addition & 0 deletions interfaces/stats_reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ type StatsReporter interface {
ReportMetricGauge(metric string, value float64, game string, platform string)
ReportMetricCount(metric string, value int64, game string, platform string)
ReportSendNotificationLatency(latencyMs time.Duration, game string, platform string, labels ...string)
ReportFirebaseLatency(latencyMs time.Duration, game string, labels ...string)
}

0 comments on commit a5fa115

Please sign in to comment.