-
Notifications
You must be signed in to change notification settings - Fork 460
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
208 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package worker | ||
|
||
import ( | ||
"strconv" | ||
"time" | ||
|
||
"github.com/topfreegames/pitaya/logger" | ||
"github.com/topfreegames/pitaya/metrics" | ||
|
||
workers "github.com/topfreegames/go-workers" | ||
) | ||
|
||
// Report sends periodic of worker reports | ||
func Report(reporters []metrics.Reporter, period time.Duration) { | ||
for { | ||
time.Sleep(period) | ||
|
||
workerStats := workers.GetStats() | ||
for _, r := range reporters { | ||
reportJobsRetry(r, workerStats.Retries) | ||
reportQueueSizes(r, workerStats.Enqueued) | ||
reportJobsTotal(r, workerStats.Failed, workerStats.Processed) | ||
} | ||
} | ||
} | ||
|
||
func reportJobsRetry(r metrics.Reporter, retries int64) { | ||
err := r.ReportGauge(metrics.WorkerJobsRetry, map[string]string{}, float64(retries)) | ||
checkReportErr(metrics.WorkerJobsRetry, err) | ||
} | ||
|
||
func reportQueueSizes(r metrics.Reporter, queues map[string]string) { | ||
for queue, size := range queues { | ||
tags := map[string]string{"queue": queue} | ||
sizeFlt, err := strconv.ParseFloat(size, 64) | ||
if err != nil { | ||
logger.Log.Errorf("queue size is not int: queue=%s size=%s", queue, size) | ||
continue | ||
} | ||
|
||
err = r.ReportGauge(metrics.WorkerQueueSize, tags, sizeFlt) | ||
checkReportErr(metrics.WorkerQueueSize, err) | ||
} | ||
} | ||
|
||
func reportJobsTotal(r metrics.Reporter, failed, processed int) { | ||
err := r.ReportCount(metrics.WorkerJobsTotal, map[string]string{ | ||
"status": "failed", | ||
}, float64(failed)) | ||
checkReportErr(metrics.WorkerJobsTotal, err) | ||
|
||
err = r.ReportCount(metrics.WorkerJobsTotal, map[string]string{ | ||
"status": "ok", | ||
}, float64(processed)) | ||
checkReportErr(metrics.WorkerJobsTotal, err) | ||
} | ||
|
||
func checkReportErr(metric string, err error) { | ||
if err != nil { | ||
logger.Log.Errorf("failed to report to %s: %q\n", metric, err) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package worker | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/topfreegames/pitaya/metrics" | ||
"github.com/topfreegames/pitaya/metrics/mocks" | ||
) | ||
|
||
func TestReportJobsRetry(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("success", func(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
mockReporter := mocks.NewMockReporter(ctrl) | ||
mockReporter.EXPECT().ReportGauge( | ||
metrics.WorkerJobsRetry, | ||
map[string]string{}, | ||
float64(10)) | ||
|
||
reportJobsRetry(mockReporter, 10) | ||
}) | ||
|
||
t.Run("error", func(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
mockReporter := mocks.NewMockReporter(ctrl) | ||
mockReporter.EXPECT().ReportGauge( | ||
metrics.WorkerJobsRetry, | ||
map[string]string{}, | ||
float64(10)).Return(errors.New("err")) | ||
|
||
reportJobsRetry(mockReporter, 10) | ||
}) | ||
} | ||
|
||
func TestReportQueueSizes(t *testing.T) { | ||
t.Parallel() | ||
|
||
var ( | ||
queue1 = "queuename1" | ||
queue2 = "queuename2" | ||
) | ||
|
||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
mockReporter := mocks.NewMockReporter(ctrl) | ||
mockReporter.EXPECT().ReportGauge( | ||
metrics.WorkerQueueSize, | ||
map[string]string{"queue": queue1}, | ||
float64(10)) | ||
mockReporter.EXPECT().ReportGauge( | ||
metrics.WorkerQueueSize, | ||
map[string]string{"queue": queue2}, | ||
float64(20)) | ||
|
||
reportQueueSizes(mockReporter, map[string]string{ | ||
queue1: "10", | ||
queue2: "20", | ||
}) | ||
} | ||
|
||
func TestReportJobsTotal(t *testing.T) { | ||
t.Parallel() | ||
|
||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
mockReporter := mocks.NewMockReporter(ctrl) | ||
mockReporter.EXPECT().ReportCount( | ||
metrics.WorkerJobsTotal, | ||
map[string]string{"status": "failed"}, | ||
float64(10)) | ||
|
||
mockReporter.EXPECT().ReportCount( | ||
metrics.WorkerJobsTotal, | ||
map[string]string{"status": "ok"}, | ||
float64(20)) | ||
|
||
reportJobsTotal(mockReporter, 10, 20) | ||
} |
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