For example I have this string "CTASK0220892", I want to search this in Application Insights for Azure Function App.
What would be the query to search this string?
It depends on which method you're using to send the string "CTASK0220892" to application insights.
As an example, if you're using ILogger.LogInformation method, like below:
[FunctionName("Function1")]
public static void Run([BlobTrigger("samples-workitems/{name}", Connection = "AzureWebJobsStorage")]Stream myBlob, string name, ILogger log)
{
//use ILogger.LogInformation method to send the string to application insights.
log.LogInformation("CTASK0220892");
log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
}
Then in application insights, you can find this string in the trace table. Nav to azure portal -> your application insights which is connected to the azure function -> Logs, then use the query below:
traces
| where message contains "CTASK0220892"
Note that: there're many operators besides the contains operator, like ==, !=,startswith etc. Please use the proper operator as per your need.
Here is the test result:
Related
I have created a blob trigger azure function which uses connection string in the code at the moment.
local.settings.json
public static class BlobTrigger_Fun
{
[FunctionName("BlobTrigger_Fun")]
public static void Run([BlobTrigger("democontainerazure/{name}", Connection = "AzureWebJobsStorage")]Stream myBlob, string name, ILogger log)
{
log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
}
}
I want to use managed identity to avoid use of connection string in the code.
No, you can't.
The MSI(managed identity) is not for such usage, it is just used for authenticating to azure services that support Azure AD authentication, the AzureWebJobsStorage is used for azure function runtime, in the function app, the property must be specified as an app setting in the site configuration.
This question is similar to Azure Blob Storage trigger Function not firing
However, their problem was that their Azure Function wasn't awaking immediately, giving the impression it wasn't processing triggers from Azure Blob Storage when in fact it was after 10 minutes which is exactly as the MS docs claim.
https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-storage-blob-trigger?tabs=csharp
My problem is different. My blob has been sitting in the container now for 9 hours and it still hasn't been processed.
All it does is post a message onto ServiceBus.
[FunctionName("IncomingFileDetected")]
[return: ServiceBus("incoming-file-received", EntityType = Microsoft.Azure.WebJobs.ServiceBus.EntityType.Topic)]
public static IncomingFile Run(
[BlobTrigger("incoming-files/{filename}", Connection = "ConnectionStrings:MutableStorage")]
Stream contents,
string filename,
ILogger log)
{
log.LogInformation($"Detected new blob file: {filename}");
return new IncomingFile(filename);
}
No messages have appeared in the service bus.
Now, after 9 hours, I have restarted the function app and the blob was processed within about 10 minutes.
Update:
Thanks for Peter Morris's sharing, the problem comes from is service plan is d1. So first make sure you are based on the three kinds of plans: consumption plan, premium plan and app service plan. When we use azure function, even only test, we should use a consumption plan. The smallest in production is S1, which is normally used for testing.
Original Answer:
The below code works fine on my side. Even the consumption plan is no problem.
using System;
using System.IO;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
namespace FunctionApp35
{
public static class Function1
{
[FunctionName("Function1")]
[return: ServiceBus("test", Connection = "ServiceBusConnection")]
public static string Run([BlobTrigger("samples-workitems/{name}", Connection = "str")]Stream myBlob, string name, ILogger log)
{
log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
string a = "111111111111111";
return a;
}
}
}
This is my local settings:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=0730bowmanwindow;AccountKey=lti/ThmF+mw9BebOacp9gVazIh76Q39ecikHSCkaTcGK5hmInspX+EkjzpNmvCPWsnvapWziHQHL+kKt2V+lZw==;EndpointSuffix=core.windows.net",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"str": "DefaultEndpointsProtocol=xxxxxx",
"ServiceBusConnection": "Endpoint=sb://bowmantestxxxxxx"
}
}
The str is from this place:
The ServiceBusConnection is from this place:
And please notice that the blob will will not be removed from container after the azure function is triggered. Also, dont forget to create at least one subscription in your service bus topic.
All all the above also works fine after the function be deployed to azure.(The difference from local is you need to add settings in configuration settings instead of local.settings.json)
I am monitoring a lot of applications in Azure Application Insights.
In all of them I add some custom properties to events, traces etc so I can filter/group in the portal.
Is it possible to add the same custom properties to the built in application insight integration with Azure Functions?
Have read the documentation but can't find anything about it.
Edit:
I maintain a large number of applications hosted in various environments. About 15 of these are Azure Functions.
From all my applications I send telemetry to the same application insights instance via a log handler. To filter/group the information I add "CustomerName" and "CustomerInstance" properties to all events automatically via my log handler.
When I get the standard events from an Azure Function it is difficult to present information in a useful way and correlate it with other events.
With some clever naming of the function apps I can parse the request url in analytics but not in the portal.
You can add these custom properties explicitly using telemetry.Context.Properties.Add() method.
I did a demo with function v2 as below:
1.Create a function v2 in visual studio
2.Then in the visual studio, add Microsoft.ApplicationInsights 2.8.1(latest version) via nuget package manager
3.In your Function.cs, write the following code:
using Microsoft.ApplicationInsights;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using System;
namespace FunctionApp17
{
public static class Function1
{
private static string key = System.Environment.GetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY",EnvironmentVariableTarget.Process);
private static TelemetryClient telemetry = new TelemetryClient() { InstrumentationKey= key };
[FunctionName("Function1")]
public static void Run([TimerTrigger("*/10 * * * * *")]TimerInfo myTimer, ILogger log)
{
if (!telemetry.Context.Properties.ContainsKey("Function_appName"))
{
telemetry.Context.Properties.Add("Function_appName", "myfuncapp111");
}
else
{
telemetry.Context.Properties["Function_appName"] = "myfuncapp111";
}
log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
telemetry.TrackEvent("event111");
telemetry.TrackTrace("trace111");
}
}
}
4.Publish to azure, and in your function app -> Application settings, add the instrumentation key:
5.After the function app is running, nav to your application insights -> search, you can add your filters which defined in your code.
Then you can see the filtered message:
Looks like function.json in your Azure function's zip file defines Az func's trigger. Is it possible to change the trigger from Timer based to Event based programatically? Meaning, can I have a python application running perhaps in my local computer which calls Azure python sdk and says hey, change the Azure Function x's trigger from timer based to event hub based trigger and here is the additional event hub namespace name and connection string information that you need.
This is not possible.
However, I can think of the following workaround, which should accomplish the goal.
First, you can define multiple function triggers within one function project (one function app service). They could be bound to different targets. Something like this:
public static class Functions
{
[FunctionName("FunctionTimer")]
public static async Task RunAsync([TimerTrigger("%Schedule%")]TimerInfo myTimer, ILogger log)
{
if (!AppSettings.IsTimerTriggerActive)
return;
...
}
[FunctionName("FunctionEventHub")]
public static async Task RunAsync([EventHubTrigger("", Connection = "eventHubConnectionString")] EventData[] eventDataBatch, ILogger log)
{
if (!AppSettings.IsEventHubTriggerActive)
return;
...
}
}
You cannot enable/disable a function programmatically, but you can control which one of these is active via the App Service Application Settings. The latter could be managed through the Web App API: https://learn.microsoft.com/en-us/rest/api/appservice/webapps/updateapplicationsettings
Thus you can activate either trigger. One can even skip the (de)activation if that's compatible with the logic.
I am getting an error for an azure function when is deployed on azure where the func cannot find the notification hub connection string settings.
Error indexing method 'NotifyMobiles.Run'Unable to resolve app setting for property 'NotificationHubAttribute.ConnectionStringSetting'. Make sure the app setting exists and has a valid value.
The function signature looks like below and I am getting weird error that I believe is result of being using beta library(Microsoft.Azure.NotificationHubs) and is that the deployed function is looking for connections strings that have prefixed AzureWebJobs even is the connections string is named SERVICEBUS in the app setting. Thus I added the prefix to avoid the error and is finding now the Service bus element but no the Notification Hub
[FunctionName("NotifyMobiles")]
public static async Task Run(
[ServiceBusTrigger("MobileNotifications", AccessRights.Manage, Connection = "SERVICEBUS")] QueueItem queueItem,
TraceWriter log,
[NotificationHub(HubName = "push-notification-hub", ConnectionStringSetting = "NOTIFICATIONHUB", TagExpression = "{Tag}")] IAsyncCollector<Notification> notification)
{}
Please remove the "AzureWebJobs" prefix from "NOTIFICATIONHUB" setting name. It should match ConnectionStringSetting value in your code.
You may delete the connection string key.