This topic describes context in Python runtimes of Function Compute. This topic also provides an example on how to use context.
What is a context?
When Function Compute runs your function, the system passes a context object to the method that is used to execute the function. The context object contains information about the invocation, service, function, and runtime environment.
The following table describes the parameters that are contained in a context.
Field | Type | Description |
request_id | String | The unique ID of the request that is used to invoke the function. You can record the ID for troubleshooting if an error occurs. |
credentials | Credentials structure, which contains the following fields:
| The temporary AccessKey pair that Function Compute obtains by assuming your service-linked role. The temporary AccessKey pair is valid for 36 hours. You can use |
function | FunctionMeta structure, which contains the following fields:
| The basic information about the invoked function, such as the name, handler, memory, and timeout period of the function. |
service | ServiceMeta structure, which contains the following fields:
| The information about the service to which the function belongs, such as the name, the related project, and Logstore in Simple Log Service, the version, and the alias of the service. The |
region | String | The ID of the region in which the function is invoked. For example, if the function is invoked in the China (Shanghai) region, the region ID is cn-shanghai. For more information, see Service endpoints. |
account_id | String | The ID of the Alibaba Cloud account to which the function belongs. |
The following sample code is used as an example to describe the context object.
# -*- coding: utf-8 -*-
import json
class Credentials:
def __init__(self, access_key_id, access_key_secret, security_token):
self.access_key_id = access_key_id
self.access_key_secret = access_key_secret
self.security_token = security_token
class Tracing:
def __init__(self, span_context, base64_baggages, jaeger_endpoint):
self.span_context = span_context
self.jaeger_endpoint = jaeger_endpoint
self.span_baggages = self.parseOpenTracingBaggages(base64_baggages)
def parseOpenTracingBaggages(self, base64_baggages):
span_baggages = {}
# None || '' returns false
if base64_baggages:
try:
import base64
str_baggages = base64.b64decode(base64_baggages)
span_baggages = json.loads(str_baggages)
except Exception as e:
import logging
fc_sys_logger = logging.getLogger('fc_sys_logger')
fc_sys_logger.error('Failed to parse base64 opentracing baggages:[{}], err: {}'.format(base64_baggages, e))
return span_baggages
class FunctionMeta:
def __init__(self, name, handler, memory, timeout):
self.name = name
self.handler = handler
self.memory = memory
self.timeout = timeout
class FCContext:
def __init__(self, account_id, request_id, credentials, function_meta, service_meta, region, tracing):
self.credentials = credentials
self.function = function_meta
self.request_id = request_id
self.service = service_meta
self.region = region
self.account_id = account_id
# self.tracing = tracing
Example
For an example on context use, see the Example 2: Read and write OSS resources by using a temporary AccessKey pair section in the "Handlers" topic.