Skip to content

Commit

Permalink
feat(measurements): add measurements
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberhck committed Nov 16, 2019
1 parent 4546206 commit 0906c8a
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 6 deletions.
3 changes: 3 additions & 0 deletions Micro.KeyStore.Api/Keys/KeysController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Threading.Tasks;
using App.Metrics;
using Micro.KeyStore.Api.Keys.Models;
using Micro.KeyStore.Api.Keys.Repositories;
using Micro.KeyStore.Api.Keys.Services;
Expand Down Expand Up @@ -27,6 +28,7 @@ public KeysController(IKeyService keyService, IKeyRepository keyRepository, ILog
[HttpPost]
[ProducesResponseType(typeof(KeyCreatedResponse), StatusCodes.Status201Created)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status409Conflict)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> Add(CreateKeyRequest request)
{
var key = new Key
Expand Down Expand Up @@ -60,6 +62,7 @@ public async Task<IActionResult> Add(CreateKeyRequest request)

[HttpGet("{id}")]
[ProducesResponseType(typeof(KeyCreatedResponse), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
public async Task<IActionResult> Get(string id)
{
var key = await _keyRepository.FindByShortSha(id);
Expand Down
7 changes: 7 additions & 0 deletions Micro.KeyStore.Api/Measurements/KeyController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Micro.KeyStore.Api.Measurements
{
public static class KeyController
{
public static void Measure
}
}
8 changes: 8 additions & 0 deletions Micro.KeyStore.Api/Measurements/Timer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,13 @@ public static (TimeSpan, T) Measure<T>(Func<T> fn)
var elapsed = stopwatch.Elapsed;
return (elapsed, result);
}

public static async Task<TimeSpan> MeasureAsync(Func<Task> fn)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
await fn();
return stopwatch.Elapsed;
}
}
}
40 changes: 40 additions & 0 deletions Micro.KeyStore.Api/Measurements/WorkerMeasurements.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using App.Metrics;

namespace Micro.KeyStore.Api.Measurements
{
public class WorkerMeasurements
{
public static void MeasureTime(IMetrics metrics, TimeSpan duration)
{
var timer = new App.Metrics.Timer.TimerOptions
{
Name = "KeyArchiveSaveTime",
DurationUnit = TimeUnit.Milliseconds,
RateUnit = TimeUnit.Milliseconds
};
metrics?.Provider?.Timer.Instance(timer).Record((long) duration.TotalMilliseconds, TimeUnit.Milliseconds);
}

public static void MeasureArchiveOccurrence(IMetrics metrics)
{
var meter = new App.Metrics.Meter.MeterOptions
{
Name = "KeyArchive",
MeasurementUnit = Unit.Events,
RateUnit = TimeUnit.Seconds
};
metrics?.Provider?.Meter.Instance(meter).Mark();
}
public static void MeasureErrorOccurrence(IMetrics metrics)
{
var meter = new App.Metrics.Meter.MeterOptions
{
Name = "KeyArchiveError",
MeasurementUnit = Unit.Events,
RateUnit = TimeUnit.Seconds
};
metrics?.Provider?.Meter.Instance(meter).Mark();
}
}
}
21 changes: 15 additions & 6 deletions Micro.KeyStore.Api/Workers/CleanupKeysWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using App.Metrics;
using Micro.KeyStore.Api.Archive;
using Micro.KeyStore.Api.Keys.Repositories;
using Micro.KeyStore.Api.Measurements;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -41,31 +42,39 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
var keys = await _keyRepository.FindCreatedBefore(
DateTime.Now.Subtract(TimeSpan.FromMinutes(_config.TimeToLiveInMinutes)), _config.BatchSize);
var keys = (await _keyRepository.FindCreatedBefore(
DateTime.Now.Subtract(TimeSpan.FromMinutes(_config.TimeToLiveInMinutes)), _config.BatchSize)).ToList();
foreach (var key in keys)
{
try
{
await _archiver.Save(new Key
var elapsed = await Measurements.Timer.MeasureAsync(async () => await _archiver.Save(new Key
{
Body = key.Body,
Id = key.ShortSha
});
}));
WorkerMeasurements.MeasureTime(_metrics, elapsed);
WorkerMeasurements.MeasureArchiveOccurrence(_metrics);
await _keyRepository.Remove(key.Id);
}
catch (Exception e)
{
WorkerMeasurements.MeasureErrorOccurrence(_metrics);
_logger.LogError(e, "exception caught");
}

if (stoppingToken.IsCancellationRequested)
{
return;
}
}

_logger.LogInformation($"Archived {keys.Count()} keys at {DateTime.Now}");
_logger.LogInformation($"Archived {keys.Count} keys at {DateTime.Now}");
if (stoppingToken.IsCancellationRequested) return;
await Task.Delay(TimeSpan.FromSeconds(_config.BatchIntervalInSeconds), stoppingToken);
}
}
catch (OperationCanceledException e)
catch (OperationCanceledException)
{
// do nothing
}
Expand Down

0 comments on commit 0906c8a

Please sign in to comment.