-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
143 lines (124 loc) · 3.54 KB
/
main.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"context"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/mackerelio-labs/mackerel-lambda-extension-agent/dispatcher"
"github.com/mackerelio-labs/mackerel-lambda-extension-agent/host/mackerel"
"github.com/mackerelio-labs/mackerel-lambda-extension-agent/lambda/extension"
"github.com/mackerelio-labs/mackerel-lambda-extension-agent/lambda/telemetry"
"github.com/sirupsen/logrus"
)
var Logger *logrus.Entry
func init() {
logLevelStr := strings.ToUpper(os.Getenv("EXT_LOG_LEVEL"))
var logLevel logrus.Level
switch logLevelStr {
case "TRACE":
logLevel = logrus.TraceLevel
case "DEBUG":
logLevel = logrus.DebugLevel
case "INFO":
logLevel = logrus.InfoLevel
case "WARNING":
logLevel = logrus.WarnLevel
case "ERROR":
logLevel = logrus.ErrorLevel
case "FATAL":
logLevel = logrus.FatalLevel
case "PANIC":
logLevel = logrus.PanicLevel
default:
logLevel = logrus.WarnLevel
}
logrus.SetLevel(logLevel)
if logLevel >= logrus.DebugLevel {
logrus.SetReportCaller(true)
}
extName := "mackerel-lambda-extension-agent"
Logger = logrus.WithFields(logrus.Fields{"ext": extName, "pkg": "main"})
mackerel.Logger = logrus.WithFields(logrus.Fields{"ext": extName, "pkg": "host/mackerel"})
extension.Logger = logrus.WithFields(logrus.Fields{"ext": extName, "pkg": "lambda/extension"})
telemetry.Logger = logrus.WithFields(logrus.Fields{"ext": extName, "pkg": "lambda/telemetry"})
dispatcher.Logger = logrus.WithFields(logrus.Fields{"ext": extName, "pkg": "dispatcher"})
}
func main() {
conf, err := GetConfig()
if err != nil {
Logger.Error(err)
return
}
ctx, cancel := context.WithCancel(context.Background())
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGTERM, syscall.SIGINT)
go func() {
s := <-sigs
cancel()
Logger.Info("received signal", s, "terminating")
}()
extCli := extension.NewClient(conf.AWSLambdaConfig.RuntimeApi)
extID, err := extCli.Register(ctx, conf.AWSLambdaConfig.ExtensionName)
if err != nil {
Logger.Error(err)
return
}
tlmListener := telemetry.NewTelemetryApiListener(conf.AWSLambdaConfig.IsSAMLocal)
tlmListenerUri, err := tlmListener.Start()
if err != nil {
Logger.Error(err)
return
}
tlmCli := telemetry.NewClient(conf.AWSLambdaConfig.RuntimeApi)
if _, err = tlmCli.Subscribe(ctx, extID, tlmListenerUri); err != nil {
Logger.Error(err)
return
}
host, err := mackerel.CreateOrGetHost(&mackerel.CreateOrGetHostParam{
MackerelApiKey: conf.MackerelConfig.ApiKey,
RoleFullnames: conf.MackerelConfig.RoleFullnames,
FunctionName: conf.AWSLambdaConfig.FunctionName,
EnvironmentID: conf.AWSLambdaConfig.EnvironmentID,
})
if err != nil {
Logger.Error(err)
return
}
if err := host.CreateGraphDefs(); err != nil {
Logger.Error(err)
return
}
dispatcher := dispatcher.NewDispatcher(host)
ticker := time.NewTicker(60 * time.Second)
defer ticker.Stop()
go func() {
for range ticker.C {
dispatcher.Dispatch(ctx, tlmListener.LogEventsQueue, false)
}
}()
for {
select {
case <-ctx.Done():
return
default:
Logger.Info("Waiting for next event...")
// This is a blocking action
res, err := extCli.NextEvent(ctx)
if err != nil {
Logger.Warning(err)
return
}
// Dispatching log events from previous invocations
dispatcher.Dispatch(ctx, tlmListener.LogEventsQueue, false)
if res.EventType == extension.Shutdown {
// Dispatch all remaining telemetry, handle shutdown
Logger.Info("Shutdown event")
dispatcher.Dispatch(ctx, tlmListener.LogEventsQueue, true)
host.Retire()
return
}
}
}
}