Updated on 2023-12-29 GMT+08:00

Developing an Event Function

Function Syntax

Syntax for creating a handler function in Go:

func Handler (payload []byte, ctx context.RuntimeContext)

  • Handler: name of the handler function.
  • payload: event parameter defined for the function. The parameter is in JSON format.
  • ctx: runtime information provided for executing the function. For details, see SDK APIs.

SDK APIs

The Go SDK provides context, and logging APIs. Download the Go SDK (Go SDK.sha256).

  • Context APIs

    The context APIs are used to obtain the context, such as agency AK/SK, current request ID, allocated memory space, and number of CPUs, required for executing a function.

    Table 1 describes the context APIs provided by FunctionGraph.

    Table 1 Context methods

    Method

    Description

    getRequestID( )

    Obtains a request ID.

    getRemainingTimeInMilligetRunningTimeInSecondsSeconds ( )

    Obtains the remaining running time of a function.

    getAccessKey( )

    Obtains the AK (valid for 24 hours) with an agency. If you use this method, you need to configure an agency for the function.

    NOTE:

    FunctionGraph has stopped maintaining the getAccessKey API in the Runtime SDK. You cannot use this API to obtain a temporary AK.

    getSecretKey( )

    Obtains the SK (valid for 24 hours) with an agency. If you use this method, you need to configure an agency for the function.

    NOTE:

    FunctionGraph has stopped maintaining the getSecretKey API in the Runtime SDK. You cannot use this API to obtain a temporary SK.

    getSecurityAccessKey()

    Obtains the SecurityAccessKey (valid for 24 hours) with an agency. If you use this method, you need to configure an agency for the function.

    getSecuritySecretKey()

    Obtains the SecuritySecretKey (valid for 24 hours) with an agency. If you use this method, you need to configure an agency for the function.

    getSecurityToken()

    Obtains the SecurityToken (valid for 24 hours) with an agency. If you use this method, you need to configure an agency for the function.

    getUserData(string key)

    Uses keys to obtain the values passed by environment variables.

    getFunctionName( )

    Obtains the name of a function.

    getRunningTimeInSeconds ( )

    Obtains the timeout of a function.

    getVersion( )

    Obtains the version of a function.

    getMemorySize( )

    Obtains the allocated memory.

    getCPUNumber( )

    CPU usage of a function.

    getPackage( )

    Obtains a function group, that is, an app.

    getToken( )

    Obtains the token (valid for 24 hours) with an agency. If you use this method, you need to configure an agency for the function.

    getLogger( )

    Obtains the logger method provided by the context. By default, information such as the time and request ID is output.

    getAlias

    Obtains function alias.

    Results returned by using the GetToken(), GetAccessKey(), and GetSecretKey() methods contain sensitive information. Exercise caution when using these methods.

  • Table 2 describes the logging API provided in the Go SDK.
    Table 2 Logging API

    Method

    Description

    RuntimeLogger()

    • Records user input logs by using the method Logf(format string, args ...interface{}).
    • This method outputs logs in the format of Time-Request ID-Output, for example, 2017-10-25T09:10:03.328Z 473d369d-101a-445e-a7a8-315cca788f86 test log output.

Execution Result

The execution result consists of the function output, summary, and log output.

Table 3 Description of the execution result

Parameter

Successful Execution

Failed Execution

Function Output

The defined function output information is returned.

A JSON file that contains errorMessage and errorType is returned. The format is as follows:

{
    "errorMessage": "", 
    "errorType":"", 
}

errorMessage: Error message returned by the runtime.

errorType: Error type.

Summary

Request ID, Memory Configured, Execution Duration, Memory Used, and Billed Duration are displayed.

Request ID, Memory Configured, Execution Duration, Memory Used, and Billed Duration are displayed.

Log Output

Function logs are printed. A maximum of 4 KB logs can be displayed.

Error information is printed. A maximum of 4 KB logs can be displayed.