File: sentry_generator.rb

package info (click to toggle)
ruby-sentry-rails 6.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 284 kB
  • sloc: ruby: 1,771; makefile: 7; sh: 4
file content (31 lines) | stat: -rw-r--r-- 884 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# frozen_string_literal: true

require "rails/generators/base"

class SentryGenerator < ::Rails::Generators::Base
  class_option :dsn, type: :string, desc: "Sentry DSN"

  class_option :inject_meta, type: :boolean, default: true, desc: "Inject meta tag into layout"

  def copy_initializer_file
    dsn = options[:dsn] ? "'#{options[:dsn]}'" : "ENV['SENTRY_DSN']"

    create_file "config/initializers/sentry.rb", <<~RUBY
      # frozen_string_literal: true

      Sentry.init do |config|
        config.breadcrumbs_logger = [:active_support_logger]
        config.dsn = #{dsn}
        config.traces_sample_rate = 1.0
      end
    RUBY
  end

  def inject_code_into_layout
    return unless options[:inject_meta]

    inject_into_file "app/views/layouts/application.html.erb", before: "</head>\n" do
      "  <%= Sentry.get_trace_propagation_meta.html_safe %>\n  "
    end
  end
end