Skip to content

Commit

Permalink
Merge pull request #124 from kamranayub/107-ravendb3
Browse files Browse the repository at this point in the history
[Closes #107] Add RavenDB 3.0 compatibility
  • Loading branch information
NickCraver committed May 20, 2016
2 parents d0c30ec + 28c8d83 commit 0b8d214
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 9 deletions.
71 changes: 62 additions & 9 deletions StackExchange.Profiling.RavenDb/RavenMiniProfiler.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,88 @@
namespace StackExchange.Profiling.RavenDb
namespace StackExchange.Profiling.RavenDb
{
using System;
using System.Text;
using System.Text.RegularExpressions;
using Raven.Client.Document;
using Raven.Client.Connection.Profiling;
using System.Web;

public class MiniProfilerRaven
{
private static readonly Regex IndexQueryPattern = new Regex(@"/indexes/[A-Za-z/]+");
private const string RavenHandledRequestMarker = "__MiniProfiler.Raven_handled";
private const string RavenRequestPrefix = "__MiniProfiler.Raven.Request.";
private const string RavenRequestPending = "Pending";
private const string RavenRequestHandled = "Handled";

private static readonly Regex IndexQueryPattern = new Regex(@"/indexes/[A-Za-z/]+");

/// <summary>
/// Initialize MiniProfilerRaven for the given DocumentStore (only call once!)
/// </summary>
/// <param name="store">The <see cref="DocumentStore"/> to attach to</param>
public static void InitializeFor(DocumentStore store)
{

if (store != null && store.JsonRequestFactory != null)
store.JsonRequestFactory.LogRequest += (sender, r) => IncludeTiming(r);
{
store.JsonRequestFactory.ConfigureRequest += (sender, args) =>
{
EventHandler<RequestResultArgs> handler = null;

var profiler = MiniProfiler.Current;
var httpContext = HttpContext.Current;

if (profiler != null && profiler.Head != null && httpContext != null)
{
var requestId = Guid.NewGuid();

// assign a unique request ID to this context since
// HttpContext may be shared across events, due to singleton-nature
// of DocumentStore
if (!httpContext.Items.Contains(RavenRequestPrefix + requestId))
{
httpContext.Items[RavenRequestPrefix + requestId] = RavenRequestPending;
}

handler = (s, r) =>
{
store.JsonRequestFactory.LogRequest -= handler;

// add a "handled" marker because not every ConfigureRequest event is for a single query
// so if we've already timed this request, ignore otherwise we will have dup timings
if (r.AdditionalInformation.ContainsKey(RavenHandledRequestMarker))
return;

// have we handled this request on this context?
if ((string)httpContext.Items[RavenRequestPrefix + requestId] == RavenRequestHandled)
return;

// add handled marker to request
r.AdditionalInformation.Add(RavenHandledRequestMarker, "");

// mark this request as handled on this context
httpContext.Items[RavenRequestPrefix + requestId] = RavenRequestHandled;

// add custom timing
IncludeTiming(r, profiler);
};

store.JsonRequestFactory.LogRequest += handler;
}
};
}

}

private static void IncludeTiming(RequestResultArgs request)
private static void IncludeTiming(RequestResultArgs request, MiniProfiler profiler)
{
if (MiniProfiler.Current == null || MiniProfiler.Current.Head == null)
if (profiler == null || profiler.Head == null)
{
return;

}

var formattedRequest = JsonFormatter.FormatRequest(request);

MiniProfiler.Current.Head.AddCustomTiming("raven", new CustomTiming(MiniProfiler.Current, BuildCommandString(formattedRequest))
profiler.Head.AddCustomTiming("raven", new CustomTiming(profiler, BuildCommandString(formattedRequest))
{
Id = Guid.NewGuid(),
DurationMilliseconds = (decimal)formattedRequest.DurationMilliseconds,
Expand Down Expand Up @@ -108,4 +161,4 @@ private static string FormatQuery(string url)
}

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<Reference Include="System" />
<Reference Include="System.ComponentModel.Composition" />
<Reference Include="System.Core" />
<Reference Include="System.Web" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand All @@ -65,6 +66,12 @@
<ItemGroup>
<None Include="packages.config" />
<None Include="README.md" />
<None Include="Scripts\jquery-2.2.0.min.map" />
</ItemGroup>
<ItemGroup>
<Content Include="Scripts\jquery-2.2.0.intellisense.js" />
<Content Include="Scripts\jquery-2.2.0.js" />
<Content Include="Scripts\jquery-2.2.0.min.js" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down

0 comments on commit 0b8d214

Please sign in to comment.