Skip to content

Commit

Permalink
Added tracking of HttpMethod. #12
Browse files Browse the repository at this point in the history
  • Loading branch information
dhindrik committed Aug 19, 2024
1 parent fec48f6 commit f94ee43
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 11 deletions.
1 change: 1 addition & 0 deletions TinyInsights.sln
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Global
{CD742E15-674D-41DB-8467-B8A83E26D5BC}.Release|Any CPU.Build.0 = Release|Any CPU
{606F1693-DAA1-4BEA-BF62-F21F0F509E75}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{606F1693-DAA1-4BEA-BF62-F21F0F509E75}.Debug|Any CPU.Build.0 = Debug|Any CPU
{606F1693-DAA1-4BEA-BF62-F21F0F509E75}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{606F1693-DAA1-4BEA-BF62-F21F0F509E75}.Release|Any CPU.ActiveCfg = Release|Any CPU
{606F1693-DAA1-4BEA-BF62-F21F0F509E75}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
Expand Down
14 changes: 12 additions & 2 deletions TinyInsights/ApplicationInsightsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ public Task TrackPageViewAsync(string viewName, Dictionary<string, string>? prop
return Task.CompletedTask;
}

public Task TrackDependencyAsync(string dependencyType, string dependencyName, string data, DateTimeOffset startTime, TimeSpan duration, bool success, int resultCode = 0, Exception? exception = null)
public Task TrackDependencyAsync(string dependencyType, string dependencyName, string data, HttpMethod? httpMethod, DateTimeOffset startTime, TimeSpan duration, bool success, int resultCode = 0, Exception? exception = null)
{
try
{
Expand All @@ -315,11 +315,16 @@ public Task TrackDependencyAsync(string dependencyType, string dependencyName, s
Timestamp = startTime,
Success = success,
Duration = duration,
ResultCode = resultCode.ToString()
ResultCode = resultCode.ToString(),
};

dependency.Properties.Add("FullUrl", fullUrl);

if (httpMethod != null)
{
dependency.Properties.Add("HttpMethod", httpMethod.ToString());
}

if (exception != null)
{
dependency.Properties.Add("ExceptionMessage", exception.Message);
Expand All @@ -342,6 +347,11 @@ public Task TrackDependencyAsync(string dependencyType, string dependencyName, s
return Task.CompletedTask;
}

public Task TrackDependencyAsync(string dependencyType, string dependencyName, string data, DateTimeOffset startTime, TimeSpan duration, bool success, int resultCode = 0, Exception? exception = null)
{
return TrackDependencyAsync(dependencyType, dependencyName, data, null, startTime, duration, success, resultCode, exception);
}

#region ILogger
public async void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
{
Expand Down
7 changes: 4 additions & 3 deletions TinyInsights/Dependency.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ internal Dependency(IInsights insights)
public string Data { get; internal set; }
public DateTimeOffset StartTime { get; private set; }
public TimeSpan Duration { get; private set; }
public HttpMethod? HttpMethod { get; internal set; }

private SemaphoreSlim semaphore = new SemaphoreSlim(1,1);
private SemaphoreSlim semaphore = new SemaphoreSlim(1, 1);

private bool isFinished;

Expand All @@ -43,11 +44,11 @@ public async Task Finish(bool sucess, int resultCode, Exception exception = null
{
Duration = DateTimeOffset.Now - StartTime;

await insights.TrackDependencyAsync(DependencyType, DependencyName, Data, StartTime, Duration, sucess, resultCode, exception);
await insights.TrackDependencyAsync(DependencyType, DependencyName, Data, HttpMethod, StartTime, Duration, sucess, resultCode, exception);

isFinished = true;

semaphore.Release();
semaphore.Release();
}
}
}
4 changes: 3 additions & 1 deletion TinyInsights/IInsights.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ public interface IInsights

Task TrackEventAsync(string eventName, Dictionary<string, string>? properties = null);

Task TrackDependencyAsync(string dependencyType, string dependencyName, string data, DateTimeOffset startTime, TimeSpan duration, bool success, int resultCode = 0, Exception exception = null);
Task TrackDependencyAsync(string dependencyType, string dependencyName, string data, DateTimeOffset startTime, TimeSpan duration, bool success, int resultCode = 0, Exception? exception = null);
Task TrackDependencyAsync(string dependencyType, string dependencyName, string data, HttpMethod? httpMethod, DateTimeOffset startTime, TimeSpan duration,
bool success, int resultCode = 0, Exception? exception = null);
Dependency CreateDependencyTracker(string dependencyType, string dependencyName, string data);
void OverrideAnonymousUserId(string userId);
void GenerateNewAnonymousUserId();
Expand Down
1 change: 1 addition & 0 deletions TinyInsights/IInsightsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public interface IInsightsProvider

Task TrackEventAsync(string eventName, Dictionary<string, string>? properties = null);

Task TrackDependencyAsync(string dependencyType, string dependencyName, string data, HttpMethod? httpMethod, DateTimeOffset startTime, TimeSpan duration, bool success, int resultCode = 0, Exception? exception = null);
Task TrackDependencyAsync(string dependencyType, string dependencyName, string data, DateTimeOffset startTime, TimeSpan duration, bool success, int resultCode = 0, Exception? exception = null);
void OverrideAnonymousUserId(string userId);
void GenerateNewAnonymousUserId();
Expand Down
21 changes: 20 additions & 1 deletion TinyInsights/Insights.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,18 @@ public Task TrackEventAsync(string eventName, Dictionary<string, string>? proper

public Task TrackDependencyAsync(string dependencyType, string dependencyName, string data, DateTimeOffset startTime, TimeSpan duration,
bool success, int resultCode = 0, Exception? exception = null)
{
return TrackDependencyAsync(dependencyType, dependencyName, data, null, startTime, duration, success, resultCode, exception);
}

public Task TrackDependencyAsync(string dependencyType, string dependencyName, string data, HttpMethod? httpMethod, DateTimeOffset startTime, TimeSpan duration,
bool success, int resultCode = 0, Exception? exception = null)
{
var tasks = new List<Task>();

foreach (var provider in insightsProviders.Where(x => x.IsTrackDependencyEnabled))
{
var task = provider.TrackDependencyAsync(dependencyType, dependencyName, data, startTime, duration, success, resultCode, exception);
var task = provider.TrackDependencyAsync(dependencyType, dependencyName, data, httpMethod, startTime, duration, success, resultCode, exception);
tasks.Add(task);
}

Expand All @@ -81,6 +87,19 @@ public Dependency CreateDependencyTracker(string dependencyType, string dependen
return dependency;
}

public Dependency CreateDependencyTracker(string dependencyType, string dependencyName, string data, HttpMethod httpMethod)
{
var dependency = new Dependency(this)
{
DependencyType = dependencyType,
DependencyName = dependencyName,
Data = data,
HttpMethod = httpMethod
};

return dependency;
}

public void OverrideAnonymousUserId(string userId)
{
foreach (var provider in insightsProviders.Where(x => x.IsTrackDependencyEnabled))
Expand Down
8 changes: 4 additions & 4 deletions TinyInsights/InsightsMessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ namespace TinyInsights;
public class InsightsMessageHandler : DelegatingHandler
{
private readonly IInsights insights;

public InsightsMessageHandler(IInsights insights)
{
this.insights = insights;

InnerHandler = new HttpClientHandler();
}

Expand All @@ -32,15 +32,15 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
exception = e;
}

await insights.TrackDependencyAsync("HTTP", request.RequestUri.Host, request.RequestUri.ToString(), startTime, endTime - startTime,
await insights.TrackDependencyAsync("HTTP", request.RequestUri.Host, request.RequestUri.ToString(), request.Method, startTime, endTime - startTime,

Check warning on line 35 in TinyInsights/InsightsMessageHandler.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
response.IsSuccessStatusCode, (int)response.StatusCode, exception);

return response;
}
catch (Exception ex)
{
var endTime = DateTime.Now;
await insights.TrackDependencyAsync("HTTP",request.RequestUri.Host, request.RequestUri.ToString(), startTime, endTime - startTime,
await insights.TrackDependencyAsync("HTTP", request.RequestUri.Host, request.RequestUri.ToString(), startTime, endTime - startTime,

Check warning on line 43 in TinyInsights/InsightsMessageHandler.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
false, 0, ex);

throw;
Expand Down

0 comments on commit f94ee43

Please sign in to comment.