Skip to content

Commit b0200bc

Browse files
refactor: extract environment metadata from DebugEntry
1 parent 254b9e2 commit b0200bc

8 files changed

Lines changed: 94 additions & 65 deletions

File tree

DebugProbe.AspNetCore/Extensions/DebugProbeExtensions.cs

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public static IServiceCollection AddDebugProbe(
3030
public static IApplicationBuilder UseDebugProbe(this IApplicationBuilder app)
3131
{
3232
app.UseMiddleware<DebugProbeMiddleware>();
33+
app.ApplicationServices.GetRequiredService<DebugEntryStore>();
3334

3435
if (app is WebApplication webApp)
3536
{
@@ -59,59 +60,83 @@ public static IApplicationBuilder UseDebugProbe(this IApplicationBuilder app)
5960
var prettyRequest = JsonUtils.Format(item.RequestBody);
6061
var prettyResponse = JsonUtils.Format(item.ResponseBody);
6162

62-
var html = HtmlRenderer.RenderDetailsPage(item, prettyRequest, prettyResponse);
63+
var html = HtmlRenderer.RenderDetailsPage(item, store.Environment, prettyRequest, prettyResponse);
6364

6465
ctx.Response.ContentType = "text/html";
6566
await ctx.Response.WriteAsync(html);
6667
}).ExcludeFromDescription();
6768

68-
webApp.MapGet("/debug/compare/{id}", async (string id, string url, DebugEntryStore store) =>
69+
webApp.MapGet("/debug/compare/{id}", async (string id, string baseUrl, string remoteTraceId, DebugEntryStore store) =>
6970
{
70-
var local = store.Get(id);
71-
if (local is null)
71+
var localEnvironment = store.Environment;
72+
var localEntry = store.Get(id);
73+
if (localEntry is null)
7274
{
7375
return Results.NotFound("Local trace not found");
7476
}
7577

76-
DebugEntry? remote;
78+
79+
var normalizedBaseUrl = baseUrl.TrimEnd('/');
80+
81+
var remoteEnvironmentUrl =
82+
$"{normalizedBaseUrl}/debug/environment";
83+
84+
var remoteEntryUrl =
85+
$"{normalizedBaseUrl}/debug/json/{remoteTraceId}";
86+
87+
DebugEntry? remoteEntry;
88+
DebugEnvironment? remoteEnvironment;
7789

7890
try
7991
{
80-
remote = await Http.GetFromJsonAsync<DebugEntry>(url);
92+
remoteEnvironment = await Http.GetFromJsonAsync<DebugEnvironment>(remoteEnvironmentUrl);
93+
94+
if (remoteEnvironment is null)
95+
{
96+
return Results.BadRequest("Failed to load remote environment");
97+
}
98+
99+
remoteEntry = await Http.GetFromJsonAsync<DebugEntry>(remoteEntryUrl);
100+
101+
if (remoteEntry is null)
102+
{
103+
return Results.NotFound("Remote trace not found");
104+
}
81105
}
82106
catch
83107
{
84108
return Results.BadRequest("Failed to reach remote server");
85109
}
86110

87-
if (remote is null)
88-
{
89-
return Results.NotFound("Remote trace not found");
90-
}
91-
92-
var diff = DebugEntryComparer.Compare(local, remote);
111+
112+
var diff = DebugEntryComparer.Compare(localEntry, remoteEntry);
93113

94114
return Results.Ok(new
95115
{
96-
method = new { local = local.Method, remote = remote.Method },
97-
path = new { local = local.Path, remote = remote.Path },
98-
status = new { local = local.StatusCode, remote = remote.StatusCode },
116+
method = new { local = localEntry.Method, remote = remoteEntry.Method },
117+
path = new { local = localEntry.Path, remote = remoteEntry.Path },
118+
status = new { local = localEntry.StatusCode, remote = remoteEntry.StatusCode },
99119

100120
requestTime = new
101121
{
102-
local = local.RequestTimeUtc.ToLocalTime().ToString("HH:mm:ss"),
103-
remote = remote.RequestTimeUtc.ToLocalTime().ToString("HH:mm:ss"),
122+
local = localEntry.RequestTimeUtc.ToLocalTime().ToString("HH:mm:ss"),
123+
remote = remoteEntry.RequestTimeUtc.ToLocalTime().ToString("HH:mm:ss"),
104124
},
105125

106-
environment = new { local = local.Environment, remote = remote.Environment },
107-
culture = new { local = local.Culture, remote = remote.Culture },
108-
requestBody = new { local = local.RequestBody ?? "", remote = remote.RequestBody ?? "" },
109-
responseBody = new { local = local.ResponseBody ?? "", remote = remote.ResponseBody ?? "" },
126+
environment = new { local = localEnvironment.Environment, remote = remoteEnvironment?.Environment ?? "" },
127+
culture = new { local = localEnvironment.Culture, remote = remoteEnvironment?.Culture ?? "" },
128+
requestBody = new { local = localEntry.RequestBody ?? "", remote = remoteEntry.RequestBody ?? "" },
129+
responseBody = new { local = localEntry.ResponseBody ?? "", remote = remoteEntry.ResponseBody ?? "" },
110130

111131
diffs = diff
112132
});
113133
}).ExcludeFromDescription();
114134

135+
webApp.MapGet("/debug/environment", (DebugEntryStore store) =>
136+
{
137+
return Results.Ok(store.Environment);
138+
}).ExcludeFromDescription();
139+
115140
webApp.MapGet("/debug/json/{id}", (string id, DebugEntryStore store) =>
116141
{
117142
var item = store.Get(id);

DebugProbe.AspNetCore/Internal/DebugEntryComparer.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@ public static List<object> Compare(DebugEntry a, DebugEntry b)
1919
if (a.StatusCode != b.StatusCode)
2020
AddDiff(diffs, "Status", a.StatusCode, b.StatusCode, "meta");
2121

22-
if (a.Environment != b.Environment)
23-
AddDiff(diffs, "Environment", a.Environment, b.Environment, "meta");
24-
25-
if (a.Culture != b.Culture)
26-
AddDiff(diffs, "Culture", a.Culture, b.Culture, "meta");
27-
2822
CompareBody(a.RequestBody, b.RequestBody, RequestBodyPath, diffs);
2923
CompareBody(a.ResponseBody, b.ResponseBody, ResponseBodyPath, diffs);
3024

DebugProbe.AspNetCore/Internal/HtmlRenderer.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public static string RenderIndexPage(List<DebugEntry> items)
3939
return BuildLayout(EmbeddedResources.Index.Replace("{{rows}}", rows));
4040
}
4141

42-
public static string RenderDetailsPage(DebugEntry x, string req, string res)
42+
public static string RenderDetailsPage(DebugEntry x, DebugEnvironment e, string req, string res)
4343
{
4444
var headers = string.Join("", x.Headers.Select(h =>
4545
$"<tr><td>{Encode(h.Key)}</td><td>{Encode(h.Value)}</td></tr>"));
@@ -71,14 +71,14 @@ public static string RenderDetailsPage(DebugEntry x, string req, string res)
7171
.Replace("{{requestSize}}", x.RequestSize.ToString())
7272
.Replace("{{responseSize}}", x.ResponseSize.ToString())
7373

74-
.Replace("{{env}}", Encode(x.Environment))
75-
.Replace("{{culture}}", Encode(x.Culture))
74+
.Replace("{{env}}", Encode(e.Environment))
75+
.Replace("{{culture}}", Encode(e.Culture))
7676

77-
.Replace("{{machineName}}", Encode(x.MachineName))
78-
.Replace("{{timeZone}}", Encode(x.TimeZone))
79-
.Replace("{{decimalSeparator}}", Encode(x.DecimalSeparator))
80-
.Replace("{{dateFormat}}", x.DateFormat ?? "")
81-
.Replace("{{assemblyVersion}}", Encode(x.AssemblyVersion))
77+
.Replace("{{machineName}}", Encode(e.MachineName))
78+
.Replace("{{timeZone}}", Encode(e.TimeZone))
79+
.Replace("{{decimalSeparator}}", Encode(e.DecimalSeparator))
80+
.Replace("{{dateFormat}}", e.DateFormat ?? "")
81+
.Replace("{{assemblyVersion}}", Encode(e.AssemblyVersion))
8282

8383
.Replace("{{requestUrl}}", Encode(string.IsNullOrEmpty(x.RequestUrl) ? "(empty)" : x.RequestUrl))
8484
.Replace("{{request}}", Encode(string.IsNullOrEmpty(req) ? "(empty)" : req))

DebugProbe.AspNetCore/Middleware/DebugProbeMiddleware.cs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
using System.Diagnostics;
2-
using System.Globalization;
3-
using System.Reflection;
42
using System.Text;
5-
using DebugProbe.AspNetCore.Internal;
63
using DebugProbe.AspNetCore.Models;
74
using DebugProbe.AspNetCore.Options;
85
using DebugProbe.AspNetCore.Storage;
96
using Microsoft.AspNetCore.Http;
10-
using Microsoft.Extensions.Options;
117

128
namespace DebugProbe.AspNetCore.Middleware;
139

@@ -83,25 +79,12 @@ public async Task Invoke(HttpContext context, DebugEntryStore store)
8379
await ms.CopyToAsync(originalBody);
8480
context.Response.Body = originalBody;
8581

86-
var shortDatePattern = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
87-
var index = shortDatePattern.LastIndexOf('y');
88-
var dataFormat = index >= 0 ? shortDatePattern[..(index + 1)] : shortDatePattern;
89-
9082
var statusCode = exception && context.Response.StatusCode == 200 ? 500 : context.Response.StatusCode;
9183

9284
store.Add(new DebugEntry
9385
{
9486
Id = Guid.NewGuid().ToString(),
9587

96-
// Environment
97-
Environment = EnvironmentUtils.TryGetEnvironment(),
98-
MachineName = Environment.MachineName,
99-
AssemblyVersion = Assembly.GetEntryAssembly()?.GetName().Version?.ToString(),
100-
TimeZone = TimeZoneInfo.Local.DisplayName,
101-
Culture = CultureInfo.CurrentCulture.Name,
102-
DecimalSeparator = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator,
103-
DateFormat = dataFormat,
104-
10588
// Overview
10689
Method = context.Request.Method,
10790
Path = context.Request.Path,

DebugProbe.AspNetCore/Models/DebugEntry.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,6 @@ public class DebugEntry
1919
public long ResponseSize { get; set; }
2020
public string ResponseBody { get; set; } = default!;
2121

22-
// Environment
23-
public string Environment { get; set; } = default!;
24-
public string Culture { get; set; } = default!;
25-
public string? UiCulture { get; set; }
26-
27-
public string? MachineName { get; set; }
28-
public string? AssemblyVersion { get; set; }
29-
public string? TimeZone { get; set; }
30-
public string? DecimalSeparator { get; set; }
31-
public string? DateFormat { get; set; }
32-
3322
// Headers
3423
public Dictionary<string, string> Headers { get; set; } = new();
3524

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace DebugProbe.AspNetCore.Models;
2+
3+
public class DebugEnvironment
4+
{
5+
public string Environment { get; init; } = default!;
6+
public string Culture { get; init; } = default!;
7+
public string? UiCulture { get; init; }
8+
public string? MachineName { get; init; }
9+
public string? AssemblyVersion { get; init; }
10+
public string? TimeZone { get; init; }
11+
public string? DecimalSeparator { get; init; }
12+
public string? DateFormat { get; init; }
13+
}

DebugProbe.AspNetCore/Resources/js/debugprobe-compare.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
setCompareResult('<b style="color:orange">Comparing...</b>');
1212

1313
try {
14-
const remoteUrl = `${base.replace(/\/$/, '')}/debug/json/${remoteId}`;
15-
const res = await fetch(`/debug/compare/${id}?url=${encodeURIComponent(remoteUrl)}`);
14+
const res = await fetch(`/debug/compare/${id}?baseUrl=${encodeURIComponent(base)}&remoteTraceId=${encodeURIComponent(remoteId)}`);
1615

1716
if (!res.ok) {
1817
const text = await res.json();

DebugProbe.AspNetCore/Storage/DebugEntryStore.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
using System.Collections.Concurrent;
2+
using System.Globalization;
3+
using System.Reflection;
4+
using DebugProbe.AspNetCore.Internal;
25
using DebugProbe.AspNetCore.Models;
36
using DebugProbe.AspNetCore.Options;
47

@@ -9,12 +12,26 @@ namespace DebugProbe.AspNetCore.Storage;
912
/// </summary>
1013
public class DebugEntryStore
1114
{
15+
public DebugEnvironment Environment { get; }
16+
1217
private readonly ConcurrentQueue<DebugEntry> _queue = new();
1318
private readonly int _limit;
1419

1520
public DebugEntryStore(DebugProbeOptions options)
1621
{
1722
_limit = options.MaxEntries;
23+
24+
Environment = new DebugEnvironment
25+
{
26+
Environment = EnvironmentUtils.TryGetEnvironment(),
27+
MachineName = System.Environment.MachineName,
28+
AssemblyVersion = Assembly.GetEntryAssembly()?.GetName().Version?.ToString(),
29+
TimeZone = TimeZoneInfo.Local.DisplayName,
30+
Culture = CultureInfo.CurrentCulture.Name,
31+
UiCulture = CultureInfo.CurrentUICulture.Name,
32+
DecimalSeparator = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator,
33+
DateFormat = GetDateFormat()
34+
};
1835
}
1936

2037
public void Add(DebugEntry entry)
@@ -38,4 +55,13 @@ public void Clear()
3855
{
3956
while (_queue.TryDequeue(out _)) { }
4057
}
58+
59+
private string GetDateFormat()
60+
{
61+
var shortDatePattern = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
62+
var index = shortDatePattern.LastIndexOf('y');
63+
var dataFormat = index >= 0 ? shortDatePattern[..(index + 1)] : shortDatePattern;
64+
65+
return dataFormat;
66+
}
4167
}

0 commit comments

Comments
 (0)