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

Allow subclasses to add metrics to MetricsRegistry #112

Conversation

chids
Copy link
Contributor

@chids chids commented Nov 17, 2011

This is primarily for testing purposes: being able to subclass MetricsRegistry and use getOrAdd() from the subclass to add manually constructed metrics (and possibly mock objects).

I made getOrAdd() final since I feels its logic is an integral part of MetricsRegistry that you shouldn't tamper with it. Might be to conservative?

Also took the opportunity to use getOrAdd in newMeter and newTimer.

…rimarily for tests)

Also, use getOrAdd() for Timers and Meters
@codahale
Copy link
Contributor

I'm OK with making getOrAdd() protected, but the reason for newMeter and newTimer having different logic is that it tries harder to not unnecessarily create duplicate timers or meters. The tick threads keep them around until you stop them, which can lead to memory leaks.

@codahale codahale closed this in cf669aa Nov 17, 2011
@chids
Copy link
Contributor Author

chids commented Nov 17, 2011

Yep, but does this patch actually change the behaviour for adding Timer/Meters? I at least thought I only removed duplication (I'm not in front of a computer right now).

/mårten.

On 17 nov 2011, at 23:57, Coda Halereply@reply.github.com wrote:

I'm OK with making getOrAdd() protected, but the reason for newMeter and newTimer having different logic is that it tries harder to not unnecessarily create duplicate timers or meters. The tick threads keep them around until you stop them, which can lead to memory leaks.


Reply to this email directly or view it on GitHub:
https://github.com/codahale/metrics/pull/112#issuecomment-2783345

@codahale
Copy link
Contributor

It went from creating the metric after checking to creating the metric before checking. I've got one more fix for this and it should be fine.

@chids
Copy link
Contributor Author

chids commented Nov 17, 2011

Either I'm way too tired or we're not reading the same diff ;) Since I didn't touch/reorder the line creating instance I fail to see how that could be the case

On 18 nov 2011, at 00:17, Coda Halereply@reply.github.com wrote:

It went from creating the metric after checking to creating the metric before checking. I've got one more fix for this and it should be fine.


Reply to this email directly or view it on GitHub:
https://github.com/codahale/metrics/pull/112#issuecomment-2783552

@codahale
Copy link
Contributor

When getOrAdd is called, its metric parameter is already created. So newTimer went from this:

final Metric existingMetric = metrics.get(metricName);
if (existingMetric == null) {
    final TimerMetric metric = new TimerMetric(newMeterTickThreadPool(), durationUnit, rateUnit);
    // etc
}

to (effectively) this:

final TimerMetric metric = new TimerMetric(newMeterTickThreadPool(), durationUnit, rateUnit);
final Metric existingMetric = metrics.get(metricName);
if (existingMetric == null) {
    // etc.
}

@chids
Copy link
Contributor Author

chids commented Nov 17, 2011

The check (line 285 to 287 in the diff) is untouched and thus performed before getOrAdd and before creating the metric instance.

I noticed now however that my patch caused the check to be carried out twice. Once in newXYZ (before creating the metric) and once in getOrAdd (when the metric have been created).

On 18 nov 2011, at 00:36, Coda Halereply@reply.github.com wrote:

When getOrAdd is called, its metric parameter is already created. So newTimer went from this:

final Metric existingMetric = metrics.get(metricName);
if (existingMetric == null) {
   final TimerMetric metric = new TimerMetric(newMeterTickThreadPool(), durationUnit, rateUnit);
   // etc
}

to (effectively) this:

final TimerMetric metric = new TimerMetric(newMeterTickThreadPool(), durationUnit, rateUnit);
final Metric existingMetric = metrics.get(metricName);
if (existingMetric == null) {
   // etc.
}

Reply to this email directly or view it on GitHub:
https://github.com/codahale/metrics/pull/112#issuecomment-2783699

@codahale
Copy link
Contributor

Ah well, so it did.

sigh

I really need to stop trying to read diffs on GitHub.

Sorry about that.

@chids
Copy link
Contributor Author

chids commented Nov 18, 2011

No worries. You really had me staring down those lines of code trying to figure out what I'd missed :D

On 18 nov 2011, at 02:07, Coda Halereply@reply.github.com wrote:

Ah well, so it did.

sigh

I really need to stop trying to read diffs on GitHub.

Sorry about that.


Reply to this email directly or view it on GitHub:
https://github.com/codahale/metrics/pull/112#issuecomment-2784481

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.

None yet

2 participants