Browse Source

add slow query logging

pull/2745/head
Jason Dove 1 day ago
parent
commit
3319d29bcb
No known key found for this signature in database
  1. 3
      CHANGELOG.md
  2. 7
      ErsatzTV.Core/SystemEnvironment.cs
  3. 24
      ErsatzTV/SlowQueryInterceptor.cs
  4. 16
      ErsatzTV/Startup.cs

3
CHANGELOG.md

@ -34,6 +34,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). @@ -34,6 +34,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- This button will extract up to 30 seconds of the media item and zip it
- Add `Target Loudness` (LUFS/LKFS) to ffmpeg profile when loudness normalization is enabled
- Default value is `-16`; some sources normalize to a quieter value, e.g. `-24`
- Add environment variables to help troubleshoot performance
- `ETV_SLOW_QUERY_MS` - milliseconds threshold for logging slow database queries (at DEBUG level)
- e.g. if this is set to `1000`, queries taking longer than 1 second will be logged
### Fixed
- Fix startup on systems unsupported by NvEncSharp

7
ErsatzTV.Core/SystemEnvironment.cs

@ -36,6 +36,12 @@ public class SystemEnvironment @@ -36,6 +36,12 @@ public class SystemEnvironment
}
MaximumUploadMb = maximumUploadMb;
string slowQueryMsVariable = Environment.GetEnvironmentVariable("ETV_SLOW_QUERY_MS");
if (int.TryParse(slowQueryMsVariable, out int slowQueryMs))
{
SlowQueryMs = slowQueryMs;
}
}
public static string BaseUrl { get; }
@ -45,4 +51,5 @@ public class SystemEnvironment @@ -45,4 +51,5 @@ public class SystemEnvironment
public static int StreamingPort { get; }
public static bool AllowSharedPlexServers { get; }
public static int MaximumUploadMb { get; }
public static int? SlowQueryMs { get; }
}

24
ErsatzTV/SlowQueryInterceptor.cs

@ -0,0 +1,24 @@ @@ -0,0 +1,24 @@
using System.Data.Common;
using Microsoft.EntityFrameworkCore.Diagnostics;
namespace ErsatzTV;
public class SlowQueryInterceptor(int threshold) : DbCommandInterceptor
{
public override ValueTask<DbDataReader> ReaderExecutedAsync(
DbCommand command,
CommandExecutedEventData eventData,
DbDataReader result,
CancellationToken cancellationToken = default)
{
if (eventData.Duration.TotalMilliseconds > threshold)
{
Serilog.Log.Logger.Debug(
"[SLOW QUERY] ({Milliseconds}ms): {Command}",
eventData.Duration.TotalMilliseconds,
command.CommandText);
}
return base.ReaderExecutedAsync(command, eventData, result, cancellationToken);
}
}

16
ErsatzTV/Startup.cs

@ -408,6 +408,12 @@ public class Startup @@ -408,6 +408,12 @@ public class Startup
var sqliteConnectionString = $"Data Source={FileSystemLayout.DatabasePath};foreign keys=true;";
string mySqlConnectionString = Configuration.GetValue<string>("MySql:ConnectionString");
SlowQueryInterceptor interceptor = null;
if (SystemEnvironment.SlowQueryMs.HasValue)
{
interceptor = new SlowQueryInterceptor(SystemEnvironment.SlowQueryMs.Value);
}
services.AddDbContext<TvContext>(
options =>
{
@ -438,6 +444,11 @@ public class Startup @@ -438,6 +444,11 @@ public class Startup
}
);
}
if (interceptor != null)
{
options.AddInterceptors(interceptor);
}
},
ServiceLifetime.Scoped,
ServiceLifetime.Singleton);
@ -467,6 +478,11 @@ public class Startup @@ -467,6 +478,11 @@ public class Startup
}
);
}
if (interceptor != null)
{
options.AddInterceptors(interceptor);
}
});
if (databaseProvider == Provider.Sqlite.Name)

Loading…
Cancel
Save