Class: ScoutApm::Extensions::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/scout_apm/extensions/config.rb

Overview

!!! Extensions are a 0.x level API and breakage is expected as the API is refined. Extensions fan out data collected by the agent to additional services.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(agent_context) ⇒ Config

Returns a new instance of Config.



33
34
35
36
37
# File 'lib/scout_apm/extensions/config.rb', line 33

def initialize(agent_context)
  @agent_context = agent_context
  @transaction_callbacks = []
  @periodic_callbacks = []
end

Instance Attribute Details

#agent_contextObject (readonly)

Returns the value of attribute agent_context.



6
7
8
# File 'lib/scout_apm/extensions/config.rb', line 6

def agent_context
  @agent_context
end

#periodic_callbacksObject

Returns the value of attribute periodic_callbacks.



8
9
10
# File 'lib/scout_apm/extensions/config.rb', line 8

def periodic_callbacks
  @periodic_callbacks
end

#transaction_callbacksObject

Returns the value of attribute transaction_callbacks.



7
8
9
# File 'lib/scout_apm/extensions/config.rb', line 7

def transaction_callbacks
  @transaction_callbacks
end

Class Method Details

.add_periodic_callback(callback) ⇒ Object

Adds a callback that runs when the per-minute report data is sent to Scout. These run in a background thread so external HTTP calls are OK. callback must be an object that responds to a call(reporting_period, metadata) method.

Example: ScoutApm::Extensions::Config.add_periodic_callback(Proc.new { |reporting_period, metadata| … })



29
30
31
# File 'lib/scout_apm/extensions/config.rb', line 29

def self.add_periodic_callback(callback)
  agent_context.extensions.periodic_callbacks << callback
end

.add_transaction_callback(callback) ⇒ Object

Adds a new callback that runs after a transaction completes. These run inline during the request and thus should add minimal overhead. For example, a transaction callback should NOT make inline HTTP calls to outside services. callback must be an object that respond to a call(payload) method.

Example: ScoutApm::Extensions::Config.add_transaction_callback(Proc.new { |payload| puts “Duration: #payloadpayload.duration_ms” })

payload is a ScoutApm::Extensions::TransactionCallbackPayload object.



19
20
21
# File 'lib/scout_apm/extensions/config.rb', line 19

def self.add_transaction_callback(callback)
  agent_context.extensions.transaction_callbacks << callback
end

.agent_contextObject



77
78
79
# File 'lib/scout_apm/extensions/config.rb', line 77

def self.agent_context
  ScoutApm::Agent.instance.context
end

Instance Method Details

#loggerObject



81
82
83
# File 'lib/scout_apm/extensions/config.rb', line 81

def logger
  agent_context.logger
end

#run_periodic_callbacks(reporting_period, metadata) ⇒ Object

Runs each reporting period callback. Each callback runs inside a begin/rescue block so a broken callback doesn’t prevent other callbacks from executing or reporting data from being sent.



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/scout_apm/extensions/config.rb', line 42

def run_periodic_callbacks(reporting_period, )
  return unless periodic_callbacks.any?

  periodic_callbacks.each do |callback|
    begin
      callback.call(reporting_period, )
    rescue => e
      logger.warn "Error running reporting callback extension=#{callback}"
      logger.info e.message
      logger.debug e.backtrace
    end
  end
end

#run_transaction_callbacks(converter_results, context, scope_layer) ⇒ Object

Runs each transaction callback. Each callback runs inside a begin/rescue block so a broken callback doesn’t prevent other callbacks from executing or the transaction from being recorded.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/scout_apm/extensions/config.rb', line 59

def run_transaction_callbacks(converter_results, context, scope_layer)
  # It looks like layer_finder.scope = nil when a Sidekiq job is retried
  return unless scope_layer
  return unless transaction_callbacks.any?

  payload = ScoutApm::Extensions::TransactionCallbackPayload.new(agent_context,converter_results,context,scope_layer)

  transaction_callbacks.each do |callback|
    begin
      callback.call(payload)
    rescue => e
      logger.warn "Error running transaction callback extension=#{callback}"
      logger.info e.message
      logger.debug e.backtrace
    end
  end
end