Skip to content

Instantly share code, notes, and snippets.

@jeremy
Created August 7, 2009 05:35
Show Gist options
  • Save jeremy/163744 to your computer and use it in GitHub Desktop.
Save jeremy/163744 to your computer and use it in GitHub Desktop.
Track object allocations in NewRelic
<table width="100%">
<tr><td width="50%">
{% compare_with_last_week_chart metric:'Custom/Objects/Allocated' title:'Object Allocations' value:total_value %}
</td><td width="50%">
{% compare_with_last_week_chart metric:'Custom/Objects/Live' title:'Live Objects' value:average_value %}
</td></tr>
</table>
module Trashed
class Sampler < NewRelic::Agent::Sampler
def initialize
super self.class::LABEL.sub(/Sampler$/, '').underscore.to_sym
end
def poll
stats.record_data_point(sample)
end
protected
def stats
stats_engine.get_stats(self.class::LABEL, false)
end
end
class PointSampler < Sampler
def install!(agent)
agent.stats_engine.add_sampler(self) if self.class.available?
end
def sample
measure
end
end
class DeltaSampler < Sampler
def initialize
super
mark!
end
def install!(agent)
agent.stats_engine.add_harvest_sampler(self) if self.class.available?
end
def sample
old = @mark
mark!
@mark - old
end
protected
def mark!
@mark = measure
end
end
class LiveObjectsSampler < PointSampler
LABEL = 'Custom/Objects/Live'
def self.available?
ObjectSpace.respond_to?(:live_objects)
end
def measure
ObjectSpace.live_objects
end
end
class AllocatedObjectsSampler < DeltaSampler
LABEL = 'Custom/Objects/Allocated'
def self.available?
ObjectSpace.respond_to?(:allocated_objects)
end
def measure
ObjectSpace.allocated_objects
end
end
end
agent = NewRelic::Agent.instance
Trashed::LiveObjectsSampler.new.install!(agent)
Trashed::AllocatedObjectsSampler.new.install!(agent)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment