Skip to content

Comments

Add metrics#29

Merged
ncipollina merged 8 commits intoLayeredCraft:mainfrom
TylerReid:addMetrics
Aug 28, 2025
Merged

Add metrics#29
ncipollina merged 8 commits intoLayeredCraft:mainfrom
TylerReid:addMetrics

Conversation

@TylerReid
Copy link
Collaborator

🚀 Pull Request

📋 Summary

This PR implements metric collection for various actions when taking locks. It uses System.Diagnostics.Metrics Meters to collect the metrics in a way that is consumable by many different telemetry systems.

The design of System.Diagnostics.Metrics is focused on being low cost if you don't opt in to the metrics being collected, so if there are no listeners to a meter, or the instrument such as a specific count metric, then the overhead is a low cost if statement inside the instrument.

I have tested this by sending the metrics to an instance of the .Net Aspire dashboard:
dynamodb distributedlock lock_acquire timer


✅ Checklist

  • My changes build cleanly
  • I’ve added/updated relevant tests
  • I’ve added/updated documentation or README
  • I’ve followed the coding style for this project
  • I’ve tested the changes locally (if applicable)

🧪 Related Issues or PRs

Closes #26

💬 Notes for Reviewers

I think that I implemented the test setup in a reasonable way, but any feedback is appreciated.

This change doesn't include any traces, which could be expanded upon using activities, but I don't currently intend to use traces at the moment, so I didn't add something I wasn't going to verify is useful myself.

One thing I would have loved to add would be extensions on IServiceCollection to configure things inside of AddOpenTelemetry in a condensed way for certain scenarios, but because the metric collection is separate from producing them, it would be bad to add a dependency just for one type. Maybe a second nuget DynamoDb.DistributedLock.Telemetry.OpenTelemetry would be the way to do that 🙂.

@ncipollina ncipollina requested a review from Copilot August 20, 2025 18:52

This comment was marked as outdated.

Copy link
Collaborator

@ncipollina ncipollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 First of all, love this implementation!!! I think this is exactly what we need for next version of this. That being said, I did leave some commentary on some suggested changes. I would prefer to not add any additional packages to the project and just use the System.Diagnostics.DiagnosticSource package that is already included. I've left a couple options for how this could be implemented.

As to your question regarding the DI for OTEL, I would prefer to add that as a separate project, that can be deployed to nuget separately. If you would like to include that, or add as an additional issue/PR, by all means, go for it!

@ncipollina ncipollina requested a review from Copilot August 20, 2025 19:59

This comment was marked as outdated.

@TylerReid
Copy link
Collaborator Author

What version prefix should this change use?

@ncipollina
Copy link
Collaborator

What version prefix should this change use?

If you keep the interface, we would bump the version to 2.0.0.

Copy link
Collaborator

@ncipollina ncipollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💰 I left a comment on how you can test if you use the static approach. You asked a question around version. If you leave it as you have it with the breaking change, make it 2.0.0. If you decide to go the static approach, then it would be 1.2.0. Let me know what you decide and commit the version there.

…koffRetryPolicy that don't take the lock interface to preserve backwards compatibility
@TylerReid
Copy link
Collaborator Author

I realized that I was probably overthinking things. If there are two constructors, that is a backwards compatible change, so we get nice unit testing, as well as a sensible static default from LockMetrics.Default. The constructor with more parameters would probably only be used by those that really care to customize, and the tests.

In the case of people wanting to do something really custom like skipping Meter completely, the interface option is much better than the static class. With the interface you could implement something that talks directly to something like DataDog.

@ncipollina ncipollina requested a review from Copilot August 28, 2025 11:37

This comment was marked as outdated.

ncipollina and others added 2 commits August 28, 2025 07:43
…cy.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…or.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@ncipollina ncipollina requested a review from Copilot August 28, 2025 11:44
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements comprehensive metrics collection for distributed lock operations using System.Diagnostics.Metrics. The implementation includes counters for successful/failed lock acquisition and release, retry attempts, and timers for operation durations, with low-overhead collection that only activates when listeners are present.

  • Adds metrics infrastructure with ILockMetrics interface and LockMetrics implementation
  • Updates lock operations to emit metrics for acquisition, release, retry attempts, and timing
  • Integrates metrics into the service collection registration for DI support

Reviewed Changes

Copilot reviewed 17 out of 17 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/DynamoDb.DistributedLock/Metrics/ New metrics infrastructure with interface, implementation, and metric name constants
src/DynamoDb.DistributedLock/DynamoDbDistributedLock.cs Added metrics tracking to lock acquisition and release operations
src/DynamoDb.DistributedLock/Retry/ExponentialBackoffRetryPolicy.cs Added retry metrics tracking and ILockMetrics dependency
src/DynamoDb.DistributedLock/Extensions/ServiceCollectionExtensions.cs Registered default LockMetrics instance in DI container
test/ Comprehensive test infrastructure and updates for metrics validation
README.md Added telemetry documentation with OpenTelemetry configuration examples

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Copy link
Collaborator

@ncipollina ncipollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 Excellent work!!!!

@ncipollina ncipollina merged commit 3b04a65 into LayeredCraft:main Aug 28, 2025
1 check passed
@TylerReid
Copy link
Collaborator Author

Thanks! I want you to know that I am now jealous of your copilot pr bot setup when I am working in enterprise GitHub. At first I wasn't sure about it's usefulness, but it does seem to have a decent rate of catching real issues.

@TylerReid TylerReid deleted the addMetrics branch August 28, 2025 12:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: Publish metrics

2 participants