-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathnotify.go
72 lines (60 loc) · 1.9 KB
/
notify.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
package gofaas
import (
"context"
"fmt"
"log"
"os"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/sns"
)
// HandlerAPIGateway is an API Gateway Proxy Request handler function
type HandlerAPIGateway func(context.Context, events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error)
// HandlerCloudWatch is a CloudWatchEvent handler function
type HandlerCloudWatch func(context.Context, events.CloudWatchEvent) error
// HandlerWorker is a Worker handler function
type HandlerWorker func(context.Context, WorkerEvent) error
// NotifyAPIGateway wraps a handler func and sends an SNS notification on error
func NotifyAPIGateway(h HandlerAPIGateway) HandlerAPIGateway {
return func(ctx context.Context, e events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
r, err := h(ctx, e)
notify(ctx, err)
return r, err
}
}
// NotifyCloudWatch wraps a handler func and sends an SNS notification on error
func NotifyCloudWatch(h HandlerCloudWatch) HandlerCloudWatch {
return func(ctx context.Context, e events.CloudWatchEvent) error {
err := h(ctx, e)
notify(ctx, err)
return err
}
}
// NotifyWorker wraps a handler func and sends an SNS notification on error
func NotifyWorker(h HandlerWorker) HandlerWorker {
return func(ctx context.Context, e WorkerEvent) error {
err := h(ctx, e)
notify(ctx, err)
return err
}
}
func notify(ctx context.Context, err error) {
if err == nil {
return
}
subj := fmt.Sprintf("ERROR %s", os.Getenv("AWS_LAMBDA_FUNCTION_NAME"))
msg := fmt.Sprintf("%+v\n", err)
log.Printf("%s %s\n", subj, msg)
topic := os.Getenv("NOTIFICATION_TOPIC")
if topic == "" {
return
}
_, err = SNS.PublishWithContext(ctx, &sns.PublishInput{
Message: aws.String(msg),
Subject: aws.String(subj),
TopicArn: aws.String(topic),
})
if err != nil {
log.Printf("NotifyError SNS Publish error %+v\n", err)
}
}