Skip to main content

Testing, Logging and Monitoring

This document details how to use the testing, log viewing, and monitoring features provided by TCB to help developers efficiently debug and monitor the running status of cloud functions.

Prerequisites

Before testing and monitoring the cloud function, make sure:

  • The Tencent Cloud TCB environment has been created
  • At least one cloud function has been deployed
  • Appropriate access permissions have been granted

Cloud Function Testing

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

Console Testing

Through the TCB console, you can quickly test the execution of cloud functions.

Test Steps

  1. In the console's corresponding cloud function management panel, click the 'Test' button to open the test pop-up.
  2. Select a test template or customize test parameters
  3. Click 'Execute' to run the test
  4. View the execution result and log output

Console Test 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 Tools

In addition to the visual testing feature, you can also use the command-line tool scf-cli for local debugging:

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

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

Log Viewing

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

Log Features

  • Real-time Logs: Supports real-time viewing of function execution logs
  • Structured Display: Log information is presented in a structured format for easy analysis.
  • Multi-dimensional Filtering: Supports filtering logs by conditions such as time and status.
  • Detailed Information: Includes detailed information such as request ID, execution time, and memory usage.

Console Log Viewing

  1. Log in to the TCB console
  2. Go to the corresponding cloud function management page
  3. Click the "Log" tab
  4. Select a time range to view log records.

Console Log 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: the time consumed by function execution
  • Memory usage: memory footprint during function execution
  • Log content: content output by console.log, etc., in the function

Log Best Practices

Structured Logging

Use structured logging in function code:

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

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

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

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

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

throw error;
}
};

Log Level Usage

Properly use different log levels:

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

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

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

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

Monitoring Data

TCB 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: The total number of times the function is called.
  • Runtime: The average and maximum execution time of the function.
  • Errors: The count of function execution failures.
  • Success Rate: The percentage of successful function executions.
  • Memory usage: Memory footprint during function execution
  • Concurrency: The number of simultaneously executing function instances

Console Monitoring View

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

Console Monitoring Page

Monitoring Features

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