I have Function App and Application Insight services. I've noticed that amount of data which my application sends is big and generates big costs. Can I disable/completely stop sending data to AI without deleting APPINSIGHTS_INSTRUMENTATIONKEY or APPLICATIONINSIGHTS_CONNECTION_STRING? But of course I want keep the both services alive.
Should host.json be configured in some way?
Here is the workaround I did for Optimizing the cost generated by Application Insights Logs:
To minimize the number of logs, you can use the higher logging level of logs in host.json as you can see in the below screenshot:
As you can see the minimization of logs here function information log is not produced and the output log is shown in the browser, only the manual logging is shown in the logs/terminal.
And the other ways of reducing the logs and optimizing the AI Cost for Azure Functions:
- Disable unneeded modules: Edit the ApplicationInsights.config to turn off collection modules which are not required.
- Disable telemetry dynamically: To disable telemetry conditionally and dynamically anywhere in the code, set the DisableTelemetry flag on it using TelemetryConfiguration instance.
This code sample prevents the sending of telemetry to Application Insights but not the automatic collection modules from colleting telemetry and in order to remove auto collection modules also, please refer this Microsoft Documentation.
- . Customize Logs Collection:
{
"Logging": {
"LogLevel": {
"Default": "Warning"
},
"ApplicationInsights": {
"LogLevel": {
"Default": "Information"
}
}
}
}
The following above configuration allows Application Insights to capture all Information logs and severe warning logs. To change this behavior, explicitly override the logging configuration for the provider ApplicationInsights as shown below:
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
}
}
There are few more techniques to manage the data volume used for telemetry data optimizing also like:
Sampling:
Daily Cap:
Pre-aggregate metrics
Please check these references for more information:
Resolve if logs shows twice in Application Insights
Optimizing logging costs of Azure Functions
Configuring or removing the necessary Telemetry Initializers
Also, Please visit my practical workarounds (Ref1, Ref2)to reduce the unnecessary logs and optimize the cost.
Related
I have been trying to filter out information messages that my ServiceBus triggered webjob is sending in the Application Insights. These messages consist of these two logs:
ReceiveBatchAsync start. MessageCount = 1
ReceiveBatchAsync done. Received '0' messages. LockTokens =
My goal is to only log information traces that I am logging in my code and ignore the logs coming from Azure.Messaging.ServiceBus. How can I achieve this?
So far I have tried to add a filter using the following code in my program.cs file
b.AddFilter("Azure.Messaging.ServiceBus", LogLevel.Warning);
and I have also tried to add the following settings in my appsettings.json file
"Logging": {
"LogLevel": {
"Default": "Information",
"Azure.Messaging.ServiceBus": "Warning"
}
},
As for my set up I am using the following packages which are of concern:
Microsoft.Extensions.Logging.Console
Microsoft.Azure.WebJobs.Extensions.ServiceBus
The following code is my Logging configuration in the program.cs file.
I had the same issue to filter some Service Bus or EFCore logs.
I was able to partially solve this adding some hardcoded filter into the logging configuration code :
builder.ConfigureLogging((context, b) => {
// If the key exists in settings, use it to enable Application Insights.
string instrumentationKey = context.Configuration["EASY_APPINSIGHTS_INSTRUMENTATIONKEY"];
if (!string.IsNullOrEmpty(instrumentationKey)) {
b.AddApplicationInsightsWebJobs(o => o.InstrumentationKey = instrumentationKey);
}
b.AddConsole();
b.AddFilter("Azure.Messaging.ServiceBus", LogLevel.Error);
b.AddFilter("Microsoft.EntityFrameworkCore", LogLevel.Error);
});
But i would like to know how can i setup logging just in updating settings from AppService's settings.
I have a .Net Framework 4.5.1 app service that has the following code:
Console.WriteLine("🔥 Console.WriteLine");
Trace.WriteLine("🔥 Trace.WriteLine");
Trace.TraceInformation("🔥 Trace.TraceInformation");
Trace.TraceWarning("🔥 Trace.TraceWarning");
Trace.TraceError("🔥 Trace.TraceError");
I would like to be able to see logs that we log into Azure when the app is in production. So I enabled Application Service Logging for my web app:
As you can see, I have the logging level set to verbose because we want to see everything. I also have Application Insights turned on for my app. But when I check the logs, all I see are the logs at the warning level and higher.
I checked in the "logs" tab:
I also checked in my blob storage. I see logs, but they're all on a warning level or higher.
There was one way I got this to work, and that's using the feature that saves logs to a file system. But that feature turns itself off after 12 hours, and the file might get too big anyways, and I'd like to use Azure Insights since it has a nicer interface.
How can I view all my logs my application outputs over a long period of time?
I usually just remove all logging configuration from config files like appsettings.json and use the ApplicationInsightsLoggerProvider type specifier in Startup.cs, like so:
public void ConfigureServices(IServiceCollection services)
{
services.AddLogging(conf =>
{
conf.AddApplicationInsights();
conf.AddFilter<ApplicationInsightsLoggerProvider>("", LogLevel.Information);
conf.AddFilter<ApplicationInsightsLoggerProvider>("Microsoft", LogLevel.Warning);
conf.AddFilter<ApplicationInsightsLoggerProvider>("System", LogLevel.Warning);
});
...
}
It's documented here.
If you have various trace levels in your web app but are only interested in having certain levels of logs sent to the logging endpoint, you can set a filter for the minimum level in your application settings under Configuration. By default, even without the app setting, the minimum trace level is set to Warning.
How to set App setting for AppServiceAppLogs level
The application setting name will be APPSERVICEAPPLOGS_TRACE_LEVEL and the value will be the minimum level (ie. Error, Warning, Verbose, etc.). Refer to TraceLevel for more info.
Links
https://learn.microsoft.com/en-us/azure/app-service/troubleshoot-diagnostic-logs
https://azure.github.io/AppService/2020/08/31/AzMon-AppServiceAppLogs.html (Image Source)
I need to log information from all invocations, successful or not, of a function app in Azure. I first tried just using log.LogInformation() and found that messages were not being written from all function invocations. Doing some research I got to understand that in high load scenarios (mine is a high load scenario), sometimes the runtime decides not to log some of the successful invocations. Fair enough.
I then tried using custom events to do logging and capture the info I needed:
TelemetryConfiguration config = TelemetryConfiguration.CreateDefault();
TelemetryClient tc = new TelemetryClient(config);
Dictionary<string, string> props = new Dictionary<string, string>();
props["msgid"] = msgid;
tc.TrackEvent("MsgToBenefitsService", props);
Still no luck, in some runs I did, I saw only 82 rows in app insights from 1000 invocations. I haven't been able to find any documentation saying that Custom Events might not be logged, so I expected that I would see 1000 events logged for 1000 invocations.
Is there anything wrong with the logging code above ? And are there any options to guarantee that I can write information from an invocation to AppInsights ? Or am I stuck with having to explicitly log myself from the function app ?
As background, this function app has a service bus trigger to read messages off a topic. I'm using v3 of the runtime.
Any help would be appreciated.
Thanks.
Please disable sampling in host.json:
"applicationInsights": {
"samplingSettings": {
"isEnabled": false
}
}
applicationInsights.samplingSettings
Sampling in Application Insights
I have a 3-week-old Function on the v2 SDK and uses the out-box App Insights support, i.e. just set the environment variable and ...magic.
I did read that by default it doesn't send Information level logs to AppInsights and this looks correct because I can't see any of my information tracing but I do see occasional errors I've logged out with my custom message.
But for metrics, its broken. I can see some semblance of my telemetry in the logs, sort of. It's strange.
Look.
Now look at these curious logs.
...see those n/a that match the LogMetric lines in my code?
And when I search explicitly for one of them, nothing is found.
And if I drill down on those n/a logs.
...they're pretty empty.
Is it me or is this all rather fishy?
How do I log metrics from a Function in a way that works?
First, for the metric, do you mean custom metrics? If yes, then you should find it in the customMetrics table in your app insights.
Second, what's the method are you using to send metrics? From your code, what's the implementation of this.Logger?.LogMetric method?
For sending metrics, you can use TrackMetric(just tested with this method, and works) or GetMetric method.
A sample code use TrackMetric(deprecated, but still can use) / GetMetric(recommended) method:
TelemetryConfiguration.Active.InstrumentationKey = "your key";
TelemetryClient client = new TelemetryClient();
client.TrackMetric("my metric", 60);
client.GetMetric("my metric").TrackValue(89);
client.Flush();
In my case, the settings in the host.json file were excluding the metrics from getting to Application Insights. The metrics are being logged at the Information level and I originally had mine configured for the Warning level.
"logging": {
"logLevel": {
"Function.<function name>": "Information", //<--won't see metrics if set to Warning
...
See here for more information on configuring the logging levels.
I have an Azure function that's connected to an App Insights instance. The function app emits log messages which I can see in the log stream in the Azure portal, and as App Insights traces.
I've increased the console log level to Verbose by adding a "tracing" element to host.json (https://github.com/Azure/azure-webjobs-sdk-script/wiki/host.json), so Verbose level messages show up in the log stream (in both the function page in the Azure portal, and in Kudu), but I can't get Verbose level traces to appear in App Insights.
Does anyone know how to get App Insights to show Verbose level traces from an Azure Function? Is it even possible? (Info traces and above are showing up just fine in App Insights)
You have a lot of control over your log levels for App Insights in Functions, but you don't use the tracing element for these. We're working on pulling the docs together in one cohesive location, but here's some links that can help:
The new logger.categoryLevel host.json settings: https://github.com/Azure/Azure-Functions/wiki/App-Insights-(Preview)#hostjson-settings
The WebJobs documentation, which gives a bit more detail on how the category filter works (behind the scenes, the host.json settings are serialized into this): https://github.com/Azure/azure-webjobs-sdk/wiki/Application-Insights-Integration#filtering
For your specific example, you can open up all the Debug logs (which matches Verbose in TraceWriter) with this in your host.json:
{
"logger": {
"categoryFilter": {
"defaultLevel": "Debug"
}
}
}
If you just want to see the verbose logs coming from your Function itself (i.e. you don't want the host's verbose logs appearing), you can restrict that with this -- which says 'for logs with the "Function" category (which is the category that function logs use), show me everything with Debug or higher log level':
{
"logger": {
"categoryFilter": {
"categoryLevels": {
"Function": "Debug"
}
}
}
}