|
| 1 | +// Unless explicitly stated otherwise all files in this repository are licensed |
| 2 | +// under the Apache License Version 2.0. |
| 3 | +// This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 4 | +// Copyright 2016-present Datadog, Inc. |
| 5 | + |
| 6 | +// Some parts of this file are taken from : https://github.com/aws-samples/aws-lambda-extensions/tree/main/go-example-extension |
| 7 | + |
| 8 | +package main |
| 9 | + |
| 10 | +import ( |
| 11 | + "bytes" |
| 12 | + "compress/gzip" |
| 13 | + "context" |
| 14 | + "encoding/json" |
| 15 | + "fmt" |
| 16 | + "io/ioutil" |
| 17 | + "net/http" |
| 18 | + "os" |
| 19 | + "os/signal" |
| 20 | + "sort" |
| 21 | + "strings" |
| 22 | + "syscall" |
| 23 | + "time" |
| 24 | + |
| 25 | + "github.com/DataDog/agent-payload/gogen" |
| 26 | +) |
| 27 | + |
| 28 | +const extensionName = "recorder-extension" // extension name has to match the filename |
| 29 | +var extensionClient = NewClient(os.Getenv("AWS_LAMBDA_RUNTIME_API")) |
| 30 | + |
| 31 | +func main() { |
| 32 | + ctx, cancel := context.WithCancel(context.Background()) |
| 33 | + |
| 34 | + sigs := make(chan os.Signal, 1) |
| 35 | + signal.Notify(sigs, syscall.SIGTERM, syscall.SIGINT) |
| 36 | + go func() { |
| 37 | + <-sigs |
| 38 | + cancel() |
| 39 | + }() |
| 40 | + |
| 41 | + err := extensionClient.Register(ctx, extensionName) |
| 42 | + if err != nil { |
| 43 | + panic(err) |
| 44 | + } |
| 45 | + |
| 46 | + // port 8080 is used by the Lambda Invoke API |
| 47 | + port := "3333" |
| 48 | + Start(port) |
| 49 | + |
| 50 | + // Will block until shutdown event is received or cancelled via the context. |
| 51 | + processEvents(ctx) |
| 52 | +} |
| 53 | + |
| 54 | +func processEvents(ctx context.Context) { |
| 55 | + for { |
| 56 | + select { |
| 57 | + case <-ctx.Done(): |
| 58 | + return |
| 59 | + default: |
| 60 | + res, err := extensionClient.NextEvent(ctx) |
| 61 | + if err != nil { |
| 62 | + return |
| 63 | + } |
| 64 | + if res.EventType == Shutdown { |
| 65 | + time.Sleep(1900 * time.Millisecond) |
| 66 | + return |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +// JSON representation of a message. |
| 73 | +type jsonServerlessPayload struct { |
| 74 | + Message jsonServerlessMessage `json:"message"` |
| 75 | + Status string `json:"status"` |
| 76 | + Timestamp int64 `json:"timestamp"` |
| 77 | + Hostname string `json:"hostname"` |
| 78 | + Service string `json:"service"` |
| 79 | + Source string `json:"ddsource"` |
| 80 | + Tags string `json:"ddtags"` |
| 81 | +} |
| 82 | + |
| 83 | +type jsonServerlessMessage struct { |
| 84 | + Message string `json:"message"` |
| 85 | + Lambda *jsonServerlessLambda `json:"lambda,omitempty"` |
| 86 | +} |
| 87 | + |
| 88 | +type jsonServerlessLambda struct { |
| 89 | + ARN string `json:"arn"` |
| 90 | + RequestID string `json:"request_id,omitempty"` |
| 91 | +} |
| 92 | + |
| 93 | +// NextEventResponse is the response for /event/next |
| 94 | +type NextEventResponse struct { |
| 95 | + EventType EventType `json:"eventType"` |
| 96 | +} |
| 97 | + |
| 98 | +// EventType represents the type of events recieved from /event/next |
| 99 | +type EventType string |
| 100 | + |
| 101 | +const ( |
| 102 | + // Shutdown is a shutdown event for the environment |
| 103 | + Shutdown EventType = "SHUTDOWN" |
| 104 | + |
| 105 | + extensionNameHeader = "Lambda-Extension-Name" |
| 106 | + extensionIdentiferHeader = "Lambda-Extension-Identifier" |
| 107 | +) |
| 108 | + |
| 109 | +// Client is a simple client for the Lambda Extensions API |
| 110 | +type Client struct { |
| 111 | + baseURL string |
| 112 | + httpClient *http.Client |
| 113 | + extensionID string |
| 114 | +} |
| 115 | + |
| 116 | +// NewClient returns a Lambda Extensions API client |
| 117 | +func NewClient(awsLambdaRuntimeAPI string) *Client { |
| 118 | + baseURL := fmt.Sprintf("http://%s/2020-01-01/extension", awsLambdaRuntimeAPI) |
| 119 | + return &Client{ |
| 120 | + baseURL: baseURL, |
| 121 | + httpClient: &http.Client{}, |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +// Register will register the extension with the Extensions API |
| 126 | +func (e *Client) Register(ctx context.Context, filename string) error { |
| 127 | + const action = "/register" |
| 128 | + url := e.baseURL + action |
| 129 | + |
| 130 | + reqBody, err := json.Marshal(map[string]interface{}{ |
| 131 | + "events": []EventType{Shutdown}, |
| 132 | + }) |
| 133 | + if err != nil { |
| 134 | + return err |
| 135 | + } |
| 136 | + httpReq, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(reqBody)) |
| 137 | + if err != nil { |
| 138 | + return err |
| 139 | + } |
| 140 | + httpReq.Header.Set(extensionNameHeader, filename) |
| 141 | + httpRes, err := e.httpClient.Do(httpReq) |
| 142 | + if err != nil { |
| 143 | + return err |
| 144 | + } |
| 145 | + if httpRes.StatusCode != 200 { |
| 146 | + return fmt.Errorf("request failed with status %s", httpRes.Status) |
| 147 | + } |
| 148 | + defer httpRes.Body.Close() |
| 149 | + e.extensionID = httpRes.Header.Get(extensionIdentiferHeader) |
| 150 | + return nil |
| 151 | +} |
| 152 | + |
| 153 | +// NextEvent blocks while long polling for the next lambda invoke or shutdown |
| 154 | +func (e *Client) NextEvent(ctx context.Context) (*NextEventResponse, error) { |
| 155 | + const action = "/event/next" |
| 156 | + url := e.baseURL + action |
| 157 | + |
| 158 | + httpReq, err := http.NewRequestWithContext(ctx, "GET", url, nil) |
| 159 | + if err != nil { |
| 160 | + return nil, err |
| 161 | + } |
| 162 | + httpReq.Header.Set(extensionIdentiferHeader, e.extensionID) |
| 163 | + httpRes, err := e.httpClient.Do(httpReq) |
| 164 | + if err != nil { |
| 165 | + return nil, err |
| 166 | + } |
| 167 | + if httpRes.StatusCode != 200 { |
| 168 | + return nil, fmt.Errorf("request failed with status %s", httpRes.Status) |
| 169 | + } |
| 170 | + defer httpRes.Body.Close() |
| 171 | + body, err := ioutil.ReadAll(httpRes.Body) |
| 172 | + if err != nil { |
| 173 | + return nil, err |
| 174 | + } |
| 175 | + res := NextEventResponse{} |
| 176 | + err = json.Unmarshal(body, &res) |
| 177 | + if err != nil { |
| 178 | + return nil, err |
| 179 | + } |
| 180 | + return &res, nil |
| 181 | +} |
| 182 | + |
| 183 | +// Start is starting the http server to receive logs, traces and metrics |
| 184 | +func Start(port string) { |
| 185 | + go startHTTPServer(port) |
| 186 | +} |
| 187 | + |
| 188 | +func startHTTPServer(port string) { |
| 189 | + http.HandleFunc("/api/beta/sketches", func(w http.ResponseWriter, r *http.Request) { |
| 190 | + body, err := ioutil.ReadAll(r.Body) |
| 191 | + if err != nil { |
| 192 | + fmt.Printf("Error while reading HTTP request body: %s \n", err) |
| 193 | + return |
| 194 | + } |
| 195 | + pl := new(gogen.SketchPayload) |
| 196 | + if err := pl.Unmarshal(body); err != nil { |
| 197 | + fmt.Printf("Error while unmarshalling sketches %s \n", err) |
| 198 | + return |
| 199 | + } |
| 200 | + |
| 201 | + for _, sketch := range pl.Sketches { |
| 202 | + jsonSketch, err := json.Marshal(sketch) |
| 203 | + if err != nil { |
| 204 | + fmt.Printf("Error while JSON encoding the sketch") |
| 205 | + } |
| 206 | + fmt.Printf("[sketch] %s \n", string(jsonSketch)) |
| 207 | + } |
| 208 | + }) |
| 209 | + |
| 210 | + http.HandleFunc("/v1/input", func(w http.ResponseWriter, r *http.Request) { |
| 211 | + body, err := ioutil.ReadAll(r.Body) |
| 212 | + if err != nil { |
| 213 | + return |
| 214 | + } |
| 215 | + decompressedBody, err := decompress(body) |
| 216 | + if err != nil { |
| 217 | + return |
| 218 | + } |
| 219 | + var messages []jsonServerlessPayload |
| 220 | + if err := json.Unmarshal(decompressedBody, &messages); err != nil { |
| 221 | + return |
| 222 | + } |
| 223 | + for _, log := range messages { |
| 224 | + sortedTags := strings.Split(log.Tags, ",") |
| 225 | + sort.Strings(sortedTags) |
| 226 | + log.Tags = strings.Join(sortedTags, ",") |
| 227 | + jsonLog, err := json.Marshal(log) |
| 228 | + if err != nil { |
| 229 | + fmt.Printf("Error while JSON encoding the Log") |
| 230 | + } |
| 231 | + stringJsonLog := string(jsonLog) |
| 232 | + // if we log an unwanted log, it will be available in the next log api payload -> infinite loop |
| 233 | + if !strings.Contains(stringJsonLog, "[log]") && !strings.Contains(stringJsonLog, "[metric]") { |
| 234 | + fmt.Printf("[log] %s\n", stringJsonLog) |
| 235 | + } |
| 236 | + } |
| 237 | + }) |
| 238 | + |
| 239 | + http.HandleFunc("/api/v1/series", func(w http.ResponseWriter, r *http.Request) { |
| 240 | + }) |
| 241 | + |
| 242 | + http.HandleFunc("/api/v1/check_run", func(w http.ResponseWriter, r *http.Request) { |
| 243 | + }) |
| 244 | + |
| 245 | + err := http.ListenAndServe(":"+port, nil) |
| 246 | + if err != nil { |
| 247 | + panic(err) |
| 248 | + } |
| 249 | +} |
| 250 | + |
| 251 | +func decompress(payload []byte) ([]byte, error) { |
| 252 | + reader, err := gzip.NewReader(bytes.NewReader(payload)) |
| 253 | + if err != nil { |
| 254 | + return nil, err |
| 255 | + } |
| 256 | + |
| 257 | + var buffer bytes.Buffer |
| 258 | + _, err = buffer.ReadFrom(reader) |
| 259 | + if err != nil { |
| 260 | + return nil, err |
| 261 | + } |
| 262 | + |
| 263 | + return buffer.Bytes(), nil |
| 264 | +} |
0 commit comments