rails_distributed_tracing
Distributed tracing for rails microservices
Installation
Add this line to Gemfile of your microservice:
gem 'rails_distributed_tracing'
Configuration
Add request id tag to log tags in application.rb. This config will make sure that the logs are tagged with a request id.
If the service is origin service, DistributedTracing.request_id_tag will create a new request id else it will reuse the request id passed from the origin service.
require 'rails_distributed_tracing'
class Application < Rails::Application
config. = [DistributedTracing.request_id_tag]
end
Passing request id tag to downstream services
To make the distributed tracing work every service has to the request id to all its downstream services.
For example lets assume that we have 3 services:
OriginService, SecondService and ThirdService .
Now a request comes to OriginService, the above config will create a new request_id for that request.
When OriginService makes a request to SecondService it should pass the same requst_id to it,
so that the request can be traced. The same should happen when SecondService calls the ThirdService.
The bottomline is whenever there is a communication between two services, source service should always pass the request id to the destination service.
How to pass request id tag to downstream services
When a request id is generated rails_distributed_tracing holds that request id till the request returns the response. You can access that request_id anywhere in your application code with the help of below APIs.
DistributedTracing.current_request_id
or you can directly get it as a request header
DistributedTracing.request_id_header
#=> {'Request-ID' => '8ed7e37b-94e8-4875-afb4-6b4cf1783817'}
The gem will automatically pick the Request-ID header from your request and use it as log tag.
Note: Make sure that you always pass the current request id and not the stale one.
Contributing
- Fork it ( https://github.com/ajitsing/rails_distributed_tracing/fork )
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request
License
MIT License
Copyright (c) 2018 Ajit Singh
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.