I want made that all log will store database. therefore I try setup Ahoy gem in my Rails Api only Project. Ahoy is awesome gem. but there is not Best pratice on README.md of Ahoy github.
i hope store http request header, reponse header and body with Ahoy. this code is track_action for storing event in ApplicationController.
class ApplicationController < ActionController::Base
after_action :track_action
protected
def track_action
ahoy.track "Viewed #{controller_name}##{action_name}", { request: { header: request.headers },
response: { headers: response.headers, body: JSON.parse(response.body) } }
end
end
but i got error.
Completed 500 Internal Server Error in 493ms (Views: 34.8ms | ActiveRecord: 89.1ms | Allocations: 317174)
SystemStackError (stack level too deep):
app/controllers/application_controller.rb:14:in `track_action'
What is Best practice ahoy.track for storing all request, response log?
storing all http request, response with Ahoy gem
The issue is with
request.headersas it is quite complex and includes various attributes and methods, and attempting to track it directly is causing the problem. They are98headers in my case:which all can be checked:
Ways to Fix
Store only HTTP headers and reject environment variables from request:
Store only specific request attributes:
Performance Optimization
Decide which endpoints or actions are crucial for your analytics and log only those. Instead of logging the entire request or response (headers, body), choose only the necessary parts. You can also use background jobs instead of logging directly into the request/response cycle to avoid database performance issues.