Skip to content
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

Gauges should be registered with registerWithReplacement #1123

Merged
merged 6 commits into from
Dec 17, 2019

Conversation

carterkozak
Copy link
Contributor

After this PR

==COMMIT_MSG==
Using TaggedMetricRegistry.gauge is equivalent to map.putIfAbsent, and
can result in subtle resource leaks. Prefer replacing existing gauge
values using registerWithReplacement.

This check doesn't apply unless a new enough version of Tritium
is available on the compilation classpath.
==COMMIT_MSG==

Possible downsides?

It's possible some consumers expect subsequent registrations not to replace the existing gauge, however in the scenarios we're aware of, the return value is used in these cases.

@changelog-app
Copy link

changelog-app bot commented Dec 13, 2019

Generate changelog in changelog/@unreleased

Type

  • Feature
  • Improvement
  • Fix
  • Break
  • Deprecation
  • Manual task
  • Migration

Description

Using TaggedMetricRegistry.gauge is equivalent to map.putIfAbsent, and
can result in subtle resource leaks. Prefer replacing existing gauge
values using registerWithReplacement.

This check doesn't apply unless a new enough version of Tritium
is available on the compilation classpath.

Check the box to generate changelog(s)

  • Generate changelog entry

Using TaggedMetricRegistry.gauge is equivalent to map.putIfAbsent, and
can result in subtle resource leaks. Prefer replacing existing gauge
values using `registerWithReplacement`.

This check doesn't apply unless a new enough version of Tritium
is available on the compilation classpath.
@carterkozak carterkozak force-pushed the ckozak/UnsafeGaugeRegistration branch from 7975232 to a35150e Compare December 14, 2019 05:11
@BeforeTemplate
void before(TaggedMetricRegistry registry, MetricName name, Gauge<T> gauge) {
registry.remove(name);
registry.registerWithReplacement(name, gauge);
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we have a template to replace following pattern with registry.registerWithReplacement(name, gauge);?

    @BeforeTemplate
    void before(TaggedMetricRegistry registry, MetricName name, Gauge<T> gauge) {
        registry.remove(name);
        registry.gauge(name, gauge);
    }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't want to create a change that doesn't compile on old versions of Tritium (pre 0.16.1). The error-prone check only runs on new tritium, and transforms registry.gauge(name, gauge) into registry.registerWithReplacement(name, gauge), allowing this rule to do the rest.
Documented in the class level javadoc :-)

* This implementation is forked from upstream error-prone to work around a bug that results in
* parameters being renamed in some scenarios: https://github.com/google/error-prone/issues/1451.
*/
public static SuggestedFix renameMethodInvocation(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@carterkozak
Copy link
Contributor Author

Validated in a large internal project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants