-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreport.go
100 lines (81 loc) · 2.41 KB
/
report.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package sqshandler
import (
"encoding/json"
"fmt"
"github.com/aws/aws-lambda-go/events"
)
/*
Report defines a struct used for serializing and printing an
execution report of a batch of messages.
# BatchSize
A count of how many [events.SQSMessage] were contained in the [events.SQSEvent]
# Success, Skip
A count of how many messages had a processing [Status] of said values.
# Retry, Failure
A count of how many messages had a processing [Status] of said values, including
any individual errors reported by either the [Handler] or [Worker].
# HandlerErrors
A collection of errors that occurred during message handling (changing
visibility, sending to a DLQ, etc.).
*/
type Report struct {
BatchSize int `json:"batchSize"`
Success int `json:"success,omitempty"`
Skip int `json:"skip,omitempty"`
Retry *retryFailureReport `json:"retry,omitempty"`
Failure *retryFailureReport `json:"failure,omitempty"`
HandlerErrors []errorReport `json:"handlerErrors,omitempty"`
}
type retryFailureReport struct {
Count int `json:"count"`
Errors []errorReport `json:"errors"`
}
type errorReport struct {
MessageId string `json:"messageId"`
Error string `json:"error"`
}
func printReport(event *events.SQSEvent, results map[Status][]result, hErrs []handlerError) {
report := Report{
BatchSize: len(event.Records),
Success: len(results[Success]),
Skip: len(results[Skip]),
HandlerErrors: errorToReport(hErrs),
}
if results[Failure] != nil {
report.Failure = &retryFailureReport{
Count: len(results[Failure]),
Errors: resultToReport(results[Failure]),
}
}
if results[Retry] != nil {
report.Retry = &retryFailureReport{
Count: len(results[Retry]),
Errors: resultToReport(results[Retry]),
}
}
if json, err := json.Marshal(report); err == nil {
fmt.Printf("%s\n", json)
} else {
fmt.Printf("unable to print report: %v", err)
}
}
func resultToReport(results []result) []errorReport {
conv := make([]errorReport, len(results))
for i, r := range results {
conv[i] = errorReport{
MessageId: r.message.MessageId,
Error: r.err.Error(),
}
}
return conv
}
func errorToReport(errors []handlerError) []errorReport {
conv := make([]errorReport, len(errors))
for i, r := range errors {
conv[i] = errorReport{
MessageId: r.MessageId,
Error: r.Error.Error(),
}
}
return conv
}