Class: AppMap::Middleware::RemoteRecording

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

Overview

RemoteRecording adds ‘/_appmap/record` routes to control recordings via HTTP requests

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ RemoteRecording

Returns a new instance of RemoteRecording.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/appmap/middleware/remote_recording.rb', line 8

def initialize(app)
  require 'appmap/command/record'
  require 'appmap/command/upload'
  require 'appmap/trace/tracer'
  require 'appmap/config'
  require 'json'

  @app = app
  @features = AppMap.inspect(config)
  @functions = @features.map(&:collect_functions).flatten
end

Instance Method Details

#call(env) ⇒ Object



81
82
83
84
85
86
# File 'lib/appmap/middleware/remote_recording.rb', line 81

def call(env)
  req = Rack::Request.new(env)
  return handle_record_request(req) if req.path == '/_appmap/record'

  @app.call(env)
end

#configObject



116
117
118
# File 'lib/appmap/middleware/remote_recording.rb', line 116

def config
  @config ||= AppMap::Config.load_from_file 'appmap.yml'
end

#event_loopObject



20
21
22
23
24
25
26
27
28
29
# File 'lib/appmap/middleware/remote_recording.rb', line 20

def event_loop
  loop do
    event = @tracer.next_event if @tracer
    if event
      @events << event.to_h
    else
      sleep 0.0001
    end
  end
end

#handle_record_request(req) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/appmap/middleware/remote_recording.rb', line 92

def handle_record_request(req)
  method = req.env['REQUEST_METHOD']

  status, body = \
    if method.eql?('GET')
      recording_state
    elsif method.eql?('POST')
      start_recording
    elsif method.eql?('DELETE')
      stop_recording(req)
    else
      [ 404, '' ]
    end

  status = 200 if status == true
  status = 500 if status == false

  [status, { 'Content-Type' => 'application/text' }, [body || '']]
end

#html_response?(headers) ⇒ Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/appmap/middleware/remote_recording.rb', line 112

def html_response?(headers)
  headers['Content-Type'] && headers['Content-Type'] =~ /html/
end

#recording?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/appmap/middleware/remote_recording.rb', line 120

def recording?
  !@event_thread.nil?
end

#recording_stateObject



88
89
90
# File 'lib/appmap/middleware/remote_recording.rb', line 88

def recording_state
  [ 200, JSON.generate({ enabled: recording? }) ]
end

#start_recordingObject



31
32
33
34
35
36
37
38
39
40
# File 'lib/appmap/middleware/remote_recording.rb', line 31

def start_recording
  return [ false, 'Recording is already in progress' ] if @tracer

  @events = []
  @tracer = AppMap::Trace.tracers.trace(@functions)
  @event_thread = Thread.new { event_loop }
  @event_thread.abort_on_exception = true

  [ true ]
end

#stop_recording(req) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/appmap/middleware/remote_recording.rb', line 42

def stop_recording(req)
  return [ false, 'No recording is in progress' ] unless @tracer

  tracer = @tracer
  @tracer = nil

  AppMap::Trace.tracers.delete(tracer)

  @event_thread.exit
  @event_thread.join
  @event_thread = nil

  # Delete the events which are calls to or returns from the URL path _appmap/record
  # because these are not of interest to the user.
  is_control_command_event = lambda do |event|
    event[:event] == :call &&
      event[:http_server_request] &&
      event[:http_server_request][:path_info] == '/_appmap/record'
  end
  control_command_events = @events.select(&is_control_command_event)

  is_return_from_control_command_event = lambda do |event|
    event[:parent_id] && control_command_events.find { |e| e[:id] == event[:parent_id] }
  end

  @events.delete_if(&is_control_command_event)
  @events.delete_if(&is_return_from_control_command_event)

  require 'appmap/command/record'
   = AppMap::Command::Record.
  [:recorder] = {
    name: 'remote_recording'
  }

  response = JSON.generate(version: AppMap::APPMAP_FORMAT_VERSION, classMap: @features, metadata: , events: @events)

  [ true, response ]
end