Lucky - Sentry.io

Integrate Sentry.io into a Lucky Application
Icons/chart bar
Used 25 times
Created by
S Stephen Dolan

Usage
From within a Lucky application with the bloat gem installed, run:
bloat with https://railsbytes.com/script/V2Gs4R

Run this command in your Rails app directory in the terminal:

rails app:template LOCATION="https://railsbytes.com/script/V2Gs4R"
Template Source

Review the code before running this template on your machine.

def install_sentry_shard
  insert_into_file "shard.yml", after: "dependencies:\n" do
    <<-EOF
  raven:
    github: Sija/raven.cr
    EOF
  end
  
  run "shards install"
end

def add_sentry_middleware
  insert_into_file "src/app_server.cr", after: "Lucky::ErrorHandler.new(action: Errors::Show),\n" do
    <<-EOF
      Raven::Lucky::ErrorHandler.new,
    EOF
  end
end

def require_sentry_shard
  append_to_file "src/shards.cr" do
    <<~EOF
      require "raven"
      require "raven/integrations/lucky"
    EOF
  end
end

def create_sentry_config
  create_file "config/sentry.cr" do
    <<~EOF
    Raven.configure do |config|
      config.async = true
      config.current_environment = Lucky::Env.name
      config.environments = %w(production)

      if Lucky::Env.production?
        config.dsn = sentry_dsn_from_env
      end
    end

    private def sentry_dsn_from_env
      ENV["SENTRY_DSN"]? || raise_missing_key_message
    end

    private def raise_missing_key_message
      puts "Missing SENTRY_DSN. Set the SENTRY_DSN env variable to 'unused' if not tracking errors, or set the SENTRY_DSN ENV var.".colorize.red
      exit(1)
    end
    EOF
  end
end

# Install the shard
install_sentry_shard

# Create the Sentry Raven configuration file
create_sentry_config

# Require the Sentry Raven code into our application
require_sentry_shard

# Add the Sentry Raven middleware handler to Lucky
add_sentry_middleware
Comments

Sign up or Login to leave a comment.