Skip to content

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.

4 participants