Skip to content

Commit a09da50

Browse files
authored
Include Sentry meta tag in the layout automatically (#2369)
* Include Sentry meta tag in the layout automatically * Add --inject-meta option
1 parent 8c1c259 commit a09da50

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
- Transaction data are now included in the context ([#2365](https://github.com/getsentry/sentry-ruby/pull/2365))
1919
- Closes [#2364](https://github.com/getsentry/sentry-ruby/issues/2363)
2020

21+
- Include Sentry meta tag in the layout automatically ([#2369](https://github.com/getsentry/sentry-ruby/pull/2369))
22+
2123
### Bug Fixes
2224

2325
- Fix skipping `connect` spans in open-telemetry [#2364](https://github.com/getsentry/sentry-ruby/pull/2364)

sentry-rails/lib/generators/sentry_generator.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
class SentryGenerator < ::Rails::Generators::Base
44
class_option :dsn, type: :string, desc: "Sentry DSN"
55

6+
class_option :inject_meta, type: :boolean, default: true, desc: "Inject meta tag into layout"
7+
68
def copy_initializer_file
79
dsn = options[:dsn] ? "'#{options[:dsn]}'" : "ENV['SENTRY_DSN']"
810

@@ -16,4 +18,12 @@ def copy_initializer_file
1618
end
1719
RUBY
1820
end
21+
22+
def inject_code_into_layout
23+
return unless options[:inject_meta]
24+
25+
inject_into_file "app/views/layouts/application.html.erb", before: "</head>\n" do
26+
" <%= Sentry.get_trace_propagation_meta.html_safe %>\n "
27+
end
28+
end
1929
end

sentry-rails/spec/sentry/generator_spec.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,33 @@
1010
self.destination File.expand_path('../../tmp', __dir__)
1111
self.generator_class = described_class
1212

13+
let(:layout_file) do
14+
File.join(destination_root, "app/views/layouts/application.html.erb")
15+
end
16+
1317
before do
1418
prepare_destination
19+
20+
FileUtils.mkdir_p(File.dirname(layout_file))
21+
22+
File.write(layout_file, <<~STR)
23+
<!DOCTYPE html>
24+
<html>
25+
<head>
26+
<title>SentryTesting</title>
27+
<meta name="viewport" content="width=device-width,initial-scale=1">
28+
<%= csrf_meta_tags %>
29+
<%= csp_meta_tag %>
30+
31+
<%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>
32+
<%= javascript_importmap_tags %>
33+
</head>
34+
35+
<body>
36+
<%= yield %>
37+
</body>
38+
</html>
39+
STR
1540
end
1641

1742
it "creates a initializer file" do
@@ -29,6 +54,22 @@
2954
RUBY
3055
end
3156

57+
it "injects meta tag into the layout" do
58+
run_generator
59+
60+
content = File.read(layout_file)
61+
62+
expect(content).to include("Sentry.get_trace_propagation_meta.html_safe")
63+
end
64+
65+
it "doesn't inject meta tag when it's disabled" do
66+
run_generator %w[--inject-meta false]
67+
68+
content = File.read(layout_file)
69+
70+
expect(content).not_to include("Sentry.get_trace_propagation_meta.html_safe")
71+
end
72+
3273
context "with a DSN option" do
3374
it "creates a initializer file with the DSN" do
3475
run_generator %w[--dsn foobarbaz]

sentry-sidekiq/spec/sentry/rails_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
require "sentry-rails"
55
require "spec_helper"
66

7+
require "action_controller/railtie"
8+
79
class TestApp < Rails::Application
810
end
911

0 commit comments

Comments
 (0)