Prevent misuse of compiled metrics#418
Merged
Merged
Conversation
pedro-stanaka
left a comment
Contributor
There was a problem hiding this comment.
We dont have that many downstream clients of this so far, I guess we can afford the "breaking" change.
Please add change log for this, and also decide if you want to bump version on this or in follow up PR.
Calling `define` directly on a base metric type class, e.g.
MyCounter = CompiledMetric::Counter.define(name: "my_metric")
appears to work until you define another metric of the same type, where
it starts to misbehavior.
This is because definition sets class instance variables and redefines
the metric method on the base class itself, silently polluting state for
all future subclasses.
This change adds two guards to `define`:
1. A base-class guard raises `ArgumentError` when `define` is called
on any of the base classes directly, with a message showing the
correct subclass pattern.
2. A double-define guard raises `ArgumentError` when `define` is called
a second time on the same class, preventing accidental state
pollution and inconsistent behavior.
Replace `ArgumentError` with a dedicated `DefinitionError` for all definition-time guards in `CompiledMetric`: - Calling `define` on a base type class directly - Calling `define` more than once on the same class - Using a metric before `define` has been called This makes definition errors independently rescuable and distinguishes them from genuine argument errors (e.g. unsupported tag types, accessing `sample_rate` before definition) and gives a consistent experience across error types.
489e5e1 to
aeb632f
Compare
Contributor
Author
It looks like the normal release pattern is to do a bump in a discrete PR so I figured that is what I would do. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
✅ What
This change prevents accidental misuse of the
CompiledMetricsystem by ensuring that you cannotdefinethe base class, as that mutates shared, global state.Additionally, the second commit introduces a "definition error" instead of using
ArgumentErrorfor definiton-time issues, as they aren't errors with passing invalid arguments, but with how you're setting up your definition. (Technically, this is a breaking change, but I doubt people are rescuing theseArgumentErrors, so it should be safe. I opted for a second commit so we can just not ship that if we want to avoid this "breaking change".)🤔 Why
I saw that the API was
CompiledMetric.defineand assumed it worked like theDatastructs. It appeared to work until I went to define a second metric, which caused things to break unexpectedly.👩🔬 How to validate
I believe the test suite covers the behavior.
Checklist