28
29
30
31
32
33
34
35
36
37
38
39
40
41
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/rails_observatory/middleware.rb', line 28
def call(env)
start_at = Time.now
request = ActionDispatch::Request.new(env)
start_at_mono = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)
response = nil
events, logs = collect_events_and_logs do
response = @app.call(env)
end
controller_action = "#{request.params[:controller]}##{request.params[:action]}"
if (event = events.find { _1.name == 'process_action.action_controller' && _1.payload[:exception_object] })
error = Error.new(exception: event.payload[:exception_object], location: controller_action, time: Time.now)
error.save
TimeSeries.record_occurrence("error.count", labels: { fingerprint: error.fingerprint })
end
return response if request.params[:controller].blank? || request.params[:controller] =~ /rails_observatory/
status, , body = response
body = ::Rack::BodyProxy.new(body) do
duration = (Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond) - start_at_mono)
serialized_events = events.map { Serializer.serialize(_1) }
RequestTrace.new(
request_id: request.request_id,
status:,
http_method: request.method,
route_pattern: request.route_uri_pattern,
action: controller_action,
error: events.any? { _1.payload[:exception] },
format: request.format,
duration:,
time: start_at.to_f,
path: request.path,
events: serialized_events,
logs:
).save
labels = { action: controller_action, format: request.format, status:, http_method: request.method }
TimeSeries.record_occurrence("request.count", labels:)
TimeSeries.record_occurrence("request.error_count", labels:) if status >= 500
TimeSeries.record_timing("request.latency", duration, labels:)
EventCollection.new(serialized_events).self_time_by_library.each do |library, self_time|
TimeSeries.record_timing("request.latency/#{library}", self_time, labels: { action: controller_action })
end
rescue => e
puts e
puts e.backtrace.join("\n")
end
[status, , body]
end
|