This topic describes the context object that is involved when you use the Python runtime in Function Compute to write code. This topic also provides the sample code for the context object.
What is context?
When Function Compute runs a function, the system passes a context object to the method that is used to execute the function. The object contains the information about the invocation, service, function, tracing analysis, and execution environment.
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 consists of 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 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 Endpoints. |
account_id | String |
The ID of the Alibaba Cloud account to which the function belongs. |
tracing | Tracing structure, which contains the following fields:
|
The parameters related to Tracing Analysis. For more information, see Overview. |
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 ServiceMeta:
def __init__(self, service_name, log_project, log_store, qualifier, version_id):
self.name = service_name
self.log_project = log_project
self.log_store = log_store
self.qualifier = qualifier
self.version_id = version_id
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
Sample code for using an HTTP handler
For more information about how to use the context object, see Example 2: Read and write OSS resources by using a temporary AccessKey pair in a secure manner.