How to Generate a Unique Request Id in Rails

How to generate a unique request ID in Rails?

If you want to insert the request UUID at the log line Started GET "/" for 127.0.0.1 at Tue Feb 21 14:00:00 -0300 2012, you can patch or subclass Rails::Rack::Logger to modify the call_app method:

def call_app(env)
request = ActionDispatch::Request.new(env)
path = request.filtered_path
Rails.logger.info "\n\nStarted #{request.request_method} \"#{path}\" for #{request.ip} at #{Time.now.to_default_s}"
@app.call(env)
ensure
ActiveSupport::LogSubscriber.flush_all!
end

The request object is created right before the logging statement, so you can change the logging statement to include request.uuid.

How to generate a unique random id for post in Ruby on Rails 4?

From the Ruby doc:

This library is an interface for secure random number generator which is suitable for generating session key in HTTP cookies, etc.

I had similar problem, I used Digest library.

Digest::MD5.hexdigest(post.title + post.created_at.to_s) #=> "b4809d..."

Adding unique id in rails4 logger to identify the request and response

Since you control the contents of the log message at each step, you can yourself create and use a unique id every time the request is made. A ID based on timestamp would be unique.

One possible solution:

begin
unique_id = "ID-" + Time.now.strftime("%Y%m%d-%H%M%S")
@@logger.debug "#{unique_id}: The request #{url}"
response = RestClient.get '#{url}'
@@logger.debug "#{unique_id}: Successful response #{response}"
rescue => e
@@logger.debug "#{unique_id}: Failure response #{e.message}"
end

Log:

D, [2014-12-09T14:27:18.576498 #29871] DEBUG -- : ID-20141209-142718: The request https://api.bitfinex.com/v1/symbols
D, [2014-12-09T14:27:21.547365 #29874] DEBUG -- : ID-20141209-142718: Successful response ["btcusd","ltcusd","ltcbtc","drkusd","drkbtc","th1btc"]

rails 5 hide production output unique request identifier

In your config file you must have log_tags set to something like

config.log_tags = [ :uuid ]

Remove that and you should be good to go.

Log tags are beneficial for debugging - give this a read when you have time.

Best way to architect unique identifier generation

I would just generate a random string and assign it while it doesn't exist. Before I get into that let me just mention that SecureRandom.uuid is the best way to go. It generates random uuids whose chance of collision are mathematically improbable.

Anyway, here is a way to use your own custom random string generator that will only assign if it doesn't already exist:

def generate_random_uid
begin
uid = my_custom_random_string_method
object.uid = uid
end while ObjectModel.exists? uid: uid

object.save
end

The do while block will execute once and set a variable called uid to be a random string you generate in the method my_custom_random_string_method, then in the while part it checks if a record exists whose uid attribute matches the random uid you just generated, if it does it runs the do block again, rinse and repeat until the expression in the while part returns false meaning the uid doesn't exists, then your object is saved and the uid written to the db. This guarantees that you will only ever save the object with a uid that doesn't exist in your db.

Ruby on rails unique token for every ticket

For the unique token, make sure you have a token field in your Ticket model:

class Ticket < ActiveRecord::Base
before_create :generate_token

protected
def generate_token
self.token = loop do
random_token = SecureRandom.urlsafe_base64(nil, false)
break random_token unless Ticket.exists?(token: random_token)
end
end
end

To make new customer in ticket#new:

def new
@ticket = Ticket.new
@customer = Customer.new
end

and in your ticket#create action, you need to create the customer first with params[:customer] and then create the ticket with params[:ticket] and the customer_id where you got from the customer you just created.

clean way to read out id in a request path in ruby on rails

Have you tried what params return if routes are like this?

resources :photos do
resources :media
end


Related Topics



Leave a reply



Submit