Instrumenter
Instrumenter is a small gem that was created to allow logging with a single request id across multiple microservice backends. The idea here is that you set an X-Request-Id header on your edge, and this Gem will allow you to propagate that value through all your various services. For example:
- CDN edge generates a request id (setting the
X-Request-Idheader) - The request goes to your first Rack microservice, which is a Rails app
- The Rails app then calls another microservice. You add
Instrumenter.log_event(message)around your call to the second service, and set theX-Request-Idheader on the call to the second service to the value ofInstrumenter.instance.request_id - The second service receives the same value for
X-Request-Idthat was generated at the edge - The second (and any subsequent services) can continue to easily pass the same
X-Request-Idheader around, allowing you to trace a single request through multiple backends in your logs
Installation
Add this line to your application's Gemfile:
gem 'instrumenter'
And then execute:
$ bundle
Or install it yourself as:
$ gem install instrumenter
Usage
Usage with Rack apps
Load the Rack middleware
require 'instrumenter/instrumenter_middleware' use Instrumenter::InstrumenterMiddlewareUse it in calls to internal services
HTTParty.get(url, headers: { 'X-Request-Id': Instrumenter.instance.request_id })
Usage with Rails apps
Lograge is recommended to get the most out of your logs on Rails
- Load the Rack middleware in an initializer ```$ruby require 'instrumenter/instrumenter_middleware'
Rails.configuration.middleware.tap do |rack| rack.use Instrumenter::InstrumenterMiddleware end
2. Configure logging in environment / application.rb
```$ruby
module YourApp
class Application < Rails::Application
config.lograge.enabled = true
config.lograge.formatter = Lograge::Formatters::Json.new
config.lograge.custom_options = lambda do |event|
{ request_id: Instrumenter.instance.request_id }
end
end
end
- Use it in calls to internal services
HTTParty.get(url, headers: { 'X-Request-Id': Instrumenter.instance.request_id })
Development
After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/mukaibot/instrumenter. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
License
The gem is available as open source under the terms of the MIT License.
Code of Conduct
Everyone interacting in the Instrumenter project’s codebases, issue trackers and mailing lists is expected to follow the code of conduct.