Skip to content

Commit

Permalink
Adds logic to interrogate the resolver cache for the stats server.
Browse files Browse the repository at this point in the history
  • Loading branch information
alanedwardes committed Jul 30, 2023
1 parent 6de0994 commit 6054012
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
5 changes: 4 additions & 1 deletion misc/Ae.Dns.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ private static async Task DoWork(string[] args)
services.AddHttpClient(staticDnsResolverHttpClient, x => x.BaseAddress = new Uri("https://1.1.1.1/"))
.SetHandlerLifetime(Timeout.InfiniteTimeSpan);

services.AddSingleton<ObjectCache>(new MemoryCache("ResolverCache"));
ObjectCache resolverCache = new MemoryCache("ResolverCache");

services.AddSingleton(resolverCache);

static DnsDelegatingHandler CreateDnsDelegatingHandler(IServiceProvider serviceProvider)
{
Expand Down Expand Up @@ -205,6 +207,7 @@ async Task ReportStats(CancellationToken token)
})
.ConfigureServices(x =>
{
x.AddSingleton(resolverCache);
x.AddSingleton<IDnsMiddlewareConfig>(new DnsMiddlewareConfig());
x.AddSingleton(dnsClient);
});
Expand Down
21 changes: 17 additions & 4 deletions misc/Ae.Dns.Console/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
using System.Diagnostics.Metrics;
using System.Linq;
using System.Net;
using System.Runtime.Caching;
using System.Text;
using System.Threading.Tasks;
using Ae.Dns.Client;
using Ae.Dns.Protocol;
using Ae.Dns.Server.Http;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;

namespace Ae.Dns.Console
{
Expand Down Expand Up @@ -38,6 +40,16 @@ public void Configure(IApplicationBuilder app)
context.Response.StatusCode = StatusCodes.Status200OK;
context.Response.ContentType = "text/plain; charset=utf-8";
var resolverCache = context.RequestServices.GetRequiredService<ObjectCache>();
async Task WriteHeader(string header)
{
await context.Response.WriteAsync(header);
await context.Response.WriteAsync(Environment.NewLine);
await context.Response.WriteAsync(new string('=', header.Length));
await context.Response.WriteAsync(Environment.NewLine);
}
var statsSets = new Dictionary<string, IDictionary<string, int>>
{
{ "Top Blocked Domains", _topBlockedDomains },
Expand All @@ -47,12 +59,13 @@ public void Configure(IApplicationBuilder app)
{ "Top Exception Error Domains", _topExceptionDomains }
};
await WriteHeader($"Cached Objects = {resolverCache.Count()}");
await context.Response.WriteAsync(Environment.NewLine);
await context.Response.WriteAsync(Environment.NewLine);
foreach (var statsSet in statsSets)
{
await context.Response.WriteAsync(statsSet.Key);
await context.Response.WriteAsync(Environment.NewLine);
await context.Response.WriteAsync(new string('=', statsSet.Key.Length));
await context.Response.WriteAsync(Environment.NewLine);
await WriteHeader(statsSet.Key);
if (!statsSet.Value.Any())
{
Expand Down

0 comments on commit 6054012

Please sign in to comment.