-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
main.go
309 lines (268 loc) · 7.75 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
// Some parts of this file are taken from : https://github.com/aws-samples/aws-lambda-extensions/tree/main/go-example-extension
package main
import (
"bytes"
"compress/gzip"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/DataDog/agent-payload/v5/gogen"
"github.com/DataDog/datadog-agent/pkg/trace/pb"
)
const extensionName = "recorder-extension" // extension name has to match the filename
var extensionClient = NewClient(os.Getenv("AWS_LAMBDA_RUNTIME_API"))
var nbHitMetrics = 0
var nbReport = 0
var nbHitTraces = 0
var outputSketches = make([]gogen.SketchPayload_Sketch, 0)
var outputLogs = make([]jsonServerlessPayload, 0)
var outputTraces = make([]*pb.TracerPayload, 0)
var hasBeenOutput = false
func main() {
ctx, cancel := context.WithCancel(context.Background())
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGTERM, syscall.SIGINT)
go func() {
<-sigs
cancel()
}()
err := extensionClient.Register(ctx, extensionName)
if err != nil {
panic(err)
}
// port 8080 is used by the Lambda Invoke API
port := "3333"
Start(port)
// Will block until shutdown event is received or cancelled via the context.
processEvents(ctx)
}
func processEvents(ctx context.Context) {
for {
select {
case <-ctx.Done():
return
default:
res, err := extensionClient.NextEvent(ctx)
if err != nil {
return
}
if res.EventType == Shutdown {
time.Sleep(1900 * time.Millisecond)
return
}
}
}
}
// JSON representation of a message.
type jsonServerlessPayload struct {
Message jsonServerlessMessage `json:"message"`
Status string `json:"status"`
Timestamp int64 `json:"timestamp"`
Hostname string `json:"hostname"`
Service string `json:"service"`
Source string `json:"ddsource"`
Tags string `json:"ddtags"`
}
type jsonServerlessMessage struct {
Message string `json:"message"`
Lambda *jsonServerlessLambda `json:"lambda,omitempty"`
}
type jsonServerlessLambda struct {
ARN string `json:"arn"`
RequestID string `json:"request_id,omitempty"`
}
// NextEventResponse is the response for /event/next
type NextEventResponse struct {
EventType EventType `json:"eventType"`
}
// EventType represents the type of events recieved from /event/next
type EventType string
const (
// Shutdown is a shutdown event for the environment
Shutdown EventType = "SHUTDOWN"
extensionNameHeader = "Lambda-Extension-Name"
extensionIdentiferHeader = "Lambda-Extension-Identifier"
)
// Client is a simple client for the Lambda Extensions API
type Client struct {
baseURL string
httpClient *http.Client
extensionID string
}
// NewClient returns a Lambda Extensions API client
func NewClient(awsLambdaRuntimeAPI string) *Client {
baseURL := fmt.Sprintf("http://%s/2020-01-01/extension", awsLambdaRuntimeAPI)
return &Client{
baseURL: baseURL,
httpClient: &http.Client{},
}
}
// Register will register the extension with the Extensions API
func (e *Client) Register(ctx context.Context, filename string) error {
const action = "/register"
url := e.baseURL + action
reqBody, err := json.Marshal(map[string]interface{}{
"events": []EventType{Shutdown},
})
if err != nil {
return err
}
httpReq, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(reqBody))
if err != nil {
return err
}
httpReq.Header.Set(extensionNameHeader, filename)
httpRes, err := e.httpClient.Do(httpReq)
if err != nil {
return err
}
if httpRes.StatusCode != 200 {
return fmt.Errorf("request failed with status %s", httpRes.Status)
}
defer httpRes.Body.Close()
e.extensionID = httpRes.Header.Get(extensionIdentiferHeader)
return nil
}
// NextEvent blocks while long polling for the next lambda invoke or shutdown
func (e *Client) NextEvent(ctx context.Context) (*NextEventResponse, error) {
const action = "/event/next"
url := e.baseURL + action
httpReq, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return nil, err
}
httpReq.Header.Set(extensionIdentiferHeader, e.extensionID)
httpRes, err := e.httpClient.Do(httpReq)
if err != nil {
return nil, err
}
if httpRes.StatusCode != 200 {
return nil, fmt.Errorf("request failed with status %s", httpRes.Status)
}
defer httpRes.Body.Close()
body, err := io.ReadAll(httpRes.Body)
if err != nil {
return nil, err
}
res := NextEventResponse{}
err = json.Unmarshal(body, &res)
if err != nil {
return nil, err
}
return &res, nil
}
// Start is starting the http server to receive logs, traces and metrics
func Start(port string) {
go startHTTPServer(port)
}
func startHTTPServer(port string) {
http.HandleFunc("/api/beta/sketches", func(w http.ResponseWriter, r *http.Request) {
nbHitMetrics++
body, err := io.ReadAll(r.Body)
if err != nil {
fmt.Printf("Error while reading HTTP request body: %s \n", err)
return
}
pl := new(gogen.SketchPayload)
if err := pl.Unmarshal(body); err != nil {
fmt.Printf("Error while unmarshalling sketches %s \n", err)
return
}
outputSketches = append(outputSketches, pl.Sketches...)
if nbHitMetrics == 3 {
// two calls + shutdown
jsonSketch, err := json.Marshal(outputSketches)
if err != nil {
fmt.Printf("Error while JSON encoding the sketch")
}
fmt.Printf("%s%s%s\n", "BEGINMETRIC", string(jsonSketch), "ENDMETRIC")
}
})
http.HandleFunc("/api/v2/logs", func(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
if err != nil {
return
}
decompressedBody, err := decompress(body)
if err != nil {
return
}
var messages []jsonServerlessPayload
if err := json.Unmarshal(decompressedBody, &messages); err != nil {
return
}
for _, log := range messages {
msg := log.Message.Message
if strings.Contains(msg, "BEGINMETRIC") || strings.Contains(msg, "BEGINLOG") || strings.Contains(msg, "BEGINTRACE") {
continue
}
if strings.HasPrefix(msg, "REPORT RequestId:") {
nbReport++
}
outputLogs = append(outputLogs, log)
}
if nbReport == 2 && !hasBeenOutput {
jsonLogs, err := json.Marshal(outputLogs)
if err != nil {
fmt.Printf("Error while JSON encoding the logs")
}
fmt.Printf("%s%s%s\n", "BEGINLOG", string(jsonLogs), "ENDLOG")
hasBeenOutput = true // make sure not re re-output the logs
}
})
http.HandleFunc("/api/v0.2/traces", func(w http.ResponseWriter, r *http.Request) {
nbHitTraces++
body, err := io.ReadAll(r.Body)
if err != nil {
return
}
decompressedBody, err := decompress(body)
if err != nil {
return
}
pl := new(pb.AgentPayload)
if err := pl.Unmarshal(decompressedBody); err != nil {
fmt.Printf("Error while unmarshalling traces %s \n", err)
return
}
outputTraces = append(outputTraces, pl.TracerPayloads...)
if nbHitTraces == 2 {
jsonLogs, err := json.Marshal(outputTraces)
if err != nil {
fmt.Printf("Error while JSON encoding the traces")
}
fmt.Printf("%s%s%s\n", "BEGINTRACE", string(jsonLogs), "ENDTRACE")
}
})
http.HandleFunc("/api/v1/series", func(w http.ResponseWriter, r *http.Request) {
})
http.HandleFunc("/api/v1/check_run", func(w http.ResponseWriter, r *http.Request) {
})
err := http.ListenAndServe(":"+port, nil)
if err != nil {
panic(err)
}
}
func decompress(payload []byte) ([]byte, error) {
reader, err := gzip.NewReader(bytes.NewReader(payload))
if err != nil {
return nil, err
}
var buffer bytes.Buffer
_, err = buffer.ReadFrom(reader)
if err != nil {
return nil, err
}
return buffer.Bytes(), nil
}