Add a couple metrics

This commit is contained in:
Gervasio Marchand 2023-08-18 22:19:18 -03:00
parent 0aa7191a9b
commit 4ed3f24ff6
No known key found for this signature in database
GPG Key ID: B7736CB188DD0A38
2 changed files with 26 additions and 15 deletions

View File

@ -1,4 +1,3 @@
using System.Net.Http.Headers;
using System.Text;
namespace FakeRelay.Core.Helpers;
@ -29,7 +28,7 @@ public static class MastodonHelper
private static HttpClient GetClient()
{
var client = new HttpClient { Timeout = TimeSpan.FromSeconds(5) };
var client = new HttpClient { Timeout = TimeSpan.FromSeconds(2) };
client.DefaultRequestHeaders.Add("User-Agent", $"FakeRelay (hosted at {Config.Instance.Host})");
return client;
}
@ -49,16 +48,8 @@ public static class MastodonHelper
var signature = CryptographyHelper.Sign(stringToSign);
request.Headers.Add("Signature", $@"keyId=""https://{Config.Instance.Host}/actor#main-key"",algorithm=""rsa-sha256"",headers=""(request-target) date host digest content-length"",signature=""{signature}""");
try
{
var response = await Client.SendAsync(request);
return await response.Content.ReadAsStringAsync();
}
catch (Exception e)
{
Console.WriteLine($"Exception {e.Message} when connecting to {targetHost}");
throw;
}
var response = await Client.SendAsync(request);
return await response.Content.ReadAsStringAsync();
}
public static async Task ProcessInstanceFollowAsync(ActivityPubModel request)

View File

@ -13,6 +13,10 @@ public class ApiController : Controller
private readonly IMemoryCache _memoryCache;
private static readonly Counter IndexRequests =
Metrics.CreateCounter("index_requests", "Requests to index statuses", "instance");
private static readonly Counter IndexRequestsTimedOut =
Metrics.CreateCounter("index_requests_timed_out", "Requests to index statuses that timed out", "instance");
private static readonly Counter IndexRequestsFailed =
Metrics.CreateCounter("index_requests_failed", "Requests to index statuses that failed", "instance");
public ApiController(IMemoryCache memoryCache)
{
@ -49,9 +53,25 @@ public class ApiController : Controller
{
return Unauthorized();
}
var response = await MastodonHelper.EnqueueStatusToFetchAsync(host, statusUrl);
IndexRequests.WithLabels(host).Inc();
string? response;
try
{
response = await MastodonHelper.EnqueueStatusToFetchAsync(host, statusUrl);
IndexRequests.WithLabels(host).Inc();
}
catch (TaskCanceledException)
{
response = "2s timeout exceeded";
IndexRequestsTimedOut.WithLabels(host).Inc();
}
catch (Exception e)
{
response = $"Error: {e.Message}";
Console.WriteLine($"Error indexing for host {host}: {e}");
IndexRequestsFailed.WithLabels(host).Inc();
}
Response.Headers["instance"] = host;
return Content(response, "application/activity+json");
}