Navigation

    APPDRAG Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Popular

    REMINDER

    Please be respectful of all AppDragers! Keep it really civil so that we can make the AppDrag community of builders as embracing, positive and inspiring as possible.

    Is the AppDrag function name (or URL?) available from within an API Function?

    Cloud Backend (Cloud DB, API Builder)
    2
    4
    343
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Daniel Mulroy
      Daniel Mulroy last edited by

      I'm setting up Sentry.io to track performance and errors for my API functions, but right now everything is labeled with the AppDrag appID and function #, e.g. "my-great-app-2s87s0s-160480"

      Obviously, this makes understanding which function it is complicated 😉

      I can set a new name for it at runtime by passing it to the Sentry client, but I don't know how to 'get' the API's AppDrag name (or the URL).

      Any tips @jbenguira ?

      1 Reply Last reply Reply Quote 0
      • Daniel Mulroy
        Daniel Mulroy last edited by Daniel Mulroy

        Man, today is the day I answer all my own questions.

        The event and context variables handed to the function WHEN USING THE 'TRY' FEATURE contain this information:

        [{
                "POST": {},
                "GET": {
                    "APPDRAG_FUNCTION_NAME": "myGreatFunction",
                    "APPDRAG_FUNCTION_FOLDER": "",
                    "APPDRAG_FUNCTION_COMMAND": "CloudAPIExecuteFunction",
                    "APPDRAG_FUNCTION_APPID": "my-great-app-2s87s0s"
                },
                "FILES": [],
                "HEADERS": {
                    "ip": "37.xxx.x.x",
                    "from-url": "this is the URL the function was called from ",
                    "Content-Type": "application/x-www-form-urlencoded;charset=utf-8",
                    "Accept": "*/*",
                    "Accept-Encoding": "gzip, deflate, br",
                    "Accept-Language": "en-US,en;q=0.9",
                    "Host": "api.appdrag.com",
                    "Referer": "https://prod.appdrag.com/",
                    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 11_1_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36",
                    "X-Forwarded-For": "IP ADDRESS HERE AGAIN",
                    "X-Forwarded-Proto": "https",
                    "X-Forwarded-Port": "443",
                    "X-Amzn-Trace-Id": "Root=1-600ea7ec-5834f1c526eef8e995ff07ce8",
                    "dnt": "1",
                    "sec-gpc": "1",
                    "origin": "https://prod.appdrag.com",
                    "sec-fetch-site": "same-site",
                    "sec-fetch-mode": "cors",
                    "sec-fetch-dest": "empty"
                },
                "body": ""
            }, {
                "callbackWaitsForEmptyEventLoop": false,
                "functionVersion": "$LATEST",
                "functionName": "my-great-app-2s87s0s-160480",
                "memoryLimitInMB": "128",
                "logGroupName": "/aws/lambda/my-great-app-2s87s0s-160480",
                "logStreamName": "2021/01/25/[$LATEST]1a2e8131cd074f3fa11394ecddab429c",
                "invokedFunctionArn": "arn:aws:lambda:eu-west-1:648990989054:function:my-great-app-2s87s0s-160480",
                "awsRequestId": "df38fab4-0063-42f6-b8b7-69d6a885c46b"
            }],
        

        HOWEVER, the GET/POST/PUT parameters don't contain those variables when called normally... only when called via the "Try" feature...

        Best answer I have is to parse the 'from-url' of the headers and return the part after the trailing slash like this:

        event["HEADERS"]['from-url'].split('/').slice(-1)
        

        Anyone have a better idea?

        1 Reply Last reply Reply Quote 1
        • Wassim
          Wassim last edited by

          Seems a very good and practical way to do it. You can also manually define it in your API functions while setting sentry up no?

          Daniel Mulroy 1 Reply Last reply Reply Quote 0
          • Daniel Mulroy
            Daniel Mulroy @Wassim last edited by Daniel Mulroy

            @Wassim said in Is the AppDrag function name (or URL?) available from within an API Function?:

            Seems a very good and practical way to do it. You can also manually define it in your API functions while setting sentry up no?

            Yes, I could, but I'm looking for reusable / template code I can copy/paste very quickly between functions and/or use in my template library... so I don't want to think about it each time, just want it to work every time 🙂

            For reference, here's my final code to get the function name both during testing "Try" in CloudBackend and also when the function is called 'normally':

            module.exports.getFunctionInfo = (event, context) => {
                let fInfo = {};
            
                // Try to extrapolate function info
            // sometimes the from-url is not available, so wrapping this in a try/catch block
                try {
                    const httpMethods = ["GET", "POST", "PUT", "DELETE", "PATCH"];
                    var functionHandle = event.HEADERS['from-url'].split('/').slice(-1)[0]; // Get from URL if it's been called from the web
                    for (let method of httpMethods) { // Get from parameters if it's been called from "Try" method, because above method will not work. Could also ignore errors if running from Try Now as they are likely development errors....
                        if (event[method] && event[method].APPDRAG_FUNCTION_NAME != null) functionHandle = event[method].APPDRAG_FUNCTION_NAME;
                    }
                    fInfo.functionHandle = functionHandle;
                } catch (e) {
                    fInfo.functionHandle = "";
                }
            
                fInfo.functionVersion = context.functionVersion;
                fInfo.functionName = context.functionName;
                return fInfo;
            };
            
            1 Reply Last reply Reply Quote 3
            • First post
              Last post