Class: NewRelic::Agent::RequestSampler
- Inherits:
-
Object
- Object
- NewRelic::Agent::RequestSampler
- Includes:
- MonitorMixin, Coerce
- Defined in:
- lib/new_relic/agent/request_sampler.rb
Constant Summary collapse
- CONFIG_NAMESPACE =
The namespace and keys of config values
'request_sampler'
- MAX_SAMPLES_KEY =
"#{CONFIG_NAMESPACE}.max_samples".to_sym
- ENABLED_KEY =
"#{CONFIG_NAMESPACE}.enabled".to_sym
- SAMPLE_TYPE =
The type field of the sample
'Transaction'
- TYPE_KEY =
Strings for static keys of the sample structure
'type'
- TIMESTAMP_KEY =
'timestamp'
- NAME_KEY =
'name'
- DURATION_KEY =
'duration'
Instance Method Summary collapse
-
#initialize(event_listener) ⇒ RequestSampler
constructor
A new instance of RequestSampler.
- #notify_full ⇒ Object
-
#on_transaction_finished(metric, start_timestamp, duration, options = {}) ⇒ Object
Event handler for the :transaction_finished event.
- #record_sampling_rate(request_count, sample_count) ⇒ Object
- #register_config_callbacks ⇒ Object
-
#reset ⇒ Object
Clear any existing samples and reset the last sample time.
-
#samples ⇒ Object
Fetch a copy of the sampler’s gathered samples.
Methods included from Coerce
#float, #int, #log_failure, #string
Constructor Details
#initialize(event_listener) ⇒ RequestSampler
Returns a new instance of RequestSampler.
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/new_relic/agent/request_sampler.rb', line 29 def initialize( event_listener ) super() @enabled = false @samples = ::NewRelic::Agent::SampledBuffer.new(NewRelic::Agent.config[MAX_SAMPLES_KEY]) @notified_full = false event_listener.subscribe( :transaction_finished, &method(:on_transaction_finished) ) self.register_config_callbacks end |
Instance Method Details
#notify_full ⇒ Object
97 98 99 100 |
# File 'lib/new_relic/agent/request_sampler.rb', line 97 def notify_full NewRelic::Agent.logger.debug "Request Sampler capacity of #{@samples.capacity} reached, beginning sampling" @notified_full = true end |
#on_transaction_finished(metric, start_timestamp, duration, options = {}) ⇒ Object
Event handler for the :transaction_finished event.
103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/new_relic/agent/request_sampler.rb', line 103 def on_transaction_finished( metric, , duration, ={} ) return unless @enabled sample = { TIMESTAMP_KEY => float(), NAME_KEY => string(metric), DURATION_KEY => float(duration), TYPE_KEY => SAMPLE_TYPE }.merge() is_full = self.synchronize { @samples.append(sample) } notify_full if is_full && !@notified_full end |
#record_sampling_rate(request_count, sample_count) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/new_relic/agent/request_sampler.rb', line 67 def record_sampling_rate(request_count, sample_count) request_count_lifetime = @samples.seen_lifetime sample_count_lifetime = @samples.captured_lifetime NewRelic::Agent.logger.debug("Sampled %d / %d (%.1f %%) requests this cycle, %d / %d (%.1f %%) since startup" % [ sample_count, request_count, (sample_count.to_f / request_count * 100.0), sample_count_lifetime, request_count_lifetime, (sample_count_lifetime.to_f / request_count_lifetime * 100.0) ]) engine = NewRelic::Agent.instance.stats_engine engine.record_supportability_metric_count("RequestSampler/requests", request_count) engine.record_supportability_metric_count("RequestSampler/samples", sample_count) end |
#register_config_callbacks ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/new_relic/agent/request_sampler.rb', line 84 def register_config_callbacks NewRelic::Agent.config.register_callback(MAX_SAMPLES_KEY) do |max_samples| NewRelic::Agent.logger.debug "RequestSampler max_samples set to #{max_samples}" self.synchronize { @samples.capacity = max_samples } self.reset end NewRelic::Agent.config.register_callback(ENABLED_KEY) do |enabled| NewRelic::Agent.logger.info "%sabling the Request Sampler." % [ enabled ? 'En' : 'Dis' ] @enabled = enabled end end |
#reset ⇒ Object
Clear any existing samples and reset the last sample time. (Synchronized)
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/new_relic/agent/request_sampler.rb', line 52 def reset NewRelic::Agent.logger.debug "Resetting RequestSampler" sample_count, request_count = 0 self.synchronize do sample_count = @samples.size request_count = @samples.seen @samples.reset @notified_full = false end record_sampling_rate(request_count, sample_count) if @enabled end |
#samples ⇒ Object
Fetch a copy of the sampler’s gathered samples. (Synchronized)
46 47 48 |
# File 'lib/new_relic/agent/request_sampler.rb', line 46 def samples return self.synchronize { @samples.to_a } end |