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:

  1. Access Azion Console with your login credentials.
  2. In the Products Menu, navigate to Libraries > Edge Functions.
  3. Click on + Edge Function to create a new function.
  4. Choose a name for your function.
  5. In the Code tab, paste the following code:
async function handleRequest(request) {
console.log("Hello World");
return new Response("Checking console output.", {
status: 200,
});
}
addEventListener("fetch", (event) => {
event.respondWith(handleRequest(event.request));
});
  1. 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:

  1. On the Products Menu, navigate to Build > Edge Application.
  2. Click on the edge application where you want to add the function or create a new one.
  3. Go to the Functions Instances tab and click + Function Instance.
  4. Name your function instance.
  5. Select the newly created edge function from the dropdown menu.
  6. 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:

  1. While still configuring your edge application, navigate to the Rules Engine tab and click the + Rule button.
  2. Give your rule a descriptive name.
  3. Select Request Phase.
  4. In the Criteria section, configure the criteria as follows:
    • If ${uri} starts with /
  5. In the Behaviors section, select Run Function from the dropdown menu.
  6. Choose the instance of your edge function.
  7. 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

  1. On the Products Menu, go to Observe > Data Stream.
  2. Select an existing stream or create a new one by clicking the + Stream button.
  3. In the Name* field, give your stream a unique and easy-to-remember name.
  4. In the Data Settings section, select Edge Functions as the source.
  5. Select the Edge Functions Event Collector template. In the Data Set box, you’ll see the preset of variables:
{
"time": "$time",
"client": "$client",
"configuration": "$global_id",
"edgeFunctionID": "$edge_function_id",
"requestID": "$request_id",
"messageSource": "$message_source",
"logLevel": "$log_level",
"logMessage": "$log_message"
}
VariableDefinition
$timeDate and time of the request.
$clientUnique Azion customer identifier.
$global_idSettings identification.
$edge_function_idEdge Function identifier.
$request_idRequest identifier.
$message_sourceThe source of the message.
$log_levelLevel of the log created (ERROR, WARN, INFO, DEBUG, TRACE).
$log_messageMessage used on the log when the function is requested.
  1. In the Destination section, leave the Connector as Standard HTTP/HTTPS POST and enter the URL which will receive the collected data.

  2. 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.

  1. In the Products Menu, navigate to Observe > Real-Time Events.
  2. Select the Edge Functions Console tab to see the logs originated from edge functions. Use the filters to specify a detailed query.
  3. On the Data Stream tab, you can see the logs of data sent to various endpoints via Data Stream.
  4. 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:


Contributors