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. .
- 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.
Execution Result
The execution result consists of the function output, summary, and log output.
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. |
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot