Class: BrainzLab::Instrumentation::GrapeInstrumentation::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/brainzlab/instrumentation/grape.rb

Overview

Middleware for Grape (alternative installation) Usage: use BrainzLab::Instrumentation::GrapeInstrumentation::Middleware

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



194
195
196
# File 'lib/brainzlab/instrumentation/grape.rb', line 194

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/brainzlab/instrumentation/grape.rb', line 198

def call(env)
  return @app.call(env) unless should_trace?

  started_at = Time.now.utc
  request = Rack::Request.new(env)

  # Initialize Pulse tracing
  Thread.current[:brainzlab_pulse_spans] = []
  Thread.current[:brainzlab_pulse_breakdown] = nil

  # Extract parent trace context
  parent_context = BrainzLab::Pulse.extract!(env)

  begin
    status, headers, response = @app.call(env)

    record_trace(request, env, started_at, status, parent_context)

    [status, headers, response]
  rescue StandardError => e
    record_trace(request, env, started_at, 500, parent_context, e)
    raise
  ensure
    cleanup_context
  end
end