Comments (10)
It sounds like logging a warning would be appropriate.
from featuremanagement-dotnet.
How does AppInsights component normally bubble up warnings/errors?
It's happy to log issues when it's not in an initializer or processor- as initializers and processors are hooked into outgoing logs. There's a thread on their github about it microsoft/ApplicationInsights-dotnet#1536 and it appears to still be an issue with no fix eta.
from featuremanagement-dotnet.
So @zhiyuanliang-ms and I looked into this a bit, and were happy to add some logging- however...
Adding a logger here becomes a circular dependency, since AppInsights is built on top of logging:
InvalidOperationException: A circular dependency was detected for the service of type
'Microsoft.Extensions.Logging.ILoggerFactory'.
Microsoft.Extensions.Hosting.IHostApplicationLifetime(Microsoft.Extensions.Hosting.Internal.ApplicationLifetime)
-> Microsoft.Extensions.Logging.ILogger<Microsoft.Extensions.Hosting.Internal.ApplicationLifetime>(Microsoft.Extensions.Logging.Logger<Microsoft.Extensions.Hosting.Internal.ApplicationLifetime>)
-> Microsoft.Extensions.Logging.ILoggerFactory(Microsoft.Extensions.Logging.LoggerFactory)
-> System.Collections.Generic.IEnumerable<Microsoft.Extensions.Logging.ILoggerProvider>
-> Microsoft.Extensions.Logging.ILoggerProvider(Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider)
-> Microsoft.Extensions.Options.IOptions<Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration>(Microsoft.Extensions.DependencyInjection.TelemetryConfigurationOptions)
-> System.Collections.Generic.IEnumerable<Microsoft.Extensions.Options.IConfigureOptions<Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration>>
-> Microsoft.Extensions.Options.IConfigureOptions<Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration>(Microsoft.Extensions.DependencyInjection.TelemetryConfigurationOptionsSetup)
-> System.Collections.Generic.IEnumerable<Microsoft.ApplicationInsights.Extensibility.ITelemetryInitializer>
-> Microsoft.ApplicationInsights.Extensibility.ITelemetryInitializer(Microsoft.FeatureManagement.Telemetry.ApplicationInsights.AspNetCore.TargetingTelemetryInitializer)
-> Microsoft.Extensions.Logging.ILogger<Microsoft.FeatureManagement.Telemetry.ApplicationInsights.AspNetCore.TargetingTelemetryInitializer>(Microsoft.Extensions.Logging.Logger<Microsoft.FeatureManagement.Telemetry.ApplicationInsights.AspNetCore.TargetingTelemetryInitializer>)
-> Microsoft.Extensions.Logging.ILoggerFactory
from featuremanagement-dotnet.
Thanks for looking into it. How does AppInsights component normally bubble up warnings/errors?
from featuremanagement-dotnet.
Did you try httpContext.RequestServices.GetService<ILogger>();
?
from featuremanagement-dotnet.
httpContext.RequestServices.GetService();
@jimmyca15 This returns null.
from featuremanagement-dotnet.
Sorry, should be httpContext.RequestServices.GetService<ILoggerFactory>();
from featuremanagement-dotnet.
httpContext.RequestServices.GetService()
This works.
protected override void OnInitializeTelemetry(HttpContext httpContext, RequestTelemetry requestTelemetry, ITelemetry telemetry)
{
if (telemetry == null)
{
throw new ArgumentNullException(nameof(telemetry));
}
if (httpContext == null)
{
throw new ArgumentNullException(nameof(httpContext));
}
EnsureInit(httpContext);
// Extract the targeting id from the http context
string targetingId = null;
if (httpContext.Items.TryGetValue(TargetingIdKey, out object value))
{
targetingId = value?.ToString();
}
if (!string.IsNullOrEmpty(targetingId))
{
// Telemetry.Properties is deprecated in favor of ISupportProperties
if (telemetry is ISupportProperties telemetryWithSupportProperties)
{
telemetryWithSupportProperties.Properties["TargetingId"] = targetingId;
}
}
else
{
//
// This line will cause StackOverFlowException, if I changed the log level to debug, then no exception will be thrown
_logger.LogWarning("No Targeting Id");
}
}
private void EnsureInit(HttpContext httpContext)
{
if (_initialized == 0)
{
lock (_lock)
{
if (Interlocked.Read(ref _initialized) == 0)
{
_logger = httpContext.RequestServices.GetRequiredService<ILoggerFactory>().CreateLogger<TargetingTelemetryInitializer>();
Interlocked.Exchange(ref _initialized, 1);
}
}
}
}
I tried this pattern. The logger can be initialized. But another issue occurred. There will be tons of _logger.LogWarning("No Targeting Id");
calls and it will eventually throw stackoverflow exception.
It seems like the Application Insights will send telemetry if there is any warning level log. This will cause infinite recursive call of OnInitializeTelemetry
and all of them are without TargetingId
.
https://learn.microsoft.com/en-us/azure/azure-monitor/app/ilogger?tabs=dotnet6
But I found that if we change the log level to debug, everything works fine (which is interesting)
Here is the screenshot of what the additional telemetry of the logwarning call looks like:
It is technically possible to filter such telemetry. A very naive example will be like:
if (!string.IsNullOrEmpty(targetingId))
{
// Telemetry.Properties is deprecated in favor of ISupportProperties
if (telemetry is ISupportProperties telemetryWithSupportProperties)
{
telemetryWithSupportProperties.Properties["TargetingId"] = targetingId;
}
}
else
{
if (telemetry is TraceTelemetry traceTelemetry)
{
if (traceTelemetry.Message.Equals("No Targeting Id"))
{
return;
}
}
_logger.LogWarning("No Targeting Id");
}
What do you think? @jimmyca15 @rossgrambo @zhenlan
from featuremanagement-dotnet.
Thank you for looking into it @zhiyuanliang-ms,
I don't like logging here. While we could get around it in this way- but I think it's a pattern we shouldn't try to break. The short version of this logic is "Initializers will run on all logs", and I don't like trying to get around this idea.
If the log level of debug was reliable, that would be great. However App Insights lets you choose your log level (by default it's Information). This means customers who change it would crash / still be subject to this.
I think the best two options are either throw or silently fail. With the library in it's current state, I think silently failing makes more sense. We're working on adjusting the TargetingContext cache logic and if we end up with a Scoped service, I don't think this is a concern anymore, since the Targeting Context will always exist.
from featuremanagement-dotnet.
The short version of this logic is "Initializers will run on all logs", and I don't like trying to get around this idea.
Agreed
from featuremanagement-dotnet.
Related Issues (20)
- ISessionManager is not mentioned in README
- Suggest to have app.UseFeatureManagement HOT 8
- The TargetingId should be included in the FeatureEvaluation event even if it's null/empty. HOT 4
- Targeting evaluation is CPU dependent. HOT 4
- Remove the support for variants and telemetry from the .NET feature flag schema HOT 14
- Include Targeting Information on outgoing telemetry HOT 2
- Support .NET 8 Targeting framework in Microsoft.FeatureManagement.Telemetry.ApplicationInsights.AspNetCore
- Target netstandard2.* in Microsoft.FeatureManagement.Telemetry.ApplicationInsights
- The approach to telemetry (using custom `ITelemetryPublisher`) seems overengineered and counter to modern OpenTelemetry abstractions HOT 37
- Add Default Http Targeting Context Accessor
- Consider moving `TargetingHttpContextMiddleware` to the App Insights package HOT 2
- No instance of ITargetingContextAccessor is available for variant assignment. HOT 6
- FeatureManagerSnapshot should snapshot TargetingContext as well
- EvaluationDataToApplicationInsights example
- Can't Set FeatureFilters In FeatureManager In .NET Framework Application HOT 7
- Update Package metadata
- Add a link to preview on main readme
- Cannot use the TargetingFilter in .NET class library project HOT 3
- Nuget "Microsoft.FeatureManagement.AspNetCore" Version 3.3.0 - Multiple feature filters match the configured filter named 'Microsoft.TimeWindow' HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from featuremanagement-dotnet.