-
Notifications
You must be signed in to change notification settings - Fork 441
Retrieving information about the currently running function
In some scenarios, information about the currently running function, like the function directory or its name, are needed. Here is how you can access this information from your function code:
You can modify your function method to take an additional parameter of type ExecutionContext
(If using pre-compiled assemblies, the full type name is Microsoft.Azure.WebJobs.ExecutionContext
in the Microsoft.Azure.WebJobs.Extensions
assembly).
With this additional parameter in place, the runtime will automatically provide an context instance that exposes the following properties:
Property name | Type | Description |
---|---|---|
InvocationId | Guid | Provides the invocation ID, uniquely identifying the current invocation |
FunctionName | String | Provides the name of the current function (e.g. HttpTrigger1) |
FunctionDirectory | String | Provides the current function directory (e.g. when running on Azure, d:\home\site\wwwroot\HttpTrigger1 ) |
FunctionAppDirectory | String | Points to the root directory of the function app (e.g. when running on Azure, d:\home\site\wwwroot ) |
Here's an example of a C# function that uses the ExecutionContext
to return the Invocation ID when executed:
public static HttpResponseMessage Run(HttpRequestMessage req, TraceWriter log, ExecutionContext context)
{
return req.CreateResponse(System.Net.HttpStatusCode.OK, context.InvocationId);
}
The context passed into your function exposes an executionContext
property, which is an object with the following properties:
Property name | Type | Description |
---|---|---|
invocationId | String | Provides the invocation ID, uniquely identifying the current invocation |
functionName | String | Provides the name of the current function (e.g. HttpTrigger1) |
functionDirectory | String | Provides the current function directory (e.g. when running on Azure, d:\home\site\wwwroot\HttpTrigger1 ) |
Here's an example of a JavaScript HTTP function that uses the ExecutionContext
to return the Invocation ID when executed:
module.exports = function (context, req) {
context.res = {
body: context.executionContext.invocationId
};
context.done();
};
Function context information is exposed by the $TriggerMetadata
variable in the run.ps1
file. This variable contains the function name, directory, and invocation id. If you are using a template that does not have the $TriggerMetadata
variable, you can get function directory via $PSScriptRoot
. See example below:
using namespace System.Net
# Input bindings are passed in via param block.
param($Request, $TriggerMetadata)
# Write to the Azure Functions log stream.
Write-Host "PowerShell HTTP trigger function processed a request."
write-output "Current directory:"
write-output "PSScriptRoot: $PSScriptRoot"
write-output "Directory: $($TriggerMetadata.FunctionDirectory)"
write-output "FunctionName: $($TriggerMetadata.FunctionName)"
write-output "InvocationId: $($TriggerMetadata.InvocationId)"
# more code ...
The function context information can be exposed by introducing context: func.Context
in the function parameter. This context contains invocation_id, function_name and function_directory. See example below:
import azure.functions as func
def main(req: func.HttpRequest, context: func.Context) -> func.HttpResponse:
return func.HttpResponse(
f"invocation_id = {context.invocation_id}\n"
f"function_name = {context.function_name}\n"
f"function_directory = {context.function_directory}"
)
Function context information is exposed by the following environment variables:
Property name | Description |
---|---|
EXECUTION_CONTEXT_INVOCATIONID | Provides the invocation ID, uniquely identifying the current invocation |
EXECUTION_CONTEXT_FUNCTIONNAME | Provides the name of the current function (e.g. HttpTrigger1) |
EXECUTION_CONTEXT_FUNCTIONDIRECTORY | Provides the current function directory (e.g. when running on Azure, d:\home\site\wwwroot\HttpTrigger1 ) |
Here's an example of a PowerShell HTTP function that returns a response containing the function name, directory and invocation ID in JSON format:
$content = "FUNCTIONNAME=$EXECUTION_CONTEXT_FUNCTIONNAME,FUNCTIONDIRECTORY=$EXECUTION_CONTEXT_FUNCTIONDIRECTORY"
$result = @{Status = 200; Headers =@{ "content-type" = "text/plain" }; Body = $content} | ConvertTo-Json
Out-File -Encoding Ascii $res -inputObject $result;
- Configuration Settings
- function.json
- host.json
- host.json (v2)
- Http Functions
- Function Runtime Versioning
- Official Functions developers guide
- Host Health Monitor
- Managing Connections
- Renaming a Function
- Retrieving information about the currently running function
- Site Extension Resolution
- Linux Consumption Regions
- Using LinuxFxVersion for Linux Function apps
- Out-of-proc Cancellation Tokens
- Assembly Resolution in Azure Functions
- ILogger
- Precompiled functions
- Official Functions C# developer reference
- Contributor Onboarding
- Development Process
- Deploying the Functions runtime as a private site extension
- Authoring & Testing Language Extensions
- Bindings in out-of-proc
- Language Extensibility
- Worker Capabilities
- Investigating and reporting issues with timer triggered functions not firing
- Sharing Your Function App name privately
- Azure Functions CLI release notes [moved here]
- Function App Zipped Deployment [deprecated]