Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
- Transaction data are now included in the context ([#2365](https://github.com/getsentry/sentry-ruby/pull/2365))
- Closes [#2364](https://github.com/getsentry/sentry-ruby/issues/2363)

- Include Sentry meta tag in the layout automatically ([#2369](https://github.com/getsentry/sentry-ruby/pull/2369))

### Bug Fixes

- Fix skipping `connect` spans in open-telemetry [#2364](https://github.com/getsentry/sentry-ruby/pull/2364)
Expand Down
10 changes: 10 additions & 0 deletions sentry-rails/lib/generators/sentry_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
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']"

Expand All @@ -16,4 +18,12 @@ def copy_initializer_file
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
41 changes: 41 additions & 0 deletions sentry-rails/spec/sentry/generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,33 @@
self.destination File.expand_path('../../tmp', __dir__)
self.generator_class = described_class

let(:layout_file) do
File.join(destination_root, "app/views/layouts/application.html.erb")
end

before do
prepare_destination

FileUtils.mkdir_p(File.dirname(layout_file))

File.write(layout_file, <<~STR)
<!DOCTYPE html>
<html>
<head>
<title>SentryTesting</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<%= csrf_meta_tags %>
<%= csp_meta_tag %>

<%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>
<%= javascript_importmap_tags %>
</head>

<body>
<%= yield %>
</body>
</html>
STR
end

it "creates a initializer file" do
Expand All @@ -29,6 +54,22 @@
RUBY
end

it "injects meta tag into the layout" do
run_generator

content = File.read(layout_file)

expect(content).to include("Sentry.get_trace_propagation_meta.html_safe")
end

it "doesn't inject meta tag when it's disabled" do
run_generator %w[--inject-meta false]

content = File.read(layout_file)

expect(content).not_to include("Sentry.get_trace_propagation_meta.html_safe")
end

context "with a DSN option" do
it "creates a initializer file with the DSN" do
run_generator %w[--dsn foobarbaz]
Expand Down
2 changes: 2 additions & 0 deletions sentry-sidekiq/spec/sentry/rails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
require "sentry-rails"
require "spec_helper"

require "action_controller/railtie"

class TestApp < Rails::Application
end

Expand Down