Skip to main content

Testing, Logging, and Monitoring

This document details how to use the testing, logging, and monitoring features provided by CloudBase to help developers efficiently debug and monitor the operational status of Cloud Functions.

Prerequisites

Before starting to test and monitor cloud functions, ensure that:

  • You have created a Tencent Cloud development environment
  • You have deployed at least one cloud function
  • Have appropriate access permissions

Cloud Function Testing

Cloud Development provides multiple cloud function testing methods, supporting online debugging and local testing, to help developers debug code more conveniently.

Console Testing

The CloudBase console enables you to quickly test cloud function execution.

Test Steps

  1. In the corresponding cloud function management panel of the console, click the "Test" button to open the test pop-up window.
  2. Select a testing template or customize test parameters
  3. Click "Run" to execute the test
  4. View execution results and log output

Console Testing Page

Test Parameter Configuration

Use template parameters

  • Click the "Submit Method" dropdown menu and select the template method for the test function.
  • Template parameters are passed to the function as the event parameter during testing.

Custom Parameters Enter JSON-formatted test data in the test parameter editor:

{
"name": "test",
"data": {
"key": "value",
"number": 123
}
}

Local Debugging Tool

Besides the visual test feature, you can also use the command-line tool scf-cli for local debugging.

# Install scf-cli
npm install -g scf-cli

# Local Function Debugging
scf local invoke --template template.yaml --event event.json

Log Viewing

CloudBase provides a comprehensive logging system to help developers quickly locate and resolve issues.

Logging Features

  • Real-time Logs: Support real-time viewing of function execution logs
  • Structured Display: Log information is displayed in a structured manner, facilitating analysis.
  • Multi-dimensional Filtering: Supports filtering logs by time, status, and other criteria
  • Details: Contains detailed information such as request ID, execution time, memory usage, etc.

View Console Logs

  1. Log in to the CloudBase console
  2. Go to the corresponding cloud function management page
  3. Click the Logs tab
  4. Select the time range to view logs

Console Logs Page

Log Information Description

Each log record contains the following information:

  • Timestamp: The specific time of function execution
  • Request ID: The unique identifier for a function invocation
  • Execution Status: Success or failure status
  • Execution Duration: Function execution time
  • Memory Usage: Memory consumption during function runtime
  • Log Content: The content output by console.log and similar statements in the function

Logs Best Practices

Structured Logging

Use a structured logging approach in your function code:

exports.main = async (event, context) => {
const requestId = context.requestId;

// Log the request start
console.log(JSON.stringify({
level: 'INFO',
requestId,
message: message: 'Function execution started',
timestamp: new Date().toISOString(),
event: event
}));

try {
//Business logic.
const result = await processData(event);

// Log the successful result
console.log(JSON.stringify({
level: 'INFO',
requestId,
message: message: 'Function executed successfully',
result: result
}));

return result;
} catch (error) {
// Log error information
console.error(JSON.stringify({
level: 'ERROR',
requestId,
message: message: 'Function execution failed',
error: {
message: error.message,
stack: error.stack
}
}));

throw error;
}
};

Log Level Usage

Proper use of different log levels:

// Info
console.log('General log information');

// Important information to record
console.info('Important information level log');

// Warning information
console.warn('Warning level log');

// Error message.
console.error('Error level log');
Notes
  • Avoid logging sensitive information (such as passwords, keys, etc.)
  • Properly control the log output volume to avoid generating excessive logs
  • Use structured log formats to facilitate subsequent analysis and processing

Monitoring Data

Cloud Development provides comprehensive monitoring metrics to help developers understand the operational status and performance of cloud functions.

Monitoring Metrics

Cloud function monitoring includes the following core metrics:

  • Invocations: total number of function calls
  • Runtime: average and maximum execution time of the function
  • Errors: number of failed function executions
  • Success rate: percentage of successful function executions
  • Memory Usage: Memory consumption during function runtime
  • Concurrency: number of concurrent function instances

View Console Monitoring

  1. Log in to the CloudBase console
  2. Go to the corresponding cloud function management page
  3. Click the Monitoring tab
  4. Select the time range to view monitoring data
  5. Click 'Export Data' to export monitoring data.

Console Monitoring Page

Monitoring Features

  • Multiple Time Dimensions: Supports viewing monitoring data for different time ranges.
  • Chart Display: Intuitive charts display trends of various metrics.
  • Data Export: Supports exporting monitoring data for offline analysis
  • Real-time Updates: Monitoring data is updated in real time to reflect the latest status.