Skip to content

Commit

Permalink
MiniProfiler v4
Browse files Browse the repository at this point in the history
Upgrading MiniProfiler to dogfood it. Also cleans up and adds options around profiling and logging...these were just oddly placed before.
  • Loading branch information
NickCraver committed Oct 14, 2017
1 parent 2d6a4b4 commit 9b048a5
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 162 deletions.
30 changes: 20 additions & 10 deletions Opserver.Core/Data/Cache.cs
Expand Up @@ -4,8 +4,8 @@
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using StackExchange.Opserver.Monitoring;
using StackExchange.Profiling;
using StackExchange.Profiling.Storage;

namespace StackExchange.Opserver.Data
{
Expand Down Expand Up @@ -108,6 +108,12 @@ private async Task<T> UpdateAsync(bool force)

private string MiniProfilerDescription { get; }

private static readonly MiniProfilerOptions _profilerOptions = new MiniProfilerOptions
{
Storage = new NullStorage(),
ProfilerProvider = new DefaultProfilerProvider()
};

/// <summary>
/// Creates a cache poller
/// </summary>
Expand All @@ -129,24 +135,25 @@ private async Task<T> UpdateAsync(bool force)
TimeSpan cacheDuration,
Func<Task<T>> getData,
int? timeoutMs = null,
bool logExceptions = false, // TODO: Settings
bool? logExceptions = null,
Action<Exception> addExceptionData = null,
Action<Cache<T>> afterPoll = null,
[CallerMemberName] string memberName = "",
[CallerFilePath] string sourceFilePath = "",
[CallerLineNumber] int sourceLineNumber = 0)
: base(cacheDuration, memberName, sourceFilePath, sourceLineNumber)
{
MiniProfilerDescription = "Poll: " + description; // contcat once
MiniProfilerDescription = "Poll: " + description; // concatenate once
logExceptions = logExceptions ?? LogExceptions;

_updateFunc = async () =>
{
var success = true;
PollStatus = "UpdateCacheItem";
if (OpserverProfileProvider.EnablePollerProfiling)
if (EnableProfiling)
{
Profiler = OpserverProfileProvider.CreateContextProfiler(MiniProfilerDescription, UniqueId,
store: false);
Profiler = _profilerOptions.StartProfiler(MiniProfilerDescription);
Profiler.Id = UniqueId;
}
using (MiniProfiler.Current.Step(description))
{
Expand All @@ -165,7 +172,7 @@ private async Task<T> UpdateAsync(bool force)
}
else
{
// This means the whenany returned the timeout first...boom.
// This means the .WhenAny returned the timeout first...boom.
throw new TimeoutException($"Fetch timed out after {timeoutMs} ms.");
}
}
Expand All @@ -181,7 +188,7 @@ private async Task<T> UpdateAsync(bool force)
catch (Exception e)
{
success = false;
if (logExceptions)
if (logExceptions.Value)
{
addExceptionData?.Invoke(e);
Current.LogException(e);
Expand All @@ -200,9 +207,9 @@ private async Task<T> UpdateAsync(bool force)
}
owner.PollComplete(this, success);
}
if (OpserverProfileProvider.EnablePollerProfiling)
if (EnableProfiling)
{
OpserverProfileProvider.StopContextProfiler();
Profiler.Stop();
}
PollStatus = "UpdateCacheItem Complete";
return Data;
Expand Down Expand Up @@ -282,6 +289,9 @@ public abstract class Cache : IMonitorStatus
/// </summary>
public MiniProfiler Profiler { get; protected set; }

public static bool EnableProfiling { get; set; }
public static bool LogExceptions { get; set; }

internal void SetSuccess()
{
LastSuccess = LastPoll = DateTime.UtcNow;
Expand Down
6 changes: 3 additions & 3 deletions Opserver.Core/Data/PollNode.Equality.cs
Expand Up @@ -4,7 +4,7 @@ public partial class PollNode
{
public bool Equals(PollNode other)
{
if (ReferenceEquals(null, other)) return false;
if (other is null) return false;
if (ReferenceEquals(this, other)) return true;
return other.GetType() == GetType() && string.Equals(UniqueKey, other.UniqueKey);
}
Expand All @@ -17,9 +17,9 @@ public bool Equals(PollNode other)

public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (obj is null) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
if (obj.GetType() != GetType()) return false;
return Equals((PollNode)obj);
}
}
Expand Down
7 changes: 4 additions & 3 deletions Opserver.Core/Monitoring/LightweightProfiler.cs
Expand Up @@ -4,6 +4,7 @@
using System.Diagnostics;
using StackExchange.Profiling;
using StackExchange.Profiling.Data;
using StackExchange.Profiling.SqlFormatters;

namespace StackExchange.Opserver.Monitoring
{
Expand Down Expand Up @@ -57,11 +58,11 @@ public void ReaderFinish(IDataReader reader)
_wrapped?.ReaderFinish(reader);
}

private static readonly SqlServerFormatter _sqlFormatter = new SqlServerFormatter();

public void OnError(IDbCommand profiledDbCommand, SqlExecuteType executeType, Exception exception)
{
var formatter = new Profiling.SqlFormatters.SqlServerFormatter();
var parameters = SqlTiming.GetCommandParameters(profiledDbCommand);
exception.Data["SQL"] = formatter.FormatSql(profiledDbCommand.CommandText, parameters);
exception.Data["SQL"] = _sqlFormatter.GetFormattedSql(profiledDbCommand);
}

public bool IsActive => true;
Expand Down
20 changes: 0 additions & 20 deletions Opserver.Core/Monitoring/MiniProfilerNullStorage.cs

This file was deleted.

86 changes: 0 additions & 86 deletions Opserver.Core/Monitoring/OpserverProfileProvider.cs

This file was deleted.

6 changes: 2 additions & 4 deletions Opserver.Core/Opserver.Core.csproj
Expand Up @@ -36,9 +36,9 @@
<ItemGroup>
<PackageReference Include="Dapper" Version="1.50.4-alpha1-00070" />
<PackageReference Include="Jil" Version="2.15.4" />
<PackageReference Include="MiniProfiler" Version="3.2.0.157" />
<PackageReference Include="MiniProfiler" Version="4.0.0-alpha8-200" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
<PackageReference Include="StackExchange.Exceptional" Version="2.0.0-alpha3-00180" />
<PackageReference Include="StackExchange.Exceptional" Version="2.0.0-alpha3-00182" />
<PackageReference Include="StackExchange.Redis" Version="1.2.6" />
<PackageReference Include="UnconstrainedMelody" Version="0.2.1" />
<Reference Include="Microsoft.CSharp" />
Expand Down Expand Up @@ -273,8 +273,6 @@
</Compile>
<Compile Include="Data\SQL\SQLServerVersions.cs" />
<Compile Include="Monitoring\LightweightProfiler.cs" />
<Compile Include="Monitoring\MiniProfilerNullStorage.cs" />
<Compile Include="Monitoring\OpserverProfileProvider.cs" />
<Compile Include="Monitoring\PerfCounters.cs" />
<Compile Include="Data\Dashboard\Application.cs" />
<Compile Include="Data\Dashboard\DashboardCategory.cs" />
Expand Down
1 change: 0 additions & 1 deletion Opserver/Controllers/HomeController.cs
Expand Up @@ -20,7 +20,6 @@ public ActionResult Home()
[Route("top-refresh")]
public ActionResult TopRefresh(string tab)
{
MiniProfiler.Stop(discardResults: true);
TopTabs.CurrentTab = tab;

var vd = new TopRefreshModel
Expand Down
38 changes: 18 additions & 20 deletions Opserver/Global.asax.cs
Expand Up @@ -8,10 +8,10 @@
using System.Web.Optimization;
using System.Web.Routing;
using StackExchange.Opserver.Data;
using StackExchange.Opserver.Monitoring;
using StackExchange.Profiling;
using StackExchange.Opserver.Helpers;
using StackExchange.Profiling.Mvc;
using StackExchange.Profiling.Storage;

namespace StackExchange.Opserver
{
Expand Down Expand Up @@ -81,35 +81,33 @@ protected void Application_End()

private static void SetupMiniProfiler()
{
MiniProfiler.Settings.RouteBasePath = "~/profiler/";
MiniProfiler.Settings.PopupRenderPosition = RenderPosition.Left;
var paths = MiniProfiler.Settings.IgnoredPaths.ToList();
paths.Add("/login");
paths.Add("/spark");
MiniProfiler.Settings.IgnoredPaths = paths.ToArray();
MiniProfiler.Settings.PopupMaxTracesToShow = 5;
MiniProfiler.Settings.ProfilerProvider = new OpserverProfileProvider();
MiniProfiler.Settings.Storage = new MiniProfilerCacheStorage(TimeSpan.FromMinutes(10));
OpserverProfileProvider.EnablePollerProfiling = SiteSettings.PollerProfiling;

var copy = ViewEngines.Engines.ToList();
ViewEngines.Engines.Clear();
foreach (var item in copy)
var options = MiniProfiler.Configure(new MiniProfilerOptions()
{
ViewEngines.Engines.Add(new ProfilingViewEngine(item));
}
RouteBasePath = "~/profiler/",
PopupRenderPosition = RenderPosition.Left,
PopupMaxTracesToShow = 5,
Storage = new MiniProfilerCacheStorage(TimeSpan.FromMinutes(10)),
ProfilerProvider = new AspNetRequestProvider(true)
}.IgnorePath("/graph")
.IgnorePath("/login")
.IgnorePath("/spark")
.IgnorePath("/top-refresh")
.AddViewPofiling()
);

Cache.EnableProfiling = SiteSettings.PollerProfiling;
Cache.LogExceptions = SiteSettings.LogPollerExceptions;
}

protected void Application_BeginRequest()
{
if (ShouldProfile())
MiniProfiler.Start();
MiniProfiler.StartNew();
}

protected void Application_EndRequest()
{
if (ShouldProfile())
MiniProfiler.Stop();
MiniProfiler.Current?.Stop();
}

private static void GetCustomErrorData(Exception ex, Dictionary<string, string> data)
Expand Down
14 changes: 7 additions & 7 deletions Opserver/Helpers/MiniProfilerCacheStorage.cs
@@ -1,16 +1,16 @@
using System;
using StackExchange.Opserver.Data;
using StackExchange.Opserver.Data;
using StackExchange.Profiling;
using StackExchange.Profiling.Storage;
using System;
using System.Threading.Tasks;

namespace StackExchange.Opserver.Helpers
{
public class MiniProfilerCacheStorage : HttpRuntimeCacheStorage, IStorage
public class MiniProfilerCacheStorage : MemoryCacheStorage, IAsyncStorage
{
public MiniProfilerCacheStorage(TimeSpan cacheDuration) : base(cacheDuration)
{
}
public MiniProfilerCacheStorage(TimeSpan cacheDuration) : base(cacheDuration) {}

MiniProfiler IStorage.Load(Guid id) => Load(id) ?? PollingEngine.GetCache(id)?.Profiler;
MiniProfiler IAsyncStorage.Load(Guid id) => Load(id) ?? PollingEngine.GetCache(id)?.Profiler;
Task<MiniProfiler> IAsyncStorage.LoadAsync(Guid id) => LoadAsync(id) ?? Task.FromResult(PollingEngine.GetCache(id)?.Profiler);
}
}
2 changes: 1 addition & 1 deletion Opserver/Opserver.csproj
Expand Up @@ -45,7 +45,7 @@
<PackageReference Include="Microsoft.AspNet.Mvc" Version="5.2.3" />
<PackageReference Include="Microsoft.AspNet.Web.Optimization" Version="1.1.3" />
<PackageReference Include="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" Version="1.0.7.0" />
<PackageReference Include="MiniProfiler.MVC4" Version="3.0.11" />
<PackageReference Include="MiniProfiler.Mvc5" Version="4.0.0-alpha8-200" />
<PackageReference Include="WebGrease" Version="1.6.0" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System" />
Expand Down
6 changes: 4 additions & 2 deletions Opserver/SiteSettings.cs
Expand Up @@ -14,13 +14,15 @@ public static class SiteSettings

public static bool PollerProfiling => bool.TryParse(ConfigurationManager.AppSettings["PollerProfiling"], out bool setting) && setting;

public static bool LogPollerExceptions => bool.TryParse(ConfigurationManager.AppSettings["LogPollerExceptions"], out bool setting) && setting;

/// <summary>
/// Semilcolon delimited list of groups that can administer the entire site, exceptions, HAProxy servers, etc.
/// Semicolon delimited list of groups that can administer the entire site, exceptions, HAProxy servers, etc.
/// </summary>
public static string AdminGroups => ConfigurationManager.AppSettings["AdminGroups"].IsNullOrEmptyReturn("");

/// <summary>
/// Semilcolon delimited list of groups that can view the entire site, exceptions, HAProxy servers, etc.
/// Semicolon delimited list of groups that can view the entire site, exceptions, HAProxy servers, etc.
/// </summary>
public static string ViewGroups => ConfigurationManager.AppSettings["ViewGroups"].IsNullOrEmptyReturn("");

Expand Down

0 comments on commit 9b048a5

Please sign in to comment.