Skip to content

Commit

Permalink
Added DependencyFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
dhindrik committed Nov 21, 2024
1 parent 900c4ee commit c34d739
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 10 deletions.
26 changes: 16 additions & 10 deletions TinyInsights.TestApp/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ILogger>((serviceProvider) =>
{
var insights = serviceProvider.GetRequiredService<IInsights>();
var providers = insights.GetProviders();
{
var insights = serviceProvider.GetRequiredService<IInsights>();
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();
Expand Down
2 changes: 2 additions & 0 deletions TinyInsights/ApplicationInsightsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions TinyInsights/IInsightsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions TinyInsights/Insights.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit c34d739

Please sign in to comment.