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
- MAX_SAMPLES_KEY =
The namespace and keys of config values
:'analytics_events.max_samples_stored'
- ENABLED_KEY =
:'analytics_events.enabled'
- ENABLED_TXN_KEY =
:'analytics_events.transactions.enabled'
- 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
-
#harvest ⇒ Object
(also: #reset)
Clear any existing samples, reset the last sample time, and return the previous set of samples.
-
#initialize(event_listener) ⇒ RequestSampler
constructor
A new instance of RequestSampler.
-
#merge(old_samples) ⇒ Object
Merge samples back into the buffer, for example after a failed transmission to the collector.
- #notify_full ⇒ Object
-
#on_transaction_finished(payload) ⇒ Object
Event handler for the :transaction_finished event.
- #record_sampling_rate(request_count, sample_count) ⇒ Object
- #register_config_callbacks ⇒ Object
-
#samples ⇒ Object
Fetch a copy of the sampler’s gathered samples.
Methods included from Coerce
#float, #int, #int_or_nil, #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
#harvest ⇒ Object Also known as: reset
Clear any existing samples, reset the last sample time, and return the previous set of samples. (Synchronized)
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/new_relic/agent/request_sampler.rb', line 52 def harvest NewRelic::Agent.logger.debug "Resetting RequestSampler" sample_count, request_count = 0 old_samples = nil self.synchronize do sample_count = @samples.size request_count = @samples.seen old_samples = @samples.to_a @samples.reset @notified_full = false end record_sampling_rate(request_count, sample_count) if @enabled old_samples end |
#merge(old_samples) ⇒ Object
Merge samples back into the buffer, for example after a failed transmission to the collector. (Synchronized)
74 75 76 77 78 |
# File 'lib/new_relic/agent/request_sampler.rb', line 74 def merge(old_samples) self.synchronize do old_samples.each { |s| @samples.append(s) } end end |
#notify_full ⇒ Object
115 116 117 118 |
# File 'lib/new_relic/agent/request_sampler.rb', line 115 def notify_full NewRelic::Agent.logger.debug "Request Sampler capacity of #{@samples.capacity} reached, beginning sampling" @notified_full = true end |
#on_transaction_finished(payload) ⇒ Object
Event handler for the :transaction_finished event.
121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/new_relic/agent/request_sampler.rb', line 121 def on_transaction_finished(payload) return unless @enabled return unless NewRelic::Agent::Transaction.transaction_type_is_web?(payload[:type]) sample = { TIMESTAMP_KEY => float(payload[:start_timestamp]), NAME_KEY => string(payload[:name]), DURATION_KEY => float(payload[:duration]), TYPE_KEY => SAMPLE_TYPE }.merge((payload[:overview_metrics] || {})) is_full = self.synchronize { @samples.append(sample) } notify_full if is_full && !@notified_full end |
#record_sampling_rate(request_count, sample_count) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/new_relic/agent/request_sampler.rb', line 80 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
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/new_relic/agent/request_sampler.rb', line 97 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 && NewRelic::Agent.config[ENABLED_TXN_KEY] end NewRelic::Agent.config.register_callback(ENABLED_TXN_KEY) do |enabled| NewRelic::Agent.logger.info "%sabling the Request Sampler." % [ enabled ? 'En' : 'Dis' ] @enabled = enabled && NewRelic::Agent.config[ENABLED_KEY] end 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 |