Skip to content

Commit ceb1c5c

Browse files
authored
Merge pull request #24 from zetroot/feature/add-metrics
Feature/add metrics +semver: feature
2 parents 4d6901c + e25e217 commit ceb1c5c

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/NTorSpectator.Observer/Services/SpectatorJob.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
1+
using System.Diagnostics;
12
using NTorSpectator.Observer.TorIntegration;
23
using NTorSpectator.Services;
34
using NTorSpectator.Services.Models;
5+
using Prometheus;
46
using Quartz;
57

68
namespace NTorSpectator.Observer.Services;
79

810
public class SpectatorJob : IJob
911
{
12+
private static readonly Gauge QueueLength = Metrics.CreateGauge("sites_queue_length", "Length of the queue left to observe");
13+
private static readonly Counter ObservationsCount = Metrics.CreateCounter("observations", "counts all observations");
14+
private static readonly Counter RetriesCount = Metrics.CreateCounter("enqueued_retries", "counts retires");
15+
private static readonly Histogram RequestDuration = Metrics.CreateHistogram("observation_duration", "duration of site observation",
16+
new HistogramConfiguration
17+
{
18+
Buckets = Histogram.LinearBuckets(0.5, 0.5, 20)
19+
});
20+
private static readonly Gauge TotalSessionDuration = Metrics.CreateGauge("observation_session_duration", "Total observation session duration, ms");
21+
1022
private readonly ILogger<SpectatorJob> _logger;
1123
private readonly ISitesCatalogue _sitesCatalogue;
1224
private readonly TorControlManager _torControl;
@@ -22,18 +34,22 @@ public SpectatorJob(ILogger<SpectatorJob> logger, ISitesCatalogue sitesCatalogue
2234

2335
public async Task Execute(IJobExecutionContext context)
2436
{
37+
TotalSessionDuration.Set(0);
38+
var sw = Stopwatch.StartNew();
2539
_logger.LogDebug("Starting sites observations");
2640
var sites = await _sitesCatalogue.GetAllSites();
2741
_logger.LogDebug("Got {Count} sites to observe", sites.Count);
2842

2943
var siteQueue = new Queue<QueuedSite>(sites.Select(x => new QueuedSite(x, 0)));
3044
while(siteQueue.TryDequeue(out var queuedSite))
3145
{
46+
QueueLength.Set(siteQueue.Count);
3247
using var _ = _logger.BeginScope(new Dictionary<string, object> { { "HiddenService", queuedSite.Site.SiteUri } });
3348
_logger.LogDebug("Starting observations on the next site");
3449
try
3550
{
3651
var observations = await ObserveSite(queuedSite.Site.SiteUri);
52+
ObservationsCount.Inc();
3753
if (!observations.IsOk)
3854
{
3955
_logger.LogDebug("Site observed as not available");
@@ -42,6 +58,7 @@ public async Task Execute(IJobExecutionContext context)
4258
{
4359
_logger.LogDebug("Site has been observed {Count} times, returning it to queue", siteObservationsCount);
4460
siteQueue.Enqueue(queuedSite with{ObservationsCount = siteObservationsCount + 1});
61+
RetriesCount.Inc();
4562
continue;
4663
}
4764
}
@@ -55,12 +72,15 @@ public async Task Execute(IJobExecutionContext context)
5572
}
5673
}
5774
_logger.LogDebug("The queue is finally empty, observations finished");
75+
sw.Stop();
76+
TotalSessionDuration.Set(sw.ElapsedMilliseconds);
5877
}
5978

6079
private record QueuedSite(Site Site, int ObservationsCount);
6180

6281
private async Task<TorWatchResults> ObserveSite(string site)
6382
{
83+
using var _ = RequestDuration.NewTimer();
6484
var torReply = await _torControl.HsFetch(site);
6585
var positive = torReply.Count(x => x.Action == HsDescAction.Received);
6686
var negative = torReply.Count(x => x.Action == HsDescAction.Failed);

0 commit comments

Comments
 (0)