|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const express = require('express'); |
| 4 | +const fetch = require('node-fetch'); |
| 5 | +const bodyParser = require('body-parser'); |
| 6 | +const protobuf = require('protobufjs'); |
| 7 | + |
| 8 | +const BASE_URL = `http://${process.env.AWS_LAMBDA_RUNTIME_API}/2020-01-01/extension`; |
| 9 | +const SHUTDOWN_EVENT = 'SHUTDOWN'; |
| 10 | + |
| 11 | +const handleShutdown = async () => { |
| 12 | + console.log('SHUTDOWN received in recorder'); |
| 13 | + //making sure that the server won't be closed when the extension will send data |
| 14 | + await new Promise(r => setTimeout(r, 1000)); |
| 15 | + console.log('SHUTDOWN end'); |
| 16 | +} |
| 17 | + |
| 18 | +async function register() { |
| 19 | + const res = await fetch(`${BASE_URL}/register`, { |
| 20 | + method: 'post', |
| 21 | + body: JSON.stringify({ |
| 22 | + 'events': [ |
| 23 | + SHUTDOWN_EVENT |
| 24 | + ], |
| 25 | + }), |
| 26 | + headers: { |
| 27 | + 'Content-Type': 'application/json', |
| 28 | + 'Lambda-Extension-Name': 'a_recorder', |
| 29 | + } |
| 30 | + }); |
| 31 | + |
| 32 | + if (!res.ok) { |
| 33 | + console.error('register failed', await res.text()); |
| 34 | + } |
| 35 | + return res.headers.get('lambda-extension-identifier'); |
| 36 | +} |
| 37 | + |
| 38 | +async function next(extensionId) { |
| 39 | + const res = await fetch(`${BASE_URL}/event/next`, { |
| 40 | + method: 'get', |
| 41 | + headers: { |
| 42 | + 'Content-Type': 'application/json', |
| 43 | + 'Lambda-Extension-Identifier': extensionId, |
| 44 | + } |
| 45 | + }); |
| 46 | + |
| 47 | + if (!res.ok) { |
| 48 | + console.error('next failed', await res.text()); |
| 49 | + return null; |
| 50 | + } |
| 51 | + |
| 52 | + return await res.json(); |
| 53 | +} |
| 54 | + |
| 55 | +(async function main() { |
| 56 | + |
| 57 | + const app = express(); |
| 58 | + const options = { |
| 59 | + inflate: true, |
| 60 | + limit: '300kb', |
| 61 | + type: 'application/x-protobuf' |
| 62 | + }; |
| 63 | + |
| 64 | + app.use(bodyParser.raw(options)); |
| 65 | + app.use(bodyParser.json()); |
| 66 | + |
| 67 | + const extensionId = await register(); |
| 68 | + |
| 69 | + const port = 3333; |
| 70 | + |
| 71 | + app.get('/*', (req, res) => { |
| 72 | + console.log("GET", req.url); |
| 73 | + res.sendStatus(200); |
| 74 | + }); |
| 75 | + |
| 76 | + app.post('/api/beta/sketches*', async (req, res) => { |
| 77 | + const root = await protobuf.load('/opt/extensions/src/agent_payload.proto'); |
| 78 | + const SketchPayload = root.lookupType('datadog.agentpayload.SketchPayload'); |
| 79 | + const obj = SketchPayload.decode(req.body); |
| 80 | + for(let i = 0; i < obj.sketches.length; ++i) { |
| 81 | + console.log("[sketch]", JSON.stringify(obj.sketches[i])); |
| 82 | + } |
| 83 | + res.sendStatus(200); |
| 84 | + }); |
| 85 | + |
| 86 | + |
| 87 | + app.post('/v1/input', async (req, res) => { |
| 88 | + if(JSON.stringify(req.body) !== '{}') { // to avoid printing empty logs due to the connectivity test |
| 89 | + for(let i = 0; i < req.body.length; ++i) { |
| 90 | + const logString = JSON.stringify(req.body[i]); |
| 91 | + if(logString.indexOf("[sketch]") === -1 && logString.indexOf("[log]") === -1) { // avoid inception |
| 92 | + console.log("[log]", logString); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + res.sendStatus(200); |
| 97 | + }); |
| 98 | + |
| 99 | + app.post('/*', (req, res) => { |
| 100 | + console.log("POST", req.url); |
| 101 | + res.sendStatus(200); |
| 102 | + }); |
| 103 | + |
| 104 | + app.listen(port); |
| 105 | + |
| 106 | + process.on('SIGINT', async () => await handleShutdown()); |
| 107 | + process.on('SIGTERM', async () => await handleShutdown()); |
| 108 | + |
| 109 | + while (true) { |
| 110 | + const event = await next(extensionId); |
| 111 | + if(event.eventType === SHUTDOWN_EVENT) { |
| 112 | + await handleShutdown(); |
| 113 | + break; |
| 114 | + } else { |
| 115 | + await handleShutdown(); |
| 116 | + throw new Error('Unexpected event'); |
| 117 | + } |
| 118 | + } |
| 119 | +})(); |
0 commit comments