Debugging
Debugging is a process that allows developers to fix log messages or variables, for example. The debugging method allows Edge Functions users to generate logs as if they were using a browser’s JavaScript.
What it is
The debugging process in Edge Functions has the purpose of promoting better control over the application to identify errors. It can also be used to assist in the development and debugging of a function, resulting in greater observability.
How it works
To debug codes, you must create a new edge function that will use the log method. This method prints to the console a message previously sent by a parameter to the function.
Creating an edge function
Follow these steps to create an edge function that logs messages:
- Access Azion Console with your login credentials.
- In the Products Menu, navigate to Libraries > Edge Functions.
- Click on + Edge Function to create a new function.
- Choose a name for your function.
- In the Code tab, paste the following code:
- Click the Save button to finish the process.
Now that you’ve created the edge function, you must configure your edge application to run the function before the logs are accessible.
Instantiating the edge function
To instantiate the newly created edge function in your edge application, follow these steps:
- On the Products Menu, navigate to Build > Edge Application.
- Click on the edge application where you want to add the function or create a new one.
- Go to the Functions Instances tab and click + Function Instance.
- Name your function instance.
- Select the newly created edge function from the dropdown menu.
- Click the Save button.
Now your edge function is instantiated and ready to be used in your edge application. You can configure the execution conditions and behaviors using the Rules Engine:
- While still configuring your edge application, navigate to the Rules Engine tab and click the + Rule button.
- Give your rule a descriptive name.
- Select Request Phase.
- In the Criteria section, configure the criteria as follows:
- If
${uri}
starts with/
- If
- In the Behaviors section, select Run Function from the dropdown menu.
- Choose the instance of your edge function.
- Click the Save button.
Now the function will be executed according to the rule and its logs are ready to be captured by Data Stream.
Setting up a Data Stream
- On the Products Menu, go to Observe > Data Stream.
- Select an existing stream or create a new one by clicking the + Stream button.
- In the Name* field, give your stream a unique and easy-to-remember name.
- In the Data Settings section, select Edge Functions as the source.
- Select the Edge Functions Event Collector template. In the Data Set box, you’ll see the preset of variables:
Variable | Definition |
---|---|
$time | Date and time of the request. |
$client | Unique Azion customer identifier. |
$global_id | Settings identification. |
$edge_function_id | Edge Function identifier. |
$request_id | Request identifier. |
$message_source | The source of the message. |
$log_level | Level of the log created (ERROR, WARN, INFO, DEBUG, TRACE). |
$log_message | Message used on the log when the function is requested. |
-
In the Destination section, leave the Connector as
Standard HTTP/HTTPS POST
and enter the URL which will receive the collected data. -
Click the Save button to finish the process.
Now your data stream will collect logs from edge functions and send them to the specified URL.
Check the documentation to learn more about Data Stream and its configurations. You can also use Real-Time Events to observe and analyze the generated logs.
- In the Products Menu, navigate to Observe > Real-Time Events.
- Select the Edge Functions Console tab to see the logs originated from edge functions. Use the filters to specify a detailed query.
- On the Data Stream tab, you can see the logs of data sent to various endpoints via Data Stream.
- Click on any item to see details of the data sent.
Read the Real-Time Events documentation to learn more.
Enhance your Edge Functions debugging skills. Watch the video below: