Class: SolidApm::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/solid_apm/middleware.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



5
6
7
# File 'lib/solid_apm/middleware.rb', line 5

def initialize(app)
  @app = app
end

Class Method Details

.callObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/solid_apm/middleware.rb', line 23

def self.call
  transaction = SpanSubscriber::Base.transaction
  SpanSubscriber::Base.transaction = nil
  if transaction.nil? || transaction.name.start_with?('SolidApm::')
    SpanSubscriber::Base.spans = nil
    return
  end

  ApplicationRecord.transaction do
    transaction.save!

    SpanSubscriber::Base.spans.each do |span|
      span[:transaction_id] = transaction.id
    end
    SolidApm::Span.insert_all SpanSubscriber::Base.spans
  end
  SpanSubscriber::Base.spans = nil
end

.init_transactionObject



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/solid_apm/middleware.rb', line 42

def self.init_transaction
  now = Time.zone.now
  SpanSubscriber::Base.transaction = Transaction.new(
    uuid: SecureRandom.uuid,
    timestamp: now,
    unix_minute: (now.to_f / 60).to_i,
    type: 'request'
  )
  SpanSubscriber::Base.spans = []
  SpanSubscriber::Base.transaction
end

Instance Method Details

#call(env) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/solid_apm/middleware.rb', line 9

def call(env)
  self.class.init_transaction
  status, headers, body = @app.call(env)

    env['rack.after_reply'] ||= []
    env['rack.after_reply'] << ->() do
      self.class.call
    rescue StandardError => e
      Rails.logger.error e
      Rails.logger.error e.backtrace&.join("\n")
    end
  [status, headers, body]
end