Search code examples
ruby-on-railsraygun

How can I call code at run-time from a Rail initializer?


I am using Raygun for error tracking in an app which uses a sharded database. Separate clients store their data on their own shard of the DB server.

rails c
1> MultiSite.get_shard
#=> master
2> Post.count
#=> 0
3> MultiSite.set_shard :client1_prod
#=> nil
4> MultiSite.get_shard
#=> client1_prod
5> Post.count
#=> 123

When using Raygun, I can set error tags by using the Raygun config/initializers/raygun.rb:

Raygun.setup do |config|
  config.api_key = LOCAL_SETTINGS["raygun_token"]
  config.filter_parameters = Rails.application.config.filter_parameters
  config.version = EnvironmentInformation.get_version
  config.tags = [
                  EnvironmentInformation.get_server_name,
                  EnvironmentInformation.get_version,
                  EnvironmentInformation.get_commit_hash,
                  EnvironmentInformation.shard_name
                ]
  config.enable_reporting = true
end

So here I am trying to use EnvironmentInformation.shard_name to tell me which client (which shard) is experiencing the error so that I can filter on that tag on the Raygun app.

Unfortunately, on app startup (when the code in this initializer is run), we are still on the master shard. This means that Raygun tags every error as the master shard.

Is there any way I can amend my initializer so that I can call EnvironmentInformation.shard_name when an error actually occurs?

Googling around hasn't come up with anything yet.


Solution

  • Via feature request, the guys at Raygun have implemented a proc which can be used to dynamically set tags at runtime.

    Although still of general interest, my specific problem has now been solved.

    https://raygun.com/thinktank/suggestion/94060