Skip to content

Commit

Permalink
Add by-path logs
Browse files Browse the repository at this point in the history
  • Loading branch information
thesecretmaster committed Mar 18, 2019
1 parent 948f23a commit b2bab68
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 2 deletions.
13 changes: 13 additions & 0 deletions app/controllers/redis_log_controller.rb
Expand Up @@ -110,6 +110,19 @@ def by_status
end.reject(&:empty?)
end

def by_path
redis = redis(logger: true)
@method = params[:method]
@path = params[:path]
@format = params[:format]
si, ei = page
@requests = redis.zrevrange("requests/by_path/#{@method}/#{@path}.#{@format}", si, ei, with_scores: true).to_a.map do |request_id, timestamp|
r = get_request(timestamp, request_id)
redis.zrem "requests/by_path/#{@method}/#{@path}.#{@format}", request_id if r.empty?
r
end.reject(&:empty?)
end

private

def get_request(timestamp, request_id)
Expand Down
2 changes: 2 additions & 0 deletions app/views/redis_log/_row.html.erb
Expand Up @@ -12,6 +12,8 @@
<%= req['user_id'].nil? ? "No User" : link_to("user", redis_log_by_user_path(id: req['user_id'])) %>
&middot;
<%= link_to("request", redis_log_request_path(req.slice(:request_id, :timestamp))) %>
&middot;
<%= %w[method path format].all? { |i| req[i].present? } ? link_to("path", redis_log_by_path_path(method: req['method'], format: req['format'], path: Rails.sensible_routes.match_for(req['path'])&.path || req['path'].split('?').first)) : 'No Path' %>
</p>
<h4><code><%= req['controller'] %>#<%= req['action'] %> (<%= req['path'] %>.<%= req['format'] %>)</code></h4>
<% if !req[:logs].nil? %>
Expand Down
3 changes: 3 additions & 0 deletions app/views/redis_log/by_path.html.erb
@@ -0,0 +1,3 @@
<h1><%= @method %> <%= @path %>.<%= @format %></h1>

<%= render 'table', requests: @requests %>
7 changes: 5 additions & 2 deletions config/initializers/redis_logging.rb
Expand Up @@ -4,7 +4,7 @@

REDIS_LOG_EXPIRATION = 1.day.seconds.to_i

def log_timestamps(ts, status:, action:, controller:, format:, method:, view_runtime:, db_runtime:, path:) # rubocop:disable Metrics/ParameterLists
def log_timestamps(ts, status:, action:, controller:, format:, method:, view_runtime:, db_runtime:, path:, uuid:) # rubocop:disable Metrics/ParameterLists
redis = redis(logger: true)
return if path.nil?
path = Rails.sensible_routes.match_for(path)&.path || path.split('?').first
Expand All @@ -20,6 +20,9 @@ def log_timestamps(ts, status:, action:, controller:, format:, method:, view_run
redis.zadd "request_timings/status_counts/by_action/#{controller}##{action}", ts, status
redis.zadd 'request_timings/status_counts', ts, status

redis.zadd "requests/by_path/#{method.upcase}/#{path}.#{format}", ts, uuid
redis.zadd "requests/by_action/#{controller}##{action}", ts, uuid

redis.zadd 'request_timings/sha', ts, CurrentCommit, nx: true
end

Expand All @@ -46,7 +49,7 @@ def log_timestamps(ts, status:, action:, controller:, format:, method:, view_run
:action, :controller,
:view_runtime, :db_runtime,
:method, :format, :status
), path: redis.hget(redis_log_key, 'path'))
), path: redis.hget(redis_log_key, 'path'), uuid: request_id)
end
end
end
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Expand Up @@ -212,6 +212,7 @@
root to: 'redis_log#index'
get 'user/:id', to: 'redis_log#by_user', as: :redis_log_by_user
get 'status/:status', to: 'redis_log#by_status', as: :redis_log_by_status
get 'by_path/:method/:path', to: 'redis_log#by_path', as: :redis_log_by_path
get 'session/:id', to: 'redis_log#by_session', as: :redis_log_by_session
scope 'request/:timestamp/:request_id', constraints: {:timestamp => /[^\/]+/ } do
root to: 'redis_log#show', as: :redis_log_request
Expand Down

0 comments on commit b2bab68

Please sign in to comment.