Skip to content

Commit

Permalink
Added TracingThrottleLogger
Browse files Browse the repository at this point in the history
  • Loading branch information
CraigKennedy committed Dec 18, 2013
1 parent 71689a7 commit b675555
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
2 changes: 2 additions & 0 deletions WebApiThrottle/ThrottleLogEntry.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

Expand All @@ -17,5 +18,6 @@ public class ThrottleLogEntry
public long RateLimit { get; set; }
public string RateLimitPeriod { get; set; }
public DateTime LogDate { get; set; }
public HttpRequestMessage Request { get; set; }
}
}
7 changes: 4 additions & 3 deletions WebApiThrottle/ThrottlingHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage reques
if (rateLimit > 0 && throttleCounter.TotalRequests > rateLimit)
{
//log blocked request
if (Logger != null) Logger.Log(ComputeLogEntry(requestId, identity, throttleCounter, rateLimitPeriod.ToString(), rateLimit));
if (Logger != null) Logger.Log(ComputeLogEntry(requestId, identity, throttleCounter, rateLimitPeriod.ToString(), rateLimit, request));

//break execution and return 409
var message = string.IsNullOrEmpty(QuotaExceededMessage) ?
Expand Down Expand Up @@ -237,7 +237,7 @@ private Task<HttpResponseMessage> QuotaExceededResponse(HttpRequestMessage reque
return Task.FromResult(request.CreateResponse(HttpStatusCode.Conflict, message));
}

private ThrottleLogEntry ComputeLogEntry(string requestId, RequestIndentity identity, ThrottleCounter throttleCounter, string rateLimitPeriod, long rateLimit)
private ThrottleLogEntry ComputeLogEntry(string requestId, RequestIndentity identity, ThrottleCounter throttleCounter, string rateLimitPeriod, long rateLimit, HttpRequestMessage request)
{
return new ThrottleLogEntry
{
Expand All @@ -249,7 +249,8 @@ private ThrottleLogEntry ComputeLogEntry(string requestId, RequestIndentity iden
RateLimitPeriod = rateLimitPeriod,
RequestId = requestId,
StartPeriod = throttleCounter.Timestamp,
TotalRequests = throttleCounter.TotalRequests
TotalRequests = throttleCounter.TotalRequests,
Request = request
};
}
}
Expand Down
24 changes: 24 additions & 0 deletions WebApiThrottle/TracingThrottleLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Web.Http.Tracing;

namespace WebApiThrottle
{
public class TracingThrottleLogger : IThrottleLogger
{
private readonly ITraceWriter traceWriter;

public TracingThrottleLogger(ITraceWriter traceWriter)
{
this.traceWriter = traceWriter;
}

public void Log(ThrottleLogEntry entry)
{
if (null != traceWriter)
{
traceWriter.Info(entry.Request, "WebApiThrottle", "{0} Request {1} to endpoint {2} from client {3} has been throttled (blocked), quota {4}/{5} exceeded by {6}",
entry.LogDate, entry.RequestId, entry.ClientIp, entry.RateLimit,
entry.Endpoint, entry.RateLimitPeriod, entry.TotalRequests);
}
}
}
}
1 change: 1 addition & 0 deletions WebApiThrottle/WebApiThrottle.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
<Compile Include="CacheRepository.cs" />
<Compile Include="RequestIndentity.cs" />
<Compile Include="ThrottlingHandler.cs" />
<Compile Include="TracingThrottleLogger.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down

0 comments on commit b675555

Please sign in to comment.