generated from arcus-azure/arcus.github.template
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature - introduce 'DependencyMeasurement' for tracking latencie… (#52)
* Feature - introduce 'DependencyMeasurement' for tracking latencies in dependencies * pr-sug: use both approach on dependency measurement * pr-sug: use overload with 'DependencyMeasurement' * pr-fix: provide xml docs for dep data prop * pr-sug: use const for dependency data test value * pr-fix: use duration correctly * pr-sug: use ctor for creating fields * pr-docs: use measurement overload in docs
- Loading branch information
1 parent
d35df2d
commit ef8963c
Showing
5 changed files
with
291 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/Arcus.Observability.Telemetry.Core/DependencyMeasurement.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System; | ||
using System.Diagnostics; | ||
|
||
namespace Arcus.Observability.Telemetry.Core | ||
{ | ||
/// <summary> | ||
/// Represents an instance to measure easily dependencies in an application. | ||
/// </summary> | ||
public class DependencyMeasurement : IDisposable | ||
{ | ||
private readonly Stopwatch _stopwatch; | ||
|
||
private DependencyMeasurement(string dependencyData) | ||
{ | ||
_stopwatch = Stopwatch.StartNew(); | ||
StartTime = DateTimeOffset.UtcNow; | ||
DependencyData = dependencyData; | ||
} | ||
|
||
/// <summary> | ||
/// Starts measuring a dependency until the measurement is disposed. | ||
/// </summary> | ||
public static DependencyMeasurement Start() | ||
{ | ||
return Start(dependencyData: null); | ||
} | ||
|
||
/// <summary> | ||
/// Starts measuring a dependency until the measurement is disposed. | ||
/// </summary> | ||
/// <param name="dependencyData">The additional data related to the dependency.</param> | ||
public static DependencyMeasurement Start(string dependencyData) | ||
{ | ||
return new DependencyMeasurement(dependencyData); | ||
} | ||
|
||
/// <summary> | ||
/// Gets the time when the dependency measurement was started. | ||
/// </summary> | ||
public DateTimeOffset StartTime { get; } | ||
|
||
/// <summary> | ||
/// Gets the total elapsed time measured for the dependency. | ||
/// </summary> | ||
public TimeSpan Elapsed => _stopwatch.Elapsed; | ||
|
||
/// <summary> | ||
/// Gets the additional data that corresponds with the measured dependency. | ||
/// </summary> | ||
public string DependencyData { get; } | ||
|
||
/// <summary> | ||
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. | ||
/// </summary> | ||
public void Dispose() | ||
{ | ||
_stopwatch.Stop(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
src/Arcus.Observability.Tests.Unit/Telemetry/DependencyMeasurementTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Arcus.Observability.Telemetry.Core; | ||
using Xunit; | ||
|
||
namespace Arcus.Observability.Tests.Unit.Telemetry | ||
{ | ||
[Trait("Category", "Unit")] | ||
public class DependencyMeasurementTests | ||
{ | ||
[Fact] | ||
public async Task DependencyMeasurement_StartMeasuringAction_HoldsStartAndElapsedTime() | ||
{ | ||
// Act | ||
using (var measurement = DependencyMeasurement.Start()) | ||
{ | ||
// Assert | ||
await Task.Delay(TimeSpan.FromMilliseconds(100)); | ||
|
||
Assert.NotNull(measurement); | ||
Assert.True(DateTimeOffset.UtcNow > measurement.StartTime, "Measurement should have lesser start time"); | ||
Assert.True(TimeSpan.Zero < measurement.Elapsed, "Measurement should be running"); | ||
} | ||
} | ||
|
||
[Fact] | ||
public async Task DependencyMeasurement_StopsMeasuringAction_WhenDisposed() | ||
{ | ||
// Arrange | ||
var measurement = DependencyMeasurement.Start(); | ||
await Task.Delay(TimeSpan.FromMilliseconds(100)); | ||
measurement.Dispose(); | ||
var elapsed = measurement.Elapsed; | ||
|
||
// Act | ||
await Task.Delay(TimeSpan.FromMilliseconds(100)); | ||
|
||
// Assert | ||
Assert.NotNull(measurement); | ||
Assert.Equal(elapsed, measurement.Elapsed); | ||
} | ||
|
||
[Fact] | ||
public async Task DependencyMeasurementWithDependencyData_StartMeasuringAction_HoldsStartAndElapsedTime() | ||
{ | ||
// Act | ||
const string dependencyData = "Operation"; | ||
using (var measurement = DependencyMeasurement.Start(dependencyData)) | ||
{ | ||
// Assert | ||
await Task.Delay(TimeSpan.FromMilliseconds(100)); | ||
|
||
Assert.NotNull(measurement); | ||
Assert.Equal(dependencyData, measurement.DependencyData); | ||
Assert.True(DateTimeOffset.UtcNow > measurement.StartTime, "Measurement should have lesser start time"); | ||
Assert.True(TimeSpan.Zero < measurement.Elapsed, "Measurement should be running"); | ||
} | ||
} | ||
|
||
[Fact] | ||
public async Task DependencyMeasurementWithDependencyData_StopsMeasuringAction_WhenDisposed() | ||
{ | ||
// Arrange | ||
const string dependencyData = "Operation"; | ||
var measurement = DependencyMeasurement.Start(dependencyData); | ||
await Task.Delay(TimeSpan.FromMilliseconds(100)); | ||
measurement.Dispose(); | ||
var elapsed = measurement.Elapsed; | ||
|
||
// Act | ||
await Task.Delay(TimeSpan.FromMilliseconds(100)); | ||
|
||
// Assert | ||
Assert.NotNull(measurement); | ||
Assert.Equal(dependencyData, measurement.DependencyData); | ||
Assert.Equal(elapsed, measurement.Elapsed); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters