-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
lambda.go
45 lines (40 loc) · 1.14 KB
/
lambda.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
package tracer
import (
"context"
"log/slog"
"strings"
"github.com/aws/aws-sdk-go-v2/aws/arn"
)
func (t *Tracer) LambdaHandlerFunc(opt *RunOption) func(ctx context.Context, event *ECSTaskEvent) error {
return func(ctx context.Context, event *ECSTaskEvent) error {
slog.Info("event", "payload", event.String())
lastStatus := event.Detail.LastStatus
if lastStatus != "STOPPED" {
return nil
}
cluster := extractClusterName(event.Detail.ClusterArn)
return t.Run(ctx, cluster, extractTaskID(cluster, event.Detail.TaskArn), opt)
}
}
func extractClusterName(clusterArn string) string {
parsed, err := arn.Parse(clusterArn)
if err != nil {
return clusterArn
}
prefix := "cluster/"
if parsed.Service == "ecs" && strings.HasPrefix(parsed.Resource, prefix) {
return strings.TrimPrefix(parsed.Resource, prefix)
}
return clusterArn
}
func extractTaskID(cluster, taskArn string) string {
parsed, err := arn.Parse(taskArn)
if err != nil {
return taskArn
}
prefix := "task/" + cluster + "/"
if parsed.Service == "ecs" && strings.HasPrefix(parsed.Resource, prefix) {
return strings.TrimPrefix(parsed.Resource, prefix)
}
return taskArn
}