All Products
Search
Document Center

Function Compute:Context

Last Updated:Nov 08, 2024

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 a 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 execution environment.

Python 3.12

The following table describes the parameters that are supported by the context object in the Python 3.12 runtime.

Parameter

Type

Description

request_id

String

The ID of the request. You can record the ID for troubleshooting if an error occurs.

function

The FunctionMeta struct, which contains the following fields:

  • name

  • handler

  • memory

  • timeout

  • version_id

  • qualifier

The basic information about the invoked function, such as the name, handler, memory, timeout period, version, and alias.

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.

logger

String

Records logs during function executions.

account_id

String

The ID of the Alibaba Cloud account to which the function belongs.

log_project and log_store

string

The name of the project in Simple Log Service. The Logstore information.

The following sample code is used as an example to describe the context object in Python 3.12.

# -*- coding: utf-8 -*-

import json


class FunctionMeta:
    def __init__(self, name, handler, memory, timeout, version_id, qualifier):
        self.name = name
        self.handler = handler
        self.memory = memory
        self.timeout = timeout
        self.version_id = version_id
        self.qualifier = qualifier


class FCContext:
    def __init__(self, account_id, request_id, function_meta, logger, log_project, log_store,
                 region):
        self.account_id = account_id
        self.request_id = request_id
        self.function = function_meta
        self.logger = logger
        self.log_project = log_project
        self.log_store = log_store
        self.region = region

Python 3.10, Python 3.9, and Python 3.6

The following table describes the parameters that are supported by the context object in Python 3.10, Python 3.9, and Python 3.6 runtimes.

Parameter

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

The credentials struct, which contains the following fields:

  • access_key_id

  • access_key_secret

  • security_token

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 credentials in your code to access related services such as Object Storage Service (OSS). This way, you can access the services without the need to write your AccessKey pair in the function code. For more information, see Grant Function Compute permissions to access other Alibaba Cloud services.

function

The FunctionMeta struct, which contains the following fields:

  • name

  • handler

  • memory

  • timeout

The basic information about the invoked function, such as the name, handler, memory, and timeout period of the function.

service

The ServiceMeta struct, which contains the following fields:

  • log_project

  • log_store

  • qualifier

  • version_id

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 qualifier parameter indicates the version or alias of the service that is specified when you invoke a function. The versionId parameter indicates the version of the service that is actually invoked.

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 more information about how to use the context, see Example 2: Read and write OSS resources by using a temporary AccessKey pair.