I'm looking for a way to query Azure Functions, or ApplicationInsights for all invocations made to a given function in an Azure Function Application.
I found that I can do this quite easily with the AZ cli, something like:
az monitor app-insights query --app "{AZURE_FUNCTION_NAME}" --resource-group "{RESOURCE_GROUP_NAME}" --analytics "requests | where name == '{FUNCTION_NAME}' | top 1 by timestamp desc"
Is there any way I can do this in C#?
I actually am just interested in knowing if my function has run and if it is finished. Basically I want to know this so I can initiate my integration tests.
I've looked into the TelemetryClient but I can't really get it to work. If someone has an example using the TelemetryClient I'd gladly take a look at that as well. But primarily, I am mainly looking for a way to check if my functions have run or not in the recent hour.
The TelemetryClient
can only be used to send telemetry. You cannot query any data with it.
You can either use the Azure Monitor Query client library for .NET for that or leverage the Log Analytics REST API
Example using the client:
var client = new LogsQueryClient(new DefaultAzureCredential());
string workspaceId = "XXX";
var functionName = "FunctionName";
var response = await client.QueryWorkspaceAsync(
workspaceId,
$"AppRequests | where Name == '{functionName}' | top 1 by TimeGenerated desc",
new QueryTimeRange(TimeSpan.FromDays(1)));
foreach (var rowData in response.Value.Table.Rows)
{
Console.WriteLine($"{rowData}");
}
You need to install the Azure.Identity and Azure.Monitor.Query packages for the code to run.
Example using the api:
GET https://api.loganalytics.io/v1/workspaces/63613592-b6f7-4c3d-a390-22ba13102111/query?query=union * | where TimeGenerated > ago(1h) | summarize count() by Type, TenantId
Do mind that it can take some time between the moment the function has finished and the moment the telemetry appears in Azure Monitor.