diff --git a/.gitignore b/.gitignore index 56b2b2bbdd..566bf7491e 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,5 @@ instrumentation/active_record/db # rbenv configuration .ruby-version + +tags diff --git a/instrumentation/rails/.rubocop.yml b/instrumentation/rails/.rubocop.yml index 4fd32ccbcd..27f6effb3f 100644 --- a/instrumentation/rails/.rubocop.yml +++ b/instrumentation/rails/.rubocop.yml @@ -1,7 +1,15 @@ inherit_from: ../.rubocop-examples.yml +AllCops: + Exclude: + - test/railtie/dummy/**/* + - gemfiles/*.gemfilqe + Metrics/BlockLength: Enabled: false Naming/FileName: Exclude: - "lib/opentelemetry-instrumentation-rails.rb" +Style/ClassAndModuleChildren: + Exclude: + - test/railtie/**/* diff --git a/instrumentation/rails/Appraisals b/instrumentation/rails/Appraisals index d358e21c82..fca584fad1 100644 --- a/instrumentation/rails/Appraisals +++ b/instrumentation/rails/Appraisals @@ -11,16 +11,22 @@ if RUBY_VERSION < '3' end end -appraise 'rails-6.0' do - gem 'rails', '~> 6.0.0' -end +if Gem::Requirement.new('>= 2.7.0', '< 3.1').satisfied_by?(Gem::Version.new(RUBY_VERSION)) + appraise 'rails-6.0' do + gem 'rails', '~> 6.0.0' + end -appraise 'rails-6.1' do - gem 'rails', '~> 6.1.0' -end + appraise 'rails-6.1' do + gem 'rails', '~> 6.1.0' + end -if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.7.0') appraise 'rails-7.0' do gem 'rails', '~> 7.0.0' end end + +if Gem::Requirement.new('>= 3.1').satisfied_by?(Gem::Version.new(RUBY_VERSION)) + appraise 'rails-7.0.ruby.3.1' do + gem 'rails', '~> 7.0.1' + end +end diff --git a/instrumentation/rails/Rakefile b/instrumentation/rails/Rakefile index 1a64ba842e..14260497c3 100644 --- a/instrumentation/rails/Rakefile +++ b/instrumentation/rails/Rakefile @@ -11,12 +11,23 @@ require 'rubocop/rake_task' RuboCop::RakeTask.new -Rake::TestTask.new :test do |t| - t.libs << 'test' - t.libs << 'lib' - t.test_files = FileList['test/**/*_test.rb'] +namespace :test do + Rake::TestTask.new :instrumentation do |t| + t.libs << 'lib' + t.libs << 'test/instrumentation' + t.test_files = FileList['test/instrumentation/**/*_test.rb'] + end + + Rake::TestTask.new :railtie do |t| + t.libs << 'lib' + t.libs << 'test/railtie' + t.test_files = FileList['test/railtie/**/*_test.rb'] + end end +desc 'Run all tests' +task test: %i[test:instrumentation test:railtie] + YARD::Rake::YardocTask.new do |t| t.stats_options = ['--list-undoc'] end diff --git a/instrumentation/rails/gemfiles/rails_5.2.gemfile b/instrumentation/rails/gemfiles/rails_5.2.gemfile index a5a62db54d..93484da7d0 100644 --- a/instrumentation/rails/gemfiles/rails_5.2.gemfile +++ b/instrumentation/rails/gemfiles/rails_5.2.gemfile @@ -12,6 +12,7 @@ group :test do gem "opentelemetry-instrumentation-action_pack", path: "../../../instrumentation/action_pack" gem "opentelemetry-instrumentation-action_view", path: "../../../instrumentation/action_view" gem "opentelemetry-instrumentation-active_record", path: "../../../instrumentation/active_record" + gem "opentelemetry-instrumentation-active_support", path: "../../../instrumentation/active_support" gem "opentelemetry-instrumentation-rack", path: "../../../instrumentation/rack" gem "opentelemetry-sdk", path: "../../../sdk" gem "opentelemetry-semantic_conventions", path: "../../../semantic_conventions" diff --git a/instrumentation/rails/gemfiles/rails_6.0.gemfile b/instrumentation/rails/gemfiles/rails_6.0.gemfile index 8a4997f3d0..3e820947f9 100644 --- a/instrumentation/rails/gemfiles/rails_6.0.gemfile +++ b/instrumentation/rails/gemfiles/rails_6.0.gemfile @@ -12,6 +12,7 @@ group :test do gem "opentelemetry-instrumentation-action_pack", path: "../../../instrumentation/action_pack" gem "opentelemetry-instrumentation-action_view", path: "../../../instrumentation/action_view" gem "opentelemetry-instrumentation-active_record", path: "../../../instrumentation/active_record" + gem "opentelemetry-instrumentation-active_support", path: "../../../instrumentation/active_support" gem "opentelemetry-instrumentation-rack", path: "../../../instrumentation/rack" gem "opentelemetry-sdk", path: "../../../sdk" gem "opentelemetry-semantic_conventions", path: "../../../semantic_conventions" diff --git a/instrumentation/rails/gemfiles/rails_6.1.gemfile b/instrumentation/rails/gemfiles/rails_6.1.gemfile index 6a6176eda4..5895d9578c 100644 --- a/instrumentation/rails/gemfiles/rails_6.1.gemfile +++ b/instrumentation/rails/gemfiles/rails_6.1.gemfile @@ -12,6 +12,7 @@ group :test do gem "opentelemetry-instrumentation-action_pack", path: "../../../instrumentation/action_pack" gem "opentelemetry-instrumentation-action_view", path: "../../../instrumentation/action_view" gem "opentelemetry-instrumentation-active_record", path: "../../../instrumentation/active_record" + gem "opentelemetry-instrumentation-active_support", path: "../../../instrumentation/active_support" gem "opentelemetry-instrumentation-rack", path: "../../../instrumentation/rack" gem "opentelemetry-sdk", path: "../../../sdk" gem "opentelemetry-semantic_conventions", path: "../../../semantic_conventions" diff --git a/instrumentation/rails/gemfiles/rails_7.0.gemfile b/instrumentation/rails/gemfiles/rails_7.0.gemfile index 1368edcc30..ff86e92699 100644 --- a/instrumentation/rails/gemfiles/rails_7.0.gemfile +++ b/instrumentation/rails/gemfiles/rails_7.0.gemfile @@ -4,7 +4,7 @@ source "https://rubygems.org" gem "opentelemetry-api", path: "../../../api" gem "opentelemetry-instrumentation-base", path: "../../base" -gem "rails", "~> 7.0.0.alpha2" +gem "rails", "~> 7.0.0" group :test do gem "byebug" @@ -12,6 +12,7 @@ group :test do gem "opentelemetry-instrumentation-action_pack", path: "../../../instrumentation/action_pack" gem "opentelemetry-instrumentation-action_view", path: "../../../instrumentation/action_view" gem "opentelemetry-instrumentation-active_record", path: "../../../instrumentation/active_record" + gem "opentelemetry-instrumentation-active_support", path: "../../../instrumentation/active_support" gem "opentelemetry-instrumentation-rack", path: "../../../instrumentation/rack" gem "opentelemetry-sdk", path: "../../../sdk" gem "opentelemetry-semantic_conventions", path: "../../../semantic_conventions" diff --git a/instrumentation/rails/gemfiles/rails_7.0.ruby.3.1.gemfile b/instrumentation/rails/gemfiles/rails_7.0.ruby.3.1.gemfile new file mode 100644 index 0000000000..fcf53ad8e2 --- /dev/null +++ b/instrumentation/rails/gemfiles/rails_7.0.ruby.3.1.gemfile @@ -0,0 +1,22 @@ +# This file was generated by Appraisal + +source "https://rubygems.org" + +gem "opentelemetry-api", path: "../../../api" +gem "opentelemetry-instrumentation-base", path: "../../base" +gem "rails", "~> 7.0.1" + +group :test do + gem "byebug" + gem "opentelemetry-common", path: "../../../common" + gem "opentelemetry-instrumentation-action_pack", path: "../../../instrumentation/action_pack" + gem "opentelemetry-instrumentation-action_view", path: "../../../instrumentation/action_view" + gem "opentelemetry-instrumentation-active_record", path: "../../../instrumentation/active_record" + gem "opentelemetry-instrumentation-active_support", path: "../../../instrumentation/active_support" + gem "opentelemetry-instrumentation-rack", path: "../../../instrumentation/rack" + gem "opentelemetry-sdk", path: "../../../sdk" + gem "opentelemetry-semantic_conventions", path: "../../../semantic_conventions" + gem "pry-byebug" +end + +gemspec path: "../" diff --git a/instrumentation/rails/lib/opentelemetry/instrumentation/rails/railtie.rb b/instrumentation/rails/lib/opentelemetry/instrumentation/rails/railtie.rb new file mode 100644 index 0000000000..2420ddd7d1 --- /dev/null +++ b/instrumentation/rails/lib/opentelemetry/instrumentation/rails/railtie.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +# Copyright The OpenTelemetry Authors +# +# SPDX-License-Identifier: Apache-2.0 +module OpenTelemetry + module Instrumentation + module Rails + # Railtie automatically configure the OpentTelemetry SDK + # + # This railtie will set defaults for the following environment variables: + # `OTEL_SERVICE_NAME` - if unset, will default to the Rails application name + # `OTEL_RESOURCE_ATTRIBUTES` - if unset, will set the deployment environment to the current Rails environment + # + # This will also set the `OpenTelemetry.logger` to the Rails logger. + class Railtie < ::Rails::Railtie + railtie_name :opentelemetry + initializer 'opentelemetry.configure' do |app| + # See https://api.rubyonrails.org/classes/Rails/Generators/NamedBase.html#method-i-application_name + ENV['OTEL_SERVICE_NAME'] ||= app.class.name.split('::').first.underscore + + ::OpenTelemetry::SDK.configure do |c| + c.logger = ::Rails.logger + c.use_all + end + end + end + end + end +end diff --git a/instrumentation/rails/test/opentelemetry/instrumentation/rails/instrumentation_test.rb b/instrumentation/rails/test/instrumentation/opentelemetry/instrumentation/rails/instrumentation_test.rb similarity index 100% rename from instrumentation/rails/test/opentelemetry/instrumentation/rails/instrumentation_test.rb rename to instrumentation/rails/test/instrumentation/opentelemetry/instrumentation/rails/instrumentation_test.rb diff --git a/instrumentation/rails/test/opentelemetry/instrumentation/rails/patches/action_controller/metal_test.rb b/instrumentation/rails/test/instrumentation/opentelemetry/instrumentation/rails/patches/action_controller/metal_test.rb similarity index 100% rename from instrumentation/rails/test/opentelemetry/instrumentation/rails/patches/action_controller/metal_test.rb rename to instrumentation/rails/test/instrumentation/opentelemetry/instrumentation/rails/patches/action_controller/metal_test.rb diff --git a/instrumentation/rails/test/test_helper.rb b/instrumentation/rails/test/instrumentation/test_helper.rb similarity index 88% rename from instrumentation/rails/test/test_helper.rb rename to instrumentation/rails/test/instrumentation/test_helper.rb index 272e2a788c..0159da503e 100644 --- a/instrumentation/rails/test/test_helper.rb +++ b/instrumentation/rails/test/instrumentation/test_helper.rb @@ -13,13 +13,14 @@ require 'rack/test' require 'test_helpers/app_config.rb' -require_relative '../lib/opentelemetry/instrumentation' +require_relative '../../lib/opentelemetry/instrumentation' # Global opentelemetry-sdk setup EXPORTER = OpenTelemetry::SDK::Trace::Export::InMemorySpanExporter.new span_processor = OpenTelemetry::SDK::Trace::Export::SimpleSpanProcessor.new(EXPORTER) OpenTelemetry::SDK.configure do |c| + c.logger = ::Logger.new(File::NULL) c.use_all c.add_span_processor span_processor end diff --git a/instrumentation/rails/test/test_helpers/app_config.rb b/instrumentation/rails/test/instrumentation/test_helpers/app_config.rb similarity index 100% rename from instrumentation/rails/test/test_helpers/app_config.rb rename to instrumentation/rails/test/instrumentation/test_helpers/app_config.rb diff --git a/instrumentation/rails/test/test_helpers/controllers.rb b/instrumentation/rails/test/instrumentation/test_helpers/controllers.rb similarity index 100% rename from instrumentation/rails/test/test_helpers/controllers.rb rename to instrumentation/rails/test/instrumentation/test_helpers/controllers.rb diff --git a/instrumentation/rails/test/test_helpers/controllers/example_controller.rb b/instrumentation/rails/test/instrumentation/test_helpers/controllers/example_controller.rb similarity index 100% rename from instrumentation/rails/test/test_helpers/controllers/example_controller.rb rename to instrumentation/rails/test/instrumentation/test_helpers/controllers/example_controller.rb diff --git a/instrumentation/rails/test/test_helpers/controllers/exceptions_controller.rb b/instrumentation/rails/test/instrumentation/test_helpers/controllers/exceptions_controller.rb similarity index 100% rename from instrumentation/rails/test/test_helpers/controllers/exceptions_controller.rb rename to instrumentation/rails/test/instrumentation/test_helpers/controllers/exceptions_controller.rb diff --git a/instrumentation/rails/test/test_helpers/middlewares.rb b/instrumentation/rails/test/instrumentation/test_helpers/middlewares.rb similarity index 100% rename from instrumentation/rails/test/test_helpers/middlewares.rb rename to instrumentation/rails/test/instrumentation/test_helpers/middlewares.rb diff --git a/instrumentation/rails/test/test_helpers/middlewares/exception_raising_middleware.rb b/instrumentation/rails/test/instrumentation/test_helpers/middlewares/exception_raising_middleware.rb similarity index 100% rename from instrumentation/rails/test/test_helpers/middlewares/exception_raising_middleware.rb rename to instrumentation/rails/test/instrumentation/test_helpers/middlewares/exception_raising_middleware.rb diff --git a/instrumentation/rails/test/test_helpers/middlewares/redirect_middleware.rb b/instrumentation/rails/test/instrumentation/test_helpers/middlewares/redirect_middleware.rb similarity index 100% rename from instrumentation/rails/test/test_helpers/middlewares/redirect_middleware.rb rename to instrumentation/rails/test/instrumentation/test_helpers/middlewares/redirect_middleware.rb diff --git a/instrumentation/rails/test/test_helpers/routes.rb b/instrumentation/rails/test/instrumentation/test_helpers/routes.rb similarity index 100% rename from instrumentation/rails/test/test_helpers/routes.rb rename to instrumentation/rails/test/instrumentation/test_helpers/routes.rb diff --git a/instrumentation/rails/test/railtie/dummy/Rakefile b/instrumentation/rails/test/railtie/dummy/Rakefile new file mode 100644 index 0000000000..9a5ea7383a --- /dev/null +++ b/instrumentation/rails/test/railtie/dummy/Rakefile @@ -0,0 +1,6 @@ +# Add your own tasks in files placed in lib/tasks ending in .rake, +# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake. + +require_relative "config/application" + +Rails.application.load_tasks diff --git a/instrumentation/rails/test/railtie/dummy/app/controllers/application_controller.rb b/instrumentation/rails/test/railtie/dummy/app/controllers/application_controller.rb new file mode 100644 index 0000000000..4ac8823b09 --- /dev/null +++ b/instrumentation/rails/test/railtie/dummy/app/controllers/application_controller.rb @@ -0,0 +1,2 @@ +class ApplicationController < ActionController::API +end diff --git a/instrumentation/rails/test/railtie/dummy/app/controllers/concerns/.keep b/instrumentation/rails/test/railtie/dummy/app/controllers/concerns/.keep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/instrumentation/rails/test/railtie/dummy/bin/rails b/instrumentation/rails/test/railtie/dummy/bin/rails new file mode 100755 index 0000000000..efc0377492 --- /dev/null +++ b/instrumentation/rails/test/railtie/dummy/bin/rails @@ -0,0 +1,4 @@ +#!/usr/bin/env ruby +APP_PATH = File.expand_path("../config/application", __dir__) +require_relative "../config/boot" +require "rails/commands" diff --git a/instrumentation/rails/test/railtie/dummy/bin/rake b/instrumentation/rails/test/railtie/dummy/bin/rake new file mode 100755 index 0000000000..4fbf10b960 --- /dev/null +++ b/instrumentation/rails/test/railtie/dummy/bin/rake @@ -0,0 +1,4 @@ +#!/usr/bin/env ruby +require_relative "../config/boot" +require "rake" +Rake.application.run diff --git a/instrumentation/rails/test/railtie/dummy/bin/setup b/instrumentation/rails/test/railtie/dummy/bin/setup new file mode 100755 index 0000000000..ec47b79b3b --- /dev/null +++ b/instrumentation/rails/test/railtie/dummy/bin/setup @@ -0,0 +1,33 @@ +#!/usr/bin/env ruby +require "fileutils" + +# path to your application root. +APP_ROOT = File.expand_path("..", __dir__) + +def system!(*args) + system(*args) || abort("\n== Command #{args} failed ==") +end + +FileUtils.chdir APP_ROOT do + # This script is a way to set up or update your development environment automatically. + # This script is idempotent, so that you can run it at any time and get an expectable outcome. + # Add necessary setup steps to this file. + + puts "== Installing dependencies ==" + system! "gem install bundler --conservative" + system("bundle check") || system!("bundle install") + + # puts "\n== Copying sample files ==" + # unless File.exist?("config/database.yml") + # FileUtils.cp "config/database.yml.sample", "config/database.yml" + # end + + puts "\n== Preparing database ==" + system! "bin/rails db:prepare" + + puts "\n== Removing old logs and tempfiles ==" + system! "bin/rails log:clear tmp:clear" + + puts "\n== Restarting application server ==" + system! "bin/rails restart" +end diff --git a/instrumentation/rails/test/railtie/dummy/config.ru b/instrumentation/rails/test/railtie/dummy/config.ru new file mode 100644 index 0000000000..4a3c09a688 --- /dev/null +++ b/instrumentation/rails/test/railtie/dummy/config.ru @@ -0,0 +1,6 @@ +# This file is used by Rack-based servers to start the application. + +require_relative "config/environment" + +run Rails.application +Rails.application.load_server diff --git a/instrumentation/rails/test/railtie/dummy/config/application.rb b/instrumentation/rails/test/railtie/dummy/config/application.rb new file mode 100644 index 0000000000..1768b94892 --- /dev/null +++ b/instrumentation/rails/test/railtie/dummy/config/application.rb @@ -0,0 +1,37 @@ +require_relative "boot" + +require "rails" +# Pick the frameworks you want: +require "active_model/railtie" +require "active_job/railtie" +# require "active_record/railtie" +# require "active_storage/engine" +require "action_controller/railtie" +# require "action_mailer/railtie" +# require "action_mailbox/engine" +# require "action_text/engine" +require "action_view/railtie" +# require "action_cable/engine" +# require "sprockets/railtie" +require "rails/test_unit/railtie" + +# Require the gems listed in Gemfile, including any gems +# you've limited to :test, :development, or :production. +Bundler.require(*Rails.groups) +require "opentelemetry/instrumentation/rails/railtie" + +module Dummy + class Application < Rails::Application + config.load_defaults Rails::VERSION::STRING.to_f + + # Configuration for the application, engines, and railties goes here. + # + # These settings can be overridden in specific environments using the files + # in config/environments, which are processed later. + # + # config.time_zone = "Central Time (US & Canada)" + # config.eager_load_paths << Rails.root.join("extras") + config.logger = ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(File::NULL)) + config.api_only = true + end +end diff --git a/instrumentation/rails/test/railtie/dummy/config/boot.rb b/instrumentation/rails/test/railtie/dummy/config/boot.rb new file mode 100644 index 0000000000..116591a4ed --- /dev/null +++ b/instrumentation/rails/test/railtie/dummy/config/boot.rb @@ -0,0 +1,5 @@ +# Set up gems listed in the Gemfile. +ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../../Gemfile", __dir__) + +require "bundler/setup" if File.exist?(ENV["BUNDLE_GEMFILE"]) +$LOAD_PATH.unshift File.expand_path("../../../lib", __dir__) diff --git a/instrumentation/rails/test/railtie/dummy/config/environment.rb b/instrumentation/rails/test/railtie/dummy/config/environment.rb new file mode 100644 index 0000000000..cac5315775 --- /dev/null +++ b/instrumentation/rails/test/railtie/dummy/config/environment.rb @@ -0,0 +1,5 @@ +# Load the Rails application. +require_relative "application" + +# Initialize the Rails application. +Rails.application.initialize! diff --git a/instrumentation/rails/test/railtie/dummy/config/environments/development.rb b/instrumentation/rails/test/railtie/dummy/config/environments/development.rb new file mode 100644 index 0000000000..8d1635ee6f --- /dev/null +++ b/instrumentation/rails/test/railtie/dummy/config/environments/development.rb @@ -0,0 +1,68 @@ +require "active_support/core_ext/integer/time" + +Rails.application.configure do + # Settings specified here will take precedence over those in config/application.rb. + + # In the development environment your application's code is reloaded any time + # it changes. This slows down response time but is perfect for development + # since you don't have to restart the web server when you make code changes. + config.cache_classes = false + + # Do not eager load code on boot. + config.eager_load = false + + # Show full error reports. + config.consider_all_requests_local = true + + # Enable server timing + config.server_timing = true + + # Enable/disable caching. By default caching is disabled. + # Run rails dev:cache to toggle caching. + if Rails.root.join("tmp/caching-dev.txt").exist? + config.action_controller.perform_caching = true + config.action_controller.enable_fragment_cache_logging = true + + config.cache_store = :memory_store + config.public_file_server.headers = { + "Cache-Control" => "public, max-age=#{2.days.to_i}" + } + else + config.action_controller.perform_caching = false + + config.cache_store = :null_store + end + + # Store uploaded files on the local file system (see config/storage.yml for options). + config.active_storage.service = :local + + # Don't care if the mailer can't send. + config.action_mailer.raise_delivery_errors = false + + config.action_mailer.perform_caching = false + + # Print deprecation notices to the Rails logger. + config.active_support.deprecation = :log + + # Raise exceptions for disallowed deprecations. + config.active_support.disallowed_deprecation = :raise + + # Tell Active Support which deprecation messages to disallow. + config.active_support.disallowed_deprecation_warnings = [] + + # Raise an error on page load if there are pending migrations. + config.active_record.migration_error = :page_load + + # Highlight code that triggered database queries in logs. + config.active_record.verbose_query_logs = true + + + # Raises error for missing translations. + # config.i18n.raise_on_missing_translations = true + + # Annotate rendered view with file names. + # config.action_view.annotate_rendered_view_with_filenames = true + + # Uncomment if you wish to allow Action Cable access from any origin. + # config.action_cable.disable_request_forgery_protection = true +end diff --git a/instrumentation/rails/test/railtie/dummy/config/environments/production.rb b/instrumentation/rails/test/railtie/dummy/config/environments/production.rb new file mode 100644 index 0000000000..b60aa07e40 --- /dev/null +++ b/instrumentation/rails/test/railtie/dummy/config/environments/production.rb @@ -0,0 +1,87 @@ +require "active_support/core_ext/integer/time" + +Rails.application.configure do + # Settings specified here will take precedence over those in config/application.rb. + + # Code is not reloaded between requests. + config.cache_classes = true + + # Eager load code on boot. This eager loads most of Rails and + # your application in memory, allowing both threaded web servers + # and those relying on copy on write to perform better. + # Rake tasks automatically ignore this option for performance. + config.eager_load = true + + # Full error reports are disabled and caching is turned on. + config.consider_all_requests_local = false + config.action_controller.perform_caching = true + + # Ensures that a master key has been made available in either ENV["RAILS_MASTER_KEY"] + # or in config/master.key. This key is used to decrypt credentials (and other encrypted files). + # config.require_master_key = true + + # Disable serving static files from the `/public` folder by default since + # Apache or NGINX already handles this. + config.public_file_server.enabled = ENV["RAILS_SERVE_STATIC_FILES"].present? + + # Enable serving of images, stylesheets, and JavaScripts from an asset server. + # config.asset_host = "http://assets.example.com" + + # Specifies the header that your server uses for sending files. + # config.action_dispatch.x_sendfile_header = "X-Sendfile" # for Apache + # config.action_dispatch.x_sendfile_header = "X-Accel-Redirect" # for NGINX + + # Store uploaded files on the local file system (see config/storage.yml for options). + config.active_storage.service = :local + + # Mount Action Cable outside main process or domain. + # config.action_cable.mount_path = nil + # config.action_cable.url = "wss://example.com/cable" + # config.action_cable.allowed_request_origins = [ "http://example.com", /http:\/\/example.*/ ] + + # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies. + # config.force_ssl = true + + # Include generic and useful information about system operation, but avoid logging too much + # information to avoid inadvertent exposure of personally identifiable information (PII). + config.log_level = :info + + # Prepend all log lines with the following tags. + config.log_tags = [ :request_id ] + + # Use a different cache store in production. + # config.cache_store = :mem_cache_store + + # Use a real queuing backend for Active Job (and separate queues per environment). + # config.active_job.queue_adapter = :resque + # config.active_job.queue_name_prefix = "dummy_production" + + config.action_mailer.perform_caching = false + + # Ignore bad email addresses and do not raise email delivery errors. + # Set this to true and configure the email server for immediate delivery to raise delivery errors. + # config.action_mailer.raise_delivery_errors = false + + # Enable locale fallbacks for I18n (makes lookups for any locale fall back to + # the I18n.default_locale when a translation cannot be found). + config.i18n.fallbacks = true + + # Don't log any deprecations. + config.active_support.report_deprecations = false + + # Use default logging formatter so that PID and timestamp are not suppressed. + config.log_formatter = ::Logger::Formatter.new + + # Use a different logger for distributed setups. + # require "syslog/logger" + # config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new "app-name") + + # if ENV["RAILS_LOG_TO_STDOUT"].present? + # logger = ActiveSupport::Logger.new(STDOUT) + # logger.formatter = config.log_formatter + # config.logger = ActiveSupport::TaggedLogging.new(logger) + # end + + # Do not dump schema after migrations. + config.active_record.dump_schema_after_migration = false +end diff --git a/instrumentation/rails/test/railtie/dummy/config/environments/test.rb b/instrumentation/rails/test/railtie/dummy/config/environments/test.rb new file mode 100644 index 0000000000..6bebc8a548 --- /dev/null +++ b/instrumentation/rails/test/railtie/dummy/config/environments/test.rb @@ -0,0 +1,60 @@ +require "active_support/core_ext/integer/time" + +# The test environment is used exclusively to run your application's +# test suite. You never need to work with it otherwise. Remember that +# your test database is "scratch space" for the test suite and is wiped +# and recreated between test runs. Don't rely on the data there! + +Rails.application.configure do + # Settings specified here will take precedence over those in config/application.rb. + + # Turn false under Spring and add config.action_view.cache_template_loading = true. + config.cache_classes = true + + # Eager loading loads your whole application. When running a single test locally, + # this probably isn't necessary. It's a good idea to do in a continuous integration + # system, or in some way before deploying your code. + config.eager_load = true + + # Configure public file server for tests with Cache-Control for performance. + config.public_file_server.enabled = true + config.public_file_server.headers = { + "Cache-Control" => "public, max-age=#{1.hour.to_i}" + } + + # Show full error reports and disable caching. + config.consider_all_requests_local = true + config.action_controller.perform_caching = false + config.cache_store = :null_store + + # Raise exceptions instead of rendering exception templates. + config.action_dispatch.show_exceptions = false + + # Disable request forgery protection in test environment. + config.action_controller.allow_forgery_protection = false + + # Store uploaded files on the local file system in a temporary directory. + # config.active_storage.service = :test + + # config.action_mailer.perform_caching = false + + # Tell Action Mailer not to deliver emails to the real world. + # The :test delivery method accumulates sent emails in the + # ActionMailer::Base.deliveries array. + # config.action_mailer.delivery_method = :test + + # Print deprecation notices to the stderr. + config.active_support.deprecation = :stderr + + # Raise exceptions for disallowed deprecations. + config.active_support.disallowed_deprecation = :raise + + # Tell Active Support which deprecation messages to disallow. + config.active_support.disallowed_deprecation_warnings = [] + + # Raises error for missing translations. + # config.i18n.raise_on_missing_translations = true + + # Annotate rendered view with file names. + # config.action_view.annotate_rendered_view_with_filenames = true +end diff --git a/instrumentation/rails/test/railtie/dummy/config/initializers/content_security_policy.rb b/instrumentation/rails/test/railtie/dummy/config/initializers/content_security_policy.rb new file mode 100644 index 0000000000..3621f97f8e --- /dev/null +++ b/instrumentation/rails/test/railtie/dummy/config/initializers/content_security_policy.rb @@ -0,0 +1,26 @@ +# Be sure to restart your server when you modify this file. + +# Define an application-wide content security policy +# For further information see the following documentation +# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy + +# Rails.application.configure do +# config.content_security_policy do |policy| +# policy.default_src :self, :https +# policy.font_src :self, :https, :data +# policy.img_src :self, :https, :data +# policy.object_src :none +# policy.script_src :self, :https +# policy.style_src :self, :https +# # Specify URI for violation reports +# # policy.report_uri "/csp-violation-report-endpoint" +# end +# +# # Generate session nonces for permitted importmap and inline scripts +# config.content_security_policy_nonce_generator = ->(request) { request.session.id.to_s } +# config.content_security_policy_nonce_directives = %w(script-src) +# +# # Report CSP violations to a specified URI. See: +# # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy-Report-Only +# # config.content_security_policy_report_only = true +# end diff --git a/instrumentation/rails/test/railtie/dummy/config/initializers/filter_parameter_logging.rb b/instrumentation/rails/test/railtie/dummy/config/initializers/filter_parameter_logging.rb new file mode 100644 index 0000000000..4b34a03668 --- /dev/null +++ b/instrumentation/rails/test/railtie/dummy/config/initializers/filter_parameter_logging.rb @@ -0,0 +1,6 @@ +# Be sure to restart your server when you modify this file. + +# Configure sensitive parameters which will be filtered from the log file. +Rails.application.config.filter_parameters += [ + :passw, :secret, :token, :_key, :crypt, :salt, :certificate, :otp, :ssn +] diff --git a/instrumentation/rails/test/railtie/dummy/config/initializers/inflections.rb b/instrumentation/rails/test/railtie/dummy/config/initializers/inflections.rb new file mode 100644 index 0000000000..3860f659ea --- /dev/null +++ b/instrumentation/rails/test/railtie/dummy/config/initializers/inflections.rb @@ -0,0 +1,16 @@ +# Be sure to restart your server when you modify this file. + +# Add new inflection rules using the following format. Inflections +# are locale specific, and you may define rules for as many different +# locales as you wish. All of these examples are active by default: +# ActiveSupport::Inflector.inflections(:en) do |inflect| +# inflect.plural /^(ox)$/i, "\\1en" +# inflect.singular /^(ox)en/i, "\\1" +# inflect.irregular "person", "people" +# inflect.uncountable %w( fish sheep ) +# end + +# These inflection rules are supported but not enabled by default: +# ActiveSupport::Inflector.inflections(:en) do |inflect| +# inflect.acronym "RESTful" +# end diff --git a/instrumentation/rails/test/railtie/dummy/config/initializers/permissions_policy.rb b/instrumentation/rails/test/railtie/dummy/config/initializers/permissions_policy.rb new file mode 100644 index 0000000000..00f64d71b0 --- /dev/null +++ b/instrumentation/rails/test/railtie/dummy/config/initializers/permissions_policy.rb @@ -0,0 +1,11 @@ +# Define an application-wide HTTP permissions policy. For further +# information see https://developers.google.com/web/updates/2018/06/feature-policy +# +# Rails.application.config.permissions_policy do |f| +# f.camera :none +# f.gyroscope :none +# f.microphone :none +# f.usb :none +# f.fullscreen :self +# f.payment :self, "https://secure.example.com" +# end diff --git a/instrumentation/rails/test/railtie/dummy/config/locales/en.yml b/instrumentation/rails/test/railtie/dummy/config/locales/en.yml new file mode 100644 index 0000000000..8ca56fc74f --- /dev/null +++ b/instrumentation/rails/test/railtie/dummy/config/locales/en.yml @@ -0,0 +1,33 @@ +# Files in the config/locales directory are used for internationalization +# and are automatically loaded by Rails. If you want to use locales other +# than English, add the necessary files in this directory. +# +# To use the locales, use `I18n.t`: +# +# I18n.t "hello" +# +# In views, this is aliased to just `t`: +# +# <%= t("hello") %> +# +# To use a different locale, set it with `I18n.locale`: +# +# I18n.locale = :es +# +# This would use the information in config/locales/es.yml. +# +# The following keys must be escaped otherwise they will not be retrieved by +# the default I18n backend: +# +# true, false, on, off, yes, no +# +# Instead, surround them with single quotes. +# +# en: +# "true": "foo" +# +# To learn more, please read the Rails Internationalization guide +# available at https://guides.rubyonrails.org/i18n.html. + +en: + hello: "Hello world" diff --git a/instrumentation/rails/test/railtie/dummy/config/puma.rb b/instrumentation/rails/test/railtie/dummy/config/puma.rb new file mode 100644 index 0000000000..daaf036999 --- /dev/null +++ b/instrumentation/rails/test/railtie/dummy/config/puma.rb @@ -0,0 +1,43 @@ +# Puma can serve each request in a thread from an internal thread pool. +# The `threads` method setting takes two numbers: a minimum and maximum. +# Any libraries that use thread pools should be configured to match +# the maximum value specified for Puma. Default is set to 5 threads for minimum +# and maximum; this matches the default thread size of Active Record. +# +max_threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 } +min_threads_count = ENV.fetch("RAILS_MIN_THREADS") { max_threads_count } +threads min_threads_count, max_threads_count + +# Specifies the `worker_timeout` threshold that Puma will use to wait before +# terminating a worker in development environments. +# +worker_timeout 3600 if ENV.fetch("RAILS_ENV", "development") == "development" + +# Specifies the `port` that Puma will listen on to receive requests; default is 3000. +# +port ENV.fetch("PORT") { 3000 } + +# Specifies the `environment` that Puma will run in. +# +environment ENV.fetch("RAILS_ENV") { "development" } + +# Specifies the `pidfile` that Puma will use. +pidfile ENV.fetch("PIDFILE") { "tmp/pids/server.pid" } + +# Specifies the number of `workers` to boot in clustered mode. +# Workers are forked web server processes. If using threads and workers together +# the concurrency of the application would be max `threads` * `workers`. +# Workers do not work on JRuby or Windows (both of which do not support +# processes). +# +# workers ENV.fetch("WEB_CONCURRENCY") { 2 } + +# Use the `preload_app!` method when specifying a `workers` number. +# This directive tells Puma to first boot the application and load code +# before forking the application. This takes advantage of Copy On Write +# process behavior so workers use less memory. +# +# preload_app! + +# Allow puma to be restarted by `bin/rails restart` command. +plugin :tmp_restart diff --git a/instrumentation/rails/test/railtie/dummy/config/routes.rb b/instrumentation/rails/test/railtie/dummy/config/routes.rb new file mode 100644 index 0000000000..262ffd5472 --- /dev/null +++ b/instrumentation/rails/test/railtie/dummy/config/routes.rb @@ -0,0 +1,6 @@ +Rails.application.routes.draw do + # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html + + # Defines the root path route ("/") + # root "articles#index" +end diff --git a/instrumentation/rails/test/railtie/dummy/tmp/development_secret.txt b/instrumentation/rails/test/railtie/dummy/tmp/development_secret.txt new file mode 100644 index 0000000000..5be479ebd7 --- /dev/null +++ b/instrumentation/rails/test/railtie/dummy/tmp/development_secret.txt @@ -0,0 +1 @@ +c9b8cb0c770beb85f0a8f3f0b0fb292ba85898cdf16193bb6ce27c8249f5c48669734b958fc7b847de4100b929febf9ee51ca1bd3a4ae0e36f047287d4e87370 \ No newline at end of file diff --git a/instrumentation/rails/test/railtie/opentelemetry/instrumentation/rails/railtie_test.rb b/instrumentation/rails/test/railtie/opentelemetry/instrumentation/rails/railtie_test.rb new file mode 100644 index 0000000000..6c5393a8c9 --- /dev/null +++ b/instrumentation/rails/test/railtie/opentelemetry/instrumentation/rails/railtie_test.rb @@ -0,0 +1,47 @@ +# frozen_string_literal: true + +# Copyright The OpenTelemetry Authors +# +# SPDX-License-Identifier: Apache-2.0 +require 'test_helper' + +class OpenTelemetry::Instrumentation::Rails::RailtieTest < ActiveSupport::TestCase + include OpenTelemetry::SemanticConventions + + setup do + OpenTelemetry::Instrumentation.registry.instance_variable_get('@instrumentation').each do |i| + i.instance_variable_set('@instance', nil) + end + OpenTelemetry::SDK::Resources::Resource.instance_variable_set('@default', nil) + OpenTelemetry.tracer_provider = OpenTelemetry::Internal::ProxyTracerProvider.new + end + + test 'configures a default instance of the SDK' do + with_env('OTEL_SERVICE_NAME' => nil) do + run_initializer + assert_instance_of(OpenTelemetry::SDK::Trace::TracerProvider, OpenTelemetry.tracer_provider) + assert_same(Rails.logger, OpenTelemetry.logger.instance_variable_get('@logger')) + end + end + + test 'uses standard OTel environment variables for SDK configuration' do + with_env('OTEL_SERVICE_NAME' => 'test_service_name') do + run_initializer + + actual_attributes = find_resource_attributes(Resource::SERVICE_NAME) + + expected_attributes = { Resource::SERVICE_NAME => 'test_service_name' } + assert_equal(expected_attributes, actual_attributes) + end + end + + private + + def run_initializer + Rails.application.initializers.find { |i| i.name == 'opentelemetry.configure' }.run(Rails.application) + end + + def find_resource_attributes(*keys) + OpenTelemetry.tracer_provider.resource.attribute_enumerator.to_h.slice(*keys) + end +end diff --git a/instrumentation/rails/test/railtie/test_helper.rb b/instrumentation/rails/test/railtie/test_helper.rb new file mode 100644 index 0000000000..de71b34d95 --- /dev/null +++ b/instrumentation/rails/test/railtie/test_helper.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +# Copyright The OpenTelemetry Authors +# +# SPDX-License-Identifier: Apache-2.0 + +# Configure Rails Environment +ENV['RACK_ENV'] = 'test' +ENV['RAILS_ENV'] = 'test' + +require_relative '../../test/railtie/dummy/config/environment' +require 'rails/test_help' +require 'opentelemetry/common/test_helpers' + +require 'simplecov' +SimpleCov.start