From a09da50d47d47cd4c3c3bea360977fefbf5af195 Mon Sep 17 00:00:00 2001 From: Peter Solnica Date: Tue, 13 Aug 2024 16:19:45 +0200 Subject: [PATCH] Include Sentry meta tag in the layout automatically (#2369) * Include Sentry meta tag in the layout automatically * Add --inject-meta option --- CHANGELOG.md | 2 + .../lib/generators/sentry_generator.rb | 10 +++++ sentry-rails/spec/sentry/generator_spec.rb | 41 +++++++++++++++++++ sentry-sidekiq/spec/sentry/rails_spec.rb | 2 + 4 files changed, 55 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 352ef2472..aed6c3666 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/sentry-rails/lib/generators/sentry_generator.rb b/sentry-rails/lib/generators/sentry_generator.rb index 76ad3ecf9..de694e40d 100644 --- a/sentry-rails/lib/generators/sentry_generator.rb +++ b/sentry-rails/lib/generators/sentry_generator.rb @@ -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']" @@ -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: "\n" do + " <%= Sentry.get_trace_propagation_meta.html_safe %>\n " + end + end end diff --git a/sentry-rails/spec/sentry/generator_spec.rb b/sentry-rails/spec/sentry/generator_spec.rb index ac4aff730..7da9e12a7 100644 --- a/sentry-rails/spec/sentry/generator_spec.rb +++ b/sentry-rails/spec/sentry/generator_spec.rb @@ -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) + + + + SentryTesting + + <%= csrf_meta_tags %> + <%= csp_meta_tag %> + + <%= stylesheet_link_tag "application", "data-turbo-track": "reload" %> + <%= javascript_importmap_tags %> + + + + <%= yield %> + + + STR end it "creates a initializer file" do @@ -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] diff --git a/sentry-sidekiq/spec/sentry/rails_spec.rb b/sentry-sidekiq/spec/sentry/rails_spec.rb index 903f8ab38..87518ea06 100644 --- a/sentry-sidekiq/spec/sentry/rails_spec.rb +++ b/sentry-sidekiq/spec/sentry/rails_spec.rb @@ -4,6 +4,8 @@ require "sentry-rails" require "spec_helper" +require "action_controller/railtie" + class TestApp < Rails::Application end