-
-
Notifications
You must be signed in to change notification settings - Fork 1
Add metrics #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add metrics #29
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ecc0181
Add metrics for lock operations using System.Diagnostics.Metrics
TylerReid 6630109
add View documentation to the readme for filtering out unwanted metrics
TylerReid 3853039
fix issues found by copilot review
TylerReid bca8eea
remove source generated metrics class, and instead implement a metricβ¦
TylerReid 7076c0c
remove unused reference
TylerReid f3ae4d7
add second constructors to DynamoDbDistributedLock and ExponentialBacβ¦
TylerReid 8a2bdd6
Update src/DynamoDb.DistributedLock/Retry/ExponentialBackoffRetryPoliβ¦
ncipollina 93b962a
Update test/DynamoDb.DistributedLock.Tests/Metrics/TestMetricAggregatβ¦
ncipollina File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,49 @@ | ||
| using System; | ||
|
|
||
| namespace DynamoDb.DistributedLock.Metrics; | ||
|
|
||
| /// <summary> | ||
| /// Defines methods for tracking metrics related to distributed lock operations. | ||
| /// </summary> | ||
| public interface ILockMetrics | ||
| { | ||
| // Timers (use with 'using' to record elapsed milliseconds) | ||
| /// <summary> | ||
| /// Creates a timer to track the duration of a lock acquisition attempt. | ||
| /// </summary> | ||
| /// <returns>IDisposable that tracks start time based on time created, and publishes end time based on disposal time</returns> | ||
| IDisposable TrackLockAcquire(); | ||
| /// <summary> | ||
| /// Creates a timer to track the duration of a lock release attempt. | ||
| /// </summary> | ||
| /// <returns>IDisposable that tracks start time based on time created, and publishes end time based on disposal time</returns> | ||
| IDisposable TrackLockRelease(); | ||
|
|
||
| // Counters | ||
| /// <summary> | ||
| /// A lock was successfully acquired. | ||
| /// </summary> | ||
| void LockAcquired(); | ||
| /// <summary> | ||
| /// A lock was successfully released. | ||
| /// </summary> | ||
| void LockReleased(); | ||
| /// <summary> | ||
| /// A lock acquisition attempt failed. | ||
| /// </summary> | ||
| /// <param name="reason">tag value for the reason the failure occured</param> | ||
| void LockAcquireFailed(string reason); // e.g., "not_owned", "timeout", "unexpected_exception" | ||
| /// <summary> | ||
| /// A lock release attempt failed. | ||
| /// </summary> | ||
| /// <param name="reason">tag value for the reason the failure occured</param> | ||
| void LockReleaseFailed(string reason); // e.g., "not_owned", "unexpected_exception" | ||
| /// <summary> | ||
| /// A retry attempt was made during lock acquisition. | ||
| /// </summary> | ||
| void RetryAttempt(); | ||
| /// <summary> | ||
| /// All retry attempts were exhausted without acquiring the lock. | ||
| /// </summary> | ||
| void RetriesExhausted(); | ||
| } |
This file contains hidden or 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,90 @@ | ||
| using System; | ||
| using System.Diagnostics; | ||
| using System.Diagnostics.Metrics; | ||
|
|
||
| namespace DynamoDb.DistributedLock.Metrics; | ||
|
|
||
| /// <inheritdoc cref="ILockMetrics" /> | ||
| public sealed class LockMetrics : ILockMetrics, IDisposable | ||
| { | ||
| private readonly Meter _meter; | ||
|
|
||
| private readonly Counter<int> _lockAcquire; | ||
| private readonly Counter<int> _lockRelease; | ||
| private readonly Counter<int> _lockAcquireFailed; | ||
| private readonly Counter<int> _lockReleaseFailed; | ||
| private readonly Counter<int> _retriesExhausted; | ||
| private readonly Counter<int> _retryAttempt; | ||
| private readonly Histogram<double> _lockAcquireTimer; | ||
| private readonly Histogram<double> _lockReleaseTimer; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="LockMetrics"/> class with the specified <see cref="Meter"/>. | ||
| /// </summary> | ||
| /// <param name="meter"></param> | ||
| public LockMetrics(Meter meter) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(meter); | ||
| _meter = meter; | ||
|
|
||
| _lockAcquire = _meter.CreateCounter<int>(MetricNames.LockAcquire, unit: "count"); | ||
| _lockRelease = _meter.CreateCounter<int>(MetricNames.LockRelease, unit: "count"); | ||
| _lockAcquireFailed = _meter.CreateCounter<int>(MetricNames.LockAcquireFailed, unit: "count"); | ||
| _lockReleaseFailed = _meter.CreateCounter<int>(MetricNames.LockReleaseFailed, unit: "count"); | ||
| _retriesExhausted = _meter.CreateCounter<int>(MetricNames.RetriesExhausted, unit: "count"); | ||
| _retryAttempt = _meter.CreateCounter<int>(MetricNames.RetryAttempt, unit: "count"); | ||
| _lockAcquireTimer = _meter.CreateHistogram<double>(MetricNames.LockAcquireTimer, unit: "ms"); | ||
| _lockReleaseTimer = _meter.CreateHistogram<double>(MetricNames.LockReleaseTimer, unit: "ms"); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// A default instance of <see cref="LockMetrics"/> using a meter with the name defined by <see cref="MetricNames.MeterName"/>. | ||
| /// </summary> | ||
| public static LockMetrics Default { get; } = new(new Meter(MetricNames.MeterName)); | ||
|
|
||
| /// <inheritdoc /> | ||
| public IDisposable TrackLockAcquire() => new TimerScope(_lockAcquireTimer); | ||
|
|
||
| /// <inheritdoc /> | ||
| public IDisposable TrackLockRelease() => new TimerScope(_lockReleaseTimer); | ||
|
|
||
| /// <inheritdoc /> | ||
| public void LockAcquired() => _lockAcquire.Add(1); | ||
|
|
||
| /// <inheritdoc /> | ||
| public void LockReleased() => _lockRelease.Add(1); | ||
|
|
||
| /// <inheritdoc /> | ||
| public void LockAcquireFailed(string reason) | ||
| { | ||
| var tags = new TagList { { "reason", reason } }; | ||
| _lockAcquireFailed.Add(1, tags); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public void LockReleaseFailed(string reason) | ||
| { | ||
| var tags = new TagList { { "reason", reason } }; | ||
| _lockReleaseFailed.Add(1, tags); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public void RetryAttempt() => _retryAttempt.Add(1); | ||
|
|
||
| /// <inheritdoc /> | ||
| public void RetriesExhausted() => _retriesExhausted.Add(1); | ||
|
|
||
| /// <inheritdoc /> | ||
| public void Dispose() => _meter.Dispose(); | ||
|
|
||
| private readonly struct TimerScope(Histogram<double> hist) : IDisposable | ||
| { | ||
| private readonly long _start = Stopwatch.GetTimestamp(); | ||
|
|
||
| public void Dispose() | ||
| { | ||
| var ms = (Stopwatch.GetTimestamp() - _start) * 1000.0 / Stopwatch.Frequency; | ||
| hist.Record(ms); | ||
| } | ||
| } | ||
| } |
TylerReid marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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,45 @@ | ||||||
| namespace DynamoDb.DistributedLock.Metrics; | ||||||
|
|
||||||
| /// <summary> | ||||||
| /// Names of metrics that are published by DynamoDb.DistributedLock | ||||||
| /// </summary> | ||||||
| public static class MetricNames | ||||||
| { | ||||||
| /// <summary> | ||||||
| /// Name of the Meter used for publishing metrics. | ||||||
| /// </summary> | ||||||
| public const string MeterName = "DynamoDb.DistributedLock"; | ||||||
|
|
||||||
| /// <summary> | ||||||
| /// A lock is successfully released. | ||||||
| /// </summary> | ||||||
| public const string LockRelease = "dynamodb.distributedlock.lock_release"; | ||||||
| /// <summary> | ||||||
| /// A lock release operation failed. | ||||||
| /// </summary> | ||||||
| public const string LockReleaseFailed = "dynamodb.distributedlock.lock_release.failed"; | ||||||
| /// <summary> | ||||||
| /// A lock is successfully acquired. | ||||||
| /// </summary> | ||||||
| public const string LockAcquire = "dynamodb.distributedlock.lock_acquire"; | ||||||
| /// <summary> | ||||||
| /// A lock acquisition operation failed. | ||||||
| /// </summary> | ||||||
| public const string LockAcquireFailed = "dynamodb.distributedlock.lock_acquire.failed"; | ||||||
| /// <summary> | ||||||
| /// Retries where attempted, but the maximum number of retries was reached without success. | ||||||
|
||||||
| /// Retries where attempted, but the maximum number of retries was reached without success. | |
| /// Retries were attempted, but the maximum number of retries was reached without success. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.