From c34d739d87180f9a55640410c3e3602b5a7ac00e Mon Sep 17 00:00:00 2001 From: Daniel Hindrikes Date: Thu, 21 Nov 2024 13:54:44 +0100 Subject: [PATCH] Added DependencyFilter --- TinyInsights.TestApp/MauiProgram.cs | 26 +++++++++++++-------- TinyInsights/ApplicationInsightsProvider.cs | 2 ++ TinyInsights/IInsightsProvider.cs | 2 ++ TinyInsights/Insights.cs | 5 ++++ 4 files changed, 25 insertions(+), 10 deletions(-) diff --git a/TinyInsights.TestApp/MauiProgram.cs b/TinyInsights.TestApp/MauiProgram.cs index 683bdbb..27a51b8 100644 --- a/TinyInsights.TestApp/MauiProgram.cs +++ b/TinyInsights.TestApp/MauiProgram.cs @@ -14,20 +14,26 @@ public static MauiApp CreateMauiApp() fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular"); fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold"); }) - .UseTinyInsights("InstrumentationKey=8b51208f-7926-4b7b-9867-16989206b950;IngestionEndpoint=https://swedencentral-0.in.applicationinsights.azure.com/;ApplicationId=0c04d3a0-9ee2-41a5-996e-526552dc730f"); + .UseTinyInsights("InstrumentationKey=8b51208f-7926-4b7b-9867-16989206b950;IngestionEndpoint=https://swedencentral-0.in.applicationinsights.azure.com/;ApplicationId=0c04d3a0-9ee2-41a5-996e-526552dc730f", (provider) => + { + provider.TrackDependencyFilter = (dependency) => + { + return !dependency.Success; + }; + }); builder.Services.AddSingleton((serviceProvider) => - { - var insights = serviceProvider.GetRequiredService(); - var providers = insights.GetProviders(); +{ + var insights = serviceProvider.GetRequiredService(); + var providers = insights.GetProviders(); - if (providers.Any()) - { - return (ILogger)providers.First(); - } + if (providers.Any()) + { + return (ILogger)providers.First(); + } - throw new InvalidOperationException("No insights provider found"); - }); + throw new InvalidOperationException("No insights provider found"); +}); #if DEBUG builder.Logging.AddDebug(); diff --git a/TinyInsights/ApplicationInsightsProvider.cs b/TinyInsights/ApplicationInsightsProvider.cs index 6867416..4863b57 100644 --- a/TinyInsights/ApplicationInsightsProvider.cs +++ b/TinyInsights/ApplicationInsightsProvider.cs @@ -29,6 +29,8 @@ public class ApplicationInsightsProvider : IInsightsProvider, ILogger public bool IsTrackDependencyEnabled { get; set; } = true; public bool EnableConsoleLogging { get; set; } + public Func<(string DependencyType, string DependencyName, string Data, DateTimeOffset StartTime, TimeSpan Duration, bool Success, int ResultCode, Exception? Exception), bool>? TrackDependencyFilter { get; set; } + #if IOS || MACCATALYST || ANDROID public ApplicationInsightsProvider(string? connectionString = null) diff --git a/TinyInsights/IInsightsProvider.cs b/TinyInsights/IInsightsProvider.cs index 9d3ceb6..3cfd4d4 100644 --- a/TinyInsights/IInsightsProvider.cs +++ b/TinyInsights/IInsightsProvider.cs @@ -9,6 +9,8 @@ public interface IInsightsProvider bool IsTrackEventsEnabled { get; set; } bool IsTrackDependencyEnabled { get; set; } + Func<(string DependencyType, string DependencyName, string Data, DateTimeOffset StartTime, TimeSpan Duration, bool Success, int ResultCode, Exception? Exception), bool>? TrackDependencyFilter { get; set; } + void Initialize(); void UpsertGlobalProperty(string key, string value); diff --git a/TinyInsights/Insights.cs b/TinyInsights/Insights.cs index d75bbbf..9137dd2 100644 --- a/TinyInsights/Insights.cs +++ b/TinyInsights/Insights.cs @@ -99,6 +99,11 @@ public Task TrackDependencyAsync(string dependencyType, string dependencyName, s foreach (var provider in insightsProviders.Where(x => x.IsTrackDependencyEnabled)) { + if (provider.TrackDependencyFilter is not null && !provider.TrackDependencyFilter.Invoke((dependencyType, dependencyName, data, startTime, duration, success, resultCode, exception))) + { + continue; + } + var task = provider.TrackDependencyAsync(dependencyType, dependencyName, data, httpMethod, startTime, duration, success, resultCode, exception); tasks.Add(task); }