Skip to content

Commit

Permalink
Include Sentry meta tag in the layout automatically (#2369)
Browse files Browse the repository at this point in the history
* Include Sentry meta tag in the layout automatically
* Add --inject-meta option
  • Loading branch information
solnic authored Aug 13, 2024
1 parent 8c1c259 commit a09da50
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,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

0 comments on commit a09da50

Please sign in to comment.