Lesson 2: Logging and Error Handling Basics
Logging Basics for Weather Monitoring with Logtap and CATCH Nodes
Overview
Prerequisites: This lesson assumes that you completed Lesson 1: HTTP Agent Introduction
You will need the components (object-type, flow, and agent) from that lesson to complete Lesson 2.
Objective: Implement logging for weather data submissions using the logtap nodes and introduce error handling with the CATCH node.
Description: This project builds on the flow from Project 1 by adding logtap nodes for logging at various levels (debug, info, warn, error). The logtap node is used to monitor weather data submissions and track important events. In case of an error (e.g., invalid data), the CATCH node will capture the error and log it at the "error" level. Students will also learn how to view the logs from a running agent.
Terminal Objective: Understand how to debug agent-deployed flows and handle errors. Enabling Objectives:
Introduce the logtap node and explain how to log at various levels (debug, info, warn, error) in a synchronous manner.
Add logtap nodes at key points in the flow to log data submissions and important events like threshold breaches in weather monitoring.
Demonstrate how to use the CATCH node to capture errors (e.g., invalid weather data) and log those at the error level using the logtap node.
View the logs from a running agent.
Skills: LOG-1, LOG-2, LOG-3, ERROR-2, ERROR-3
By using logtap nodes and introducing error handling with the CATCH node, this project will help students understand how to monitor agent-deployed flows, handle different log levels, and deal with errors in a structured way.
Detailed Steps
Part 1 — Adding Logging to your Flows
Login to your Contextual tenant. If you completed Lesson #1, then you will have the object-type, flow, and agent definitions from that lesson to use in this lesson.
Navigate to Components/Flows and open the flow editor for the "submit-weather-report" flow.
Locate the log-tap node in the palette.
Drag the log-tap node onto the canvas and onto the wire in between the HTTP-IN node (for GET /form) and the template node. If there isn't enough room for the log-tap node, drag the nodes farther apart to make room.
Now double-click the log-tap node to open the configuration editor.
Add a name: "GET /flow"
Click on "msg." and select "compelte msg object"
Push "Done" to save your changes
Insert additional log-tap nodes in between the other nodes in the flow. You will end up with four log-tap nodes looking something like this:
Part 2 — Debugging with Inject Nodes
There are two ways to see the outputs from your log-tap nodes. The first is in the flow editor itself while the second is in the log-viewer in the console.
In order to see log outputs in the flow editor however we need to actually execute the flow. We do that by using an "inject" node.
Find the inject node in the palette under the "common" group and drag it onto the canvas above the HTTP-IN GET node.
Double-click the inject node and open the configuration editor. Give the node a name such as "TEST".
Now wire the Inject node to the log-tap node as shown below:
Push "Save" in the top-right corner of the editor to save all of your changes.
Now you are ready to test. On the right side of editor you will find various panes that provide different functions.
Click on the little "Bug" button on the toolbar to select the debug pane.
Now click on the litte button on the left side of the Inject node. This will "inject" the test payload into the flow.
You should now see the debug output in the Debug pane. There will be a top-level message from each of the two log-tap nodes. You can expand the messages by clicking the little triangle next to each message.
Play around with the log messages and see what is in each one. The first one should show you the test payload from the inject node, while the second one should show you the HTML from the template node.
Part 3 — Handling Errors with Catch Nodes
The next step is to add a Catch Node to handle errors. The simplest form of a catch node, which is a Catch ALL, is what we will cover in this lesson.
Find the Catch Node (in the Common group) in the palette and drag it onto the canvas.
Now drag a log-tap Node onto the canvas and place it next to the Catch Node. Wire the catch node to the log-tap node.
Set the "Log Level" for the log-tap node to "Error".
Now drag an HTTP Response Node to the canvas and place it to the right of the log-tap node. Wire the log-tap node to the HTTP Response node.
Double-click the HTTP Response node to open the configuration editor.
Set the name of the node to "SERVER ERROR"
Set the status code to 500.
Click Done to save your changes to the node.
You should now have a new set of nodes in your flow that look something like this:
Click Save (upper right) to save changes to the flow.
Part 4 — Updating the Object Type with Validation Rules
In order to be able to test the Catch node, we need to be able to force an error to occur in the flow. In order to do this, we will first update the object-type to add validation rules on the 'temperature' property.
Navigate to Components/Object Types and click on weather-report to open that type. Click on the "Data Schema" tab to edit the schema.
The modify the schema for the temperature attribute as shown below.
With the minimum and maximum values specified on that field, we will be able to force an error to occur simply by entering a value outside of the valid range.
Part 5 — Viewing Logs in the Debug Pane
Drag a new inject node into the flow above your "Submit Form" (/POST) node.
Double-click the Inject node to open the configuration editor.
Where it says "timestamp", click on that drop-down and select "JSON".
Now click the "..." button next to the "{}"
In the JSON editor window that opens, create a test JSON weather report with an invalid value.
Click "Done" to save your changes in the editor.
Give the inject node a name such as "INVALID".
Click "Done" a second time to save the changes to the Inject node.
Save the flow.
Inject the test message using the "INVALID" inject node button.
View the output in the debug pane.
You will now see output from the "Error" log-tap node created earlier showing the validation error:
Part 6 — Restarting Your Agent with the New Flow
Go back to the Console and navigate to Components/Agents
Open the "Submit Weather Report" agent
You should see the following under the details section:
Click "Update to Version X". This will update the version of the flow currently used and prompt you to restart the agent. Click "RESTART NOW" when prompted.
Use the Operations tab to check on the instance status. Wait for the agent to return to the "Running" state before continuing.
Part 7 — Using the Web Form to Test
Go back to the Definitions tab for the agent and copy the "Agent URL" to the clipboard
Open a browser and paste in the URL. Add "/form" to the end of the URL and hit enter.
You should once again see the Weather Report form.
Submit a valid weather report with 25 degrees and a station ID.
Submit an invalid weather report with 101 for the temperature and a station ID.
Part 8 — Viewing Logs in the Console
Now go back to the console.
Open the agent and go to the "logs" tab.
You will see something like this:
Notice that you have only one flow log from the two tests that. you did above in Part 7 — Using the Web Form to Test. That is because Log levels below "Warn" are not active when the flow is running within an agent. You can see those log outputs in the editor, but they won't appear in the agent logs. The 'flow' log that you do see is the one from the error condition that you created by submitting an invalid temperature using the web form.
Click on the 'id' next to the flow log and open the log.
You will see the "Error" output from your log-tap node. You can open the second-level "message" object to see the details.
Congratulations! You have now completed Lesson 2. You now have a good understanding of logging and error handling in Contextual flows, and you know how to view those log messages in both the editor and in the agent log viewer.
Last updated