Skip to content

Let clients of DropwizardExports handle validation errors #1354

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 1 commit into from
May 23, 2025

Conversation

mnssn
Copy link
Contributor

@mnssn mnssn commented May 14, 2025

A possible solution for #1353.

In the current form, if e.g. a Counter contains invalid data the entire collect calls fails with InvalidArgumentException.

In large applications, it may be preferable to gracefully degrade the available metrics and report the failure via e.g. logging. This change allows just that - by changing the access modifiers to the fromXYZ methods subclasses can now handle the errors.

Finally, to allow for skipping of invalid metrics, we follow the example in fromGauge to interpret the meaning of a returned null to be that the metric should be skipped.

@zeitlinger
Copy link
Member

I like the idea of making this customizable.

Instead of subclassing, I'd prefer to use a callback and builder - would that work?

@mnssn
Copy link
Contributor Author

mnssn commented May 22, 2025

Yes - that is a much cleaner solution. I have updated the PR to allow the callback to decide if an exception is to be suppressed.

@mnssn mnssn force-pushed the main branch 5 times, most recently from 56cb38c to 292a1bc Compare May 22, 2025 13:09
Copy link
Member

@zeitlinger zeitlinger left a comment

Choose a reason for hiding this comment

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

great

@@ -179,7 +202,7 @@ MetricSnapshot fromTimer(String dropwizardName, Timer timer) {
}

/** Export a Meter as a prometheus COUNTER. */
MetricSnapshot fromMeter(String dropwizardName, Meter meter) {
protected MetricSnapshot fromMeter(String dropwizardName, Meter meter) {
Copy link
Member

Choose a reason for hiding this comment

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

should not have to be protected

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

* @param metricFilter a custom metric filter.
* @param labelMapper a labelMapper to use to map labels.
*/
public DropwizardExports(
Copy link
Member

Choose a reason for hiding this comment

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

this ctor should be private - i.e. you have to use the builder for that

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

In the current form, if e.g. a Counter contains invalid data the entire `collect` calls fails with
InvalidArgumentException.

In large applications, it may be preferable to gracefully degrade the available metrics and report
the failure via e.g. logging. This change allows that by letting the user add a exception handler.

This allows the user to inspect the exception, and the metric name it was associated with, and
decide if the exception should be suppressed or not.

Metrics for which exceptions were suppressed will be absent from the collection.

Signed-off-by: Anders Månsson <[email protected]>
@mnssn mnssn changed the title Allow subclasses of DropwizardExports to deal with validation errors Let clients of DropwizardExports handle validation errors May 22, 2025
@zeitlinger zeitlinger enabled auto-merge (squash) May 23, 2025 07:34
@zeitlinger zeitlinger merged commit 6c82338 into prometheus:main May 23, 2025
7 checks passed
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.

2 participants