-
Notifications
You must be signed in to change notification settings - Fork 457
Filter Azure monitor logs when customer does not subscribe for the categories #10982
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you give an example of these categories? Are they regular ILogger categories?
If so, we should use built in filtering with Microsoft.Extensions.Logging. We can either transform configuration, or we can simply call:
Part 1: log level filter
// condition on if this is the right sku
services.AddOptions<LoggerFilterOptions>()
.Configure<IConfiguration>((options, config) =>
{
string setting = config[EnvironmentSettingNames.AzureMonitorCategories];
options.AddFilter<AzureMonitorDiagnosticLoggerProvider>((category, level) =>
{
// return false when AzureMonitorCategories does not contain FunctionAppLogs
});
});
Part 2: IsEnabled update
// AzureMonitorDiagnosticLogger.cs
// Update the below method to return false when AzureMonitorCategories does not contain FunctionAppLogs
public bool IsEnabled(LogLevel logLevel)
{
// We want to instantiate this Logger in placeholder mode to warm it up, but do not want to log anything.
return !string.IsNullOrEmpty(_hostNameProvider.Value) && !_environment.IsPlaceholderModeEnabled();
}
Although I have to ask - who sets EnvironmentSettingNames.AzureMonitorCategories
? Is it from the platform, or does the user explicitly set it?
I agree with @jviau that providing more context about the scenario would be beneficial. |
369f120
to
396bf40
Compare
I have removed all the changes. We already have category filter in the host code. I just had to called it in isenabled class |
@manikantanallagatla I believe you will need to also add the filter callback I suggested. It may seem redundant but they have different purposes:
I also am wondering about the Also this change will affect all skus - is the behavior of that env variable consistent across all skus? |
6f22843
to
14d46af
Compare
14d46af
to
e8a0b4c
Compare
Addressed it |
Addressed it. Made changes to happen only for legion based SKUs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couple suggestions to embrace the options pattern a bit more.
Addressed all comments and updates unit tests as well |
@@ -91,6 +93,18 @@ public static IHostBuilder AddWebScriptHost(this IHostBuilder builder, IServiceP | |||
loggingBuilder.AddWebJobsSystem<SystemLoggerProvider>(); | |||
if (environment.IsAzureMonitorEnabled()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, this is awkward. Going back and forth on this PR and it looks like it is completely unnecessary. We already completely disable AzureMonitor / shoebox by simply not registering the AzureMonitorDiagnosticLoggerProvider
- and this is applicable to all skus.
Which means, is this PR even necessary anymore?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup. I looked at this code and tested the flow. Without the PR, the logs are not getting disabled. I think what was happening is that:
- Legion pods start in placeholder mode and this code is executed at that time. That time, we enable azure monitor.
- When legion pods are specialized, the env variable is set(lets say to None when categories are not subscribed) and we never disable the Azure monitor as we are not doing any options monitor or env variable monitor without the PR.
- Thats why I started with checking for this env variable in is_enabled flow in aAzureMonitorDiagnosticLogger.cs in the PR initially
- Then we went through this options monitor flow which is more efficient.
In summary, specialization is updating the env variable and we need options monitor on that env variable in order to enable/ disable azure monitor logs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you saying AzureMonitor env variable is set during placeholder mode? I would expect it to be the opposite: no env variable set, logger not registered, specialization brings in the azure monitor variable, but it has no effect.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean:
During placeholder mode, WEBSITE_FUNCTIONS_AZUREMONITOR_CATEGORIES env is set to null. it is not set to anything. So, logger is initialized as we are returning true here for null case:
return true; |
It is also mentioned here that the logger is initialized but the logs are filtered out in placeholder mode:
https://github.com/Azure/azure-functions-host/blob/dev/src/WebJobs.Script.WebHost/Diagnostics/AzureMonitorDiagnosticLogger.cs#L58
So, specialization updates the env variable but it has no effect of disabling the logs.
I think we both are saying samething.
As env variable update is not taking effect, we need this PR so we can disable logging when that env variable is set to None.
@@ -54,6 +51,10 @@ public AzureMonitorDiagnosticLogger(string category, string hostInstanceId, IEve | |||
|
|||
public bool IsEnabled(LogLevel logLevel) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any scenario where this could switch from enabled to disabled? I believe that once enabled, it will stay active. Would you like to store the state so that, after activation, all checks can be bypassed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. Customer can unsubscribe the categories which they have subscribed in the past. Then, this would go from enabled to disabled.
In PR, I was initially caching the isenabled value. But, the options monitor in essence handles it for us. We are not doing too much computation like string split or something. We are checking env variables in this change. So, again caching over the options monitor is redundant
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@RohitRanjanMS my understanding of the changes is that this will correctly enable/disable dynamically as the config value is changed (due to the usage of OptionsMonitor
).
With that said, the primary interaction point is an app setting, which when changed will always cause a site restart.
Do you see some issue or concern with how this will enable/disable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’m concerned about the number of environment variable lookups in IsEnabled
, especially since they occur for every log statement. I’ll run a benchmark on this branch to assess the impact.
AzureMonitorDiagnosticLoggerProvider
is added in WebScriptHostBuilderExtension
and isn’t reinitialized during specialization. This is why AppInsights is configured in ScriptHostBuilderExtensions
, ensuring it’s reconfigured when specialization happens (or when host.json is modified).
Here’s what I am thinking:
- Register
AzureMonitorDiagnosticLoggerProvider
in ScriptHost if categories are set. The deferred logger should forward all logs from the root logger toAzureMonitorDiagnosticLogger
. If categories aren’t set,AzureMonitorDiagnosticLoggerProvider
won’t be registered, and the deferred logger won’t forward logs to it. Deferred logger only forwards Error and above so this option will require a change in deferred logger as well. - Determine and cache IsAzureMonitorLoggingEnabled when ScriptHost is built, using it in
IsEnabled
instead of relying on environment variable lookups.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should also disable AzureMonitorDiagnosticLoggerProvider
if categories are not set.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I spoke with @jviau, and here’s our proposed approach:
- Utilize OptionsMonitor to perform the environment variable lookup just once.
- When building ScriptHost, add logic to disable the logger provider if AzureMonitorDiagnosticLogger isn’t needed. Instead of AzureMonitorDiagnosticLogger, return NullLogger.Instance.
return _isEnabled ? new AzureMonitorDiagnosticLogger(categoryName, _hostInstanceId, _eventGenerator, _environment, _scopeProvider, _hostNameProvider, _appServiceOptions) : NullLogger.Instance;
- Can this be applied across all SKUs? This would be a solid improvement.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for checking with Jacob.
Will try the first two things.
Regarding Can this be applied across all SKUs?
-> We can do it if we have confidence on the change blast radius. As it is logging change which will have direct customer impact in case it has issues. Can we do for legion SKUs for now and add a task to remove legion skus check after deployments and change is working fine? That way we will reduce impact in case of issues.
CC: @vivekjilla
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, we can do this later.
Issue describing the changes in this PR
For a few SKUs in functions, we want to filter azure monitor logs based on the categories subscribed by the user. This PR checks for the env variable for the categories and skips logging.
Pull request checklist
IMPORTANT: Currently, changes must be backported to the
in-proc
branch to be included in Core Tools and non-Flex deployments.in-proc
branch is not requiredrelease_notes.md
Additional information
Additional PR information