This topic describes the context object available when you write code in the PHP runtime environment of Function Compute.
When Function Compute runs your function, the runtime passes a $context parameter to your handler. This array contains metadata about the current invocation, your function configuration, and temporary credentials you can use to call other Alibaba Cloud services.
Context object structure
The following example shows the structure of the $context array in the PHP runtime:
[
'requestId' => 'b1c5100f-819d-c421-3a5e-7782a27d8a33',
'credentials' => [
'accessKeyId' => 'STS.access_key_id',
'accessKeySecret' => 'access_key_secret',
'securityToken' => 'security_token',
],
'function' => [
'name' => 'my-func',
'handler' => 'index.handler',
'memory' => 128,
'timeout' => 10,
'initializer' => 'index.initializer',
'initializationTimeout' => 10,
],
'service' =>[
'logProject' => 'my-log-project',
'logStore' => 'my-log-store',
'qualifier' => 'qualifier',
'versionId' => '1'
],
'region' => 'cn-shanghai',
'accountId' => '123456',
'tracing': {
'openTracingSpanContext': 'xxxxxxxxxxxx',
'jaegerEndpoint': 'xxxxxxxx',
'openTracingSpanBaggages': []
}
]Context fields
Top-level fields
| Field | Type | Description |
|---|---|---|
requestId | String | The unique ID of the current invocation. Record this ID to trace issues when errors occur. |
credentials | Array | The temporary AccessKey pair that Function Compute obtains by assuming your service-linked role. Valid for 36 hours. Use these credentials to call other Alibaba Cloud services, such as Object Storage Service (OSS), without hardcoding your AccessKey pair in the function code. For details, see Grant Function Compute permissions to access other Alibaba Cloud services. |
function | Array | Configuration of the invoked function. |
service | Array | Information about the service to which the function belongs. |
region | String | The ID of the region where the function runs. For example, cn-shanghai for the China (Shanghai) region. For all region IDs, see Service endpoints. |
accountId | String | The ID of the Alibaba Cloud account that owns the function. |
tracing | Object | Distributed tracing context based on the OpenTracing standard, populated when tracing is enabled. |
credentials fields
| Field | Type | Description |
|---|---|---|
accessKeyId | String | The AccessKey ID of the temporary credential. |
accessKeySecret | String | The AccessKey secret of the temporary credential. |
securityToken | String | The security token associated with the temporary credential. |
function fields
| Field | Type | Description |
|---|---|---|
name | String | The name of the function. |
handler | String | The handler of the function, in the format <file>.<method>. |
memory | Integer | The memory size configured for the function, in MB. |
timeout | Integer | The maximum execution time configured for the function, in seconds. |
initializer | String | The initializer of the function, in the format <file>.<method>. |
initializationTimeout | Integer | The maximum initialization time configured for the function, in seconds. |
service fields
| Field | Type | Description |
|---|---|---|
logProject | String | The Log Project in Simple Log Service associated with this service. |
logStore | String | The Logstore in Simple Log Service associated with this service. |
qualifier | String | The version or alias specified when the function was invoked. |
versionId | String | The version of the service that was actually invoked. |
tracing fields
| Field | Type | Description |
|---|---|---|
openTracingSpanContext | String | The OpenTracing span context for the current invocation, used for distributed trace propagation. |
jaegerEndpoint | String | The Jaeger collector endpoint for submitting trace data. |
openTracingSpanBaggages | Array | OpenTracing baggage items attached to the current span. |
Access context fields in your function
The following example shows how to access common context fields in a PHP handler:
<?php
function handler($event, $context) {
// Use requestId to correlate logs with a specific invocation
$requestId = $context['requestId'];
error_log("Request ID: " . $requestId);
// Use temporary credentials to call other Alibaba Cloud services
$accessKeyId = $context['credentials']['accessKeyId'];
$accessKeySecret = $context['credentials']['accessKeySecret'];
$securityToken = $context['credentials']['securityToken'];
// Read function configuration
$functionName = $context['function']['name'];
$memoryLimit = $context['function']['memory']; // in MB
$timeoutLimit = $context['function']['timeout']; // in seconds
// Identify which version or alias was invoked
$qualifier = $context['service']['qualifier'];
$versionId = $context['service']['versionId'];
return "OK";
}Note The
credentials in the context are temporary and expire after 36 hours. Do not cache them beyond the function invocation.该文章对您有帮助吗?