diff --git a/.cspell.yml b/.cspell.yml index 327822c4d4..a2bcce24e6 100644 --- a/.cspell.yml +++ b/.cspell.yml @@ -84,4 +84,5 @@ words: - webmocks - Xuan - yardoc - - traceparent \ No newline at end of file + - rackup + - traceparent diff --git a/.github/workflows/ci-contrib.yml b/.github/workflows/ci-contrib.yml index 7285ed5899..3af02b03ba 100644 --- a/.github/workflows/ci-contrib.yml +++ b/.github/workflows/ci-contrib.yml @@ -229,3 +229,33 @@ jobs: with: gem: "opentelemetry-sampler-${{ matrix.gem }}" ruby: "jruby-10.0.2.0" + + auto-instrumentation: + strategy: + fail-fast: false + matrix: + os: + - ubuntu-latest + name: "opentelemetry-auto-instrumentation / ${{ matrix.os }}" + runs-on: ${{ matrix.os }} + steps: + - uses: actions/checkout@v5 + - name: "Test Ruby 3.4" + uses: ./.github/actions/test_gem + with: + gem: "opentelemetry-auto-instrumentation" + ruby: "3.4" + - name: "Test Ruby 3.3" + uses: ./.github/actions/test_gem + with: + gem: "opentelemetry-auto-instrumentation" + ruby: "3.3" + - name: "Test Ruby 3.2" + uses: ./.github/actions/test_gem + with: + gem: "opentelemetry-auto-instrumentation" + ruby: "3.2" + yard: true + rubocop: true + coverage: true + build: true diff --git a/opentelemetry-auto-instrumentation/.rubocop.yml b/opentelemetry-auto-instrumentation/.rubocop.yml new file mode 100644 index 0000000000..154b948f5d --- /dev/null +++ b/opentelemetry-auto-instrumentation/.rubocop.yml @@ -0,0 +1,5 @@ +inherit_from: ../.rubocop.yml +Naming/FileName: + Enabled: false +Gemspec/DevelopmentDependencies: + Enabled: false diff --git a/opentelemetry-auto-instrumentation/CHANGELOG.md b/opentelemetry-auto-instrumentation/CHANGELOG.md new file mode 100644 index 0000000000..d068b6d9e8 --- /dev/null +++ b/opentelemetry-auto-instrumentation/CHANGELOG.md @@ -0,0 +1 @@ +# Release History: opentelemetry-auto-instrumentation diff --git a/opentelemetry-auto-instrumentation/Gemfile b/opentelemetry-auto-instrumentation/Gemfile new file mode 100644 index 0000000000..228d700664 --- /dev/null +++ b/opentelemetry-auto-instrumentation/Gemfile @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +# Copyright The OpenTelemetry Authors +# +# SPDX-License-Identifier: Apache-2.0 + +source 'https://rubygems.org' + +gemspec + +group :test do + gem 'bundler' + gem 'minitest' + gem 'rake' + gem 'rubocop' + gem 'rubocop-performance' + gem 'simplecov' + gem 'yard' + gem 'opentelemetry-test-helpers' + if RUBY_VERSION >= '3.4' + gem 'base64' + gem 'bigdecimal' + gem 'mutex_m' + end +end diff --git a/opentelemetry-auto-instrumentation/LICENSE b/opentelemetry-auto-instrumentation/LICENSE new file mode 100644 index 0000000000..1ef7dad2c5 --- /dev/null +++ b/opentelemetry-auto-instrumentation/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright The OpenTelemetry Authors + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/opentelemetry-auto-instrumentation/README.md b/opentelemetry-auto-instrumentation/README.md new file mode 100644 index 0000000000..e29808744f --- /dev/null +++ b/opentelemetry-auto-instrumentation/README.md @@ -0,0 +1,157 @@ +# Opentelemetry Auto Instrumentation + +The `opentelemetry-auto-instrumentation` gem provides an easy way to load and initialize opentelemetry-ruby for auto instrumentation. + +## What is OpenTelemetry? + +OpenTelemetry is an open source observability framework, providing a general-purpose API, SDK, and related tools required for the instrumentation of cloud-native software, frameworks, and libraries. + +OpenTelemetry provides a single set of APIs, libraries, agents, and collector services to capture distributed traces and metrics from your application. You can analyze them using Prometheus, Jaeger, and other observability tools. + +## How does this gem fit in? + +The `opentelemetry-auto-instrumentation` gem provides an easy way to load and initialize the OpenTelemetry Ruby SDK without changing your code. This gem is particularly used with the [OpenTelemetry Operator][opentelemetry-operator]. + +## How do I get started? + +It's recommended to install this gem through `gem install` rather than Bundler since it doesn't require modifying your codebase (including the Gemfile). + +Install the gem using: + +```console +gem install opentelemetry-auto-instrumentation +``` + +Installing opentelemetry-auto-instrumentation will automatically install following gems: + +```console +opentelemetry-sdk +opentelemetry-api +opentelemetry-instrumentation-all +opentelemetry-exporter-otlp +opentelemetry-helpers-mysql +opentelemetry-helpers-sql-obfuscation +opentelemetry-resource-detector-google_cloud_platform +opentelemetry-resource-detector-azure +opentelemetry-resource-detector-container +opentelemetry-resource-detector-aws +``` + +### Example installation strategies + +Instrument your application: + +```console +RUBYOPT="-r opentelemetry-auto-instrumentation" ruby application.rb +``` + +Instrument your application with additional environment variables: + +```console +export OTEL_TRACES_EXPORTER="otlp" +export OTEL_EXPORTER_OTLP_ENDPOINT="your-endpoint" +export OTEL_RUBY_RESOURCE_DETECTORS="container" +export OTEL_SERVICE_NAME="your-service-name" + +RUBYOPT="-r opentelemetry-auto-instrumentation" ruby application.rb +``` + +Instrument your application with disabling certain instrumentation (e.g. sinatra): + +```console +export OTEL_RUBY_INSTRUMENTATION_SINATRA_ENABLED='false' +RUBYOPT="-r opentelemetry-auto-instrumentation" ruby application.rb +``` + +Instrument your application with only certain instrumentation installed (e.g. mysql, redis): + +```console +export OTEL_RUBY_ENABLED_INSTRUMENTATIONS='mysql2,redis' +RUBYOPT="-r opentelemetry-auto-instrumentation" ruby application.rb +``` + +Instrument your application with only redis and configure its options: + +```console +export OTEL_RUBY_ENABLED_INSTRUMENTATIONS='redis' +export OTEL_RUBY_INSTRUMENTATION_REDIS_CONFIG_OPTS='peer_service=new_service;db_statement=omit' +RUBYOPT="-r opentelemetry-auto-instrumentation" ruby application.rb +``` + +Instrument Rails application: + +```console +RUBYOPT="-r opentelemetry-auto-instrumentation" rails server +``` + +Instrument Rails application with `bundle exec`: + +Since the `opentelemetry-auto-instrumentation` gem should be installed through `gem install`, anything related to the OpenTelemetry gem won't be stored in Bundler's gem path. Therefore, users need to add an additional gem path that contains these gems prior to initialization. + +```console +RUBYOPT="-r {PUT YOUR GEM PATH}/gems/opentelemetry-auto-instrumentation-0.1.0/lib/opentelemetry-auto-instrumentation" bundle exec rails server +``` + +If you wish to load some gems outside the Gemfile, then they need to be placed in front of opentelemetry-auto-instrumentation: + +```console +export BUNDLE_WITHOUT=development,test +gem install mysql2 +RUBYOPT="-r mysql2 -r opentelemetry-auto-instrumentation" ruby application.rb +``` + +Instrument Sinatra application with rackup: + +If you are using a Gemfile to install the required gems but without `Bundler.require`, set `REQUIRE_BUNDLER` to true. This way, `opentelemetry-auto-instrumentation` will call `Bundler.require` to initialize the required gems prior to SDK initialization. + +```console +export REQUIRE_BUNDLER=true +RUBYOPT="-r opentelemetry-auto-instrumentation" rackup config.ru +``` + +### Troubleshooting + +The auto-instrumentation works by patching the `Bundler::Runtime#require` method to inject the `opentelemetry-auto-instrumentation` gem into your application. Rails applications automatically call `Bundler.require` during their boot process, so they work out of the box. However, many other frameworks (like Sinatra) do not call `Bundler.require` automatically, which means the OpenTelemetry SDK is never loaded. To fix this, explicitly call `Bundler.require` early in your application's startup process, or set the `OTEL_RUBY_REQUIRE_BUNDLER` environment variable to `true`. + +## Example + +In example folder, executing the following commands should result in trace output. + +```console +# if the user doesn't want to install opentelemetry-auto-instrumentation from rubygems.org +# the user can build the gem and install it with gem install *.gem + +gem install opentelemetry-auto-instrumentation +bundle install +OTEL_RUBY_REQUIRE_BUNDLER=true OTEL_TRACES_EXPORTER=console RUBYOPT="-r opentelemetry-auto-instrumentation" OTEL_RUBY_AUTO_INSTRUMENTATION_DEBUG=false OTEL_RUBY_OPERATOR=false ruby app.rb +``` + +## Configuration + +These environment variables are not standard OpenTelemetry environment variables; they are only feature flags for this gem. + +| Environment Variable | Description | Default | Example | +|----------------------|-------------|---------|---------| +| `OTEL_RUBY_REQUIRE_BUNDLER` | Set to `true` if you are using Bundler to install gems but without `Bundler.require` in your script during initialization. | nil | N/A | +| `OTEL_RUBY_ADDITIONAL_GEM_PATH` | Intended to be used for the OpenTelemetry Operator environment if you install `opentelemetry-auto-instrumentation` to a customized path. | nil | N/A | +| `OTEL_RUBY_OPERATOR` | Set to `true` to set the binding path for the OpenTelemetry Operator. | `/otel-auto-instrumentation-ruby` | N/A | +| `OTEL_RUBY_RESOURCE_DETECTORS` | Determine what kind of resource detector is needed. Currently supports `container`, `azure`, and `google_cloud_platform`. Use commas to separate multiple detectors. | nil | `container,azure` | +| `OTEL_RUBY_ENABLED_INSTRUMENTATIONS` | Configuration used when you only want to install instrumentation for specific libraries. | nil | `redis,active_record` | +| `OTEL_RUBY_AUTO_INSTRUMENTATION_DEBUG` | Set to `true` if want to see some debug information. This to avoid preloading the logger gem | nil | N/A | +| `OTEL_RUBY_UNLOAD_LIBRARY` | Use OTEL_RUBY_UNLOAD_LIBRARY to avoid certain gem preload (esp. google protobuf) | nil | N/A | + +## How can I get involved? + +The `opentelemetry-auto-instrumentation` gem source is on GitHub, along with related gems. + +The OpenTelemetry Ruby gems are maintained by the OpenTelemetry Ruby special interest group (SIG). You can get involved by joining us on our [GitHub Discussions][discussions-url], [Slack Channel][slack-channel] or attending our weekly meeting. See the [meeting calendar][community-meetings] for dates and times. For more information on this and other language SIGs, see the OpenTelemetry [community page][ruby-sig]. + +## License + +The `opentelemetry-auto-instrumentation` gem is distributed under the Apache 2.0 license. See LICENSE for more information. + +[ruby-sig]: https://github.com/open-telemetry/community#ruby-sig +[community-meetings]: https://github.com/open-telemetry/community#community-meetings +[slack-channel]: https://cloud-native.slack.com/archives/C01NWKKMKMY +[discussions-url]: https://github.com/open-telemetry/opentelemetry-ruby/discussions +[opentelemetry-operator]: https://github.com/open-telemetry/opentelemetry-operator diff --git a/opentelemetry-auto-instrumentation/Rakefile b/opentelemetry-auto-instrumentation/Rakefile new file mode 100644 index 0000000000..1a64ba842e --- /dev/null +++ b/opentelemetry-auto-instrumentation/Rakefile @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +# Copyright The OpenTelemetry Authors +# +# SPDX-License-Identifier: Apache-2.0 + +require 'bundler/gem_tasks' +require 'rake/testtask' +require 'yard' +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'] +end + +YARD::Rake::YardocTask.new do |t| + t.stats_options = ['--list-undoc'] +end + +if RUBY_ENGINE == 'truffleruby' + task default: %i[test] +else + task default: %i[test rubocop yard] +end diff --git a/opentelemetry-auto-instrumentation/example/README.md b/opentelemetry-auto-instrumentation/example/README.md new file mode 100644 index 0000000000..ea87807ec0 --- /dev/null +++ b/opentelemetry-auto-instrumentation/example/README.md @@ -0,0 +1,72 @@ +# Example + +## Installation + +Install opentelemetry-auto-instrumentation through `gem install` +Then `bundle install` + +## Simple Example (simple-example) + +```bash +OTEL_RUBY_REQUIRE_BUNDLER=true OTEL_TRACES_EXPORTER=console RUBYOPT="-r opentelemetry-auto-instrumentation" OTEL_RUBY_OPERATOR=false ruby app.rb +``` + +## Rails Example (rails-example) + +Without auto-instrumentation + +```bash +bundle exec rackup config.ru +``` + +In other terminal make the request call + +```bash +wget http://localhost:9292 +# or curl http://localhost:9292 if you have curl on system +``` + +With auto-instrumentation + +```bash +OTEL_RUBY_REQUIRE_BUNDLER=false OTEL_TRACES_EXPORTER=console RUBYOPT="-r opentelemetry-auto-instrumentation" OTEL_RUBY_OPERATOR=false bundle exec rackup config.ru +``` + +The load sequence must be opentelemetry-auto-instrumentation -> user library -> bundler.require (initialize otel sdk and instrumentation installation) + +In other terminal make the request call + +```bash +wget http://localhost:9292 +# or curl http://localhost:9292 if you have curl on system +``` + +### Troubleshooting: Default Gem Version Conflicts + +If you encounter an error like "You have already activated [gem] X.X.X, but your Gemfile requires [gem] Y.Y.Y", this indicates a version conflict with a default gem (such as `json` or `logger`). + +**Error example:** +``` +You have already activated json 2.6.3, but your Gemfile requires json 2.16.0. +Since json is a default gem, you can either remove your dependency on it or +try updating to a newer version of bundler that supports json as a default gem. +``` + +**Solution:** + +Install the specific gem version that your Gemfile requires: + +```bash +gem install [gem-name] -v '[version]' +``` + +For example: +```bash +gem install json -v '2.16.0' +``` + +Then run your application again with auto-instrumentation. + +**Why this happens:** + +The OpenTelemetry gem is loaded before `bundle exec` runs. If the OpenTelemetry dependencies activate a default gem version that differs from what your Gemfile specifies, Bundler will raise an error. Installing the required version explicitly resolves this conflict. diff --git a/opentelemetry-auto-instrumentation/example/rails-example/Gemfile b/opentelemetry-auto-instrumentation/example/rails-example/Gemfile new file mode 100644 index 0000000000..5318a25bf7 --- /dev/null +++ b/opentelemetry-auto-instrumentation/example/rails-example/Gemfile @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +# Gemfile +source 'https://rubygems.org' + +gem 'rails' +gem 'rack' +gem 'rake', '13.0.6' +gem 'bigdecimal', '3.1.3' +gem 'logger', '1.5.3' +gem 'webrick' diff --git a/opentelemetry-auto-instrumentation/example/rails-example/app.rb b/opentelemetry-auto-instrumentation/example/rails-example/app.rb new file mode 100644 index 0000000000..9ffc35f864 --- /dev/null +++ b/opentelemetry-auto-instrumentation/example/rails-example/app.rb @@ -0,0 +1,47 @@ +# frozen_string_literal: true + +# Copyright The OpenTelemetry Authors +# +# SPDX-License-Identifier: Apache-2.0 + +require 'rails' +require 'action_controller/railtie' + +require 'bundler' +Bundler.require + +# MyApp +class MyApp < Rails::Application + config.secret_key_base = 'your_secret_key_here' + config.eager_load = false + config.logger = Logger.new($stdout) + config.api_only = true + config.active_support.to_time_preserves_timezone = :zone + + routes.draw do + get '/', to: 'application#index' + get '/hello', to: 'application#hello' + post '/data', to: 'application#create' + end +end + +# ApplicationController +class ApplicationController < ActionController::API + def index + render json: { message: 'Hello World!', time: Time.current } + end + + def hello + name = params[:name] || 'World' + render json: { greeting: "Hello #{name}!" } + end + + def create + render json: { + message: 'Data received', + data: params.except(:controller, :action) + } + end +end + +MyApp.initialize! diff --git a/opentelemetry-auto-instrumentation/example/rails-example/config.ru b/opentelemetry-auto-instrumentation/example/rails-example/config.ru new file mode 100644 index 0000000000..3ca0d86570 --- /dev/null +++ b/opentelemetry-auto-instrumentation/example/rails-example/config.ru @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +require_relative 'app' + +run MyApp diff --git a/opentelemetry-auto-instrumentation/example/simple-example/Gemfile b/opentelemetry-auto-instrumentation/example/simple-example/Gemfile new file mode 100644 index 0000000000..ebc45888f5 --- /dev/null +++ b/opentelemetry-auto-instrumentation/example/simple-example/Gemfile @@ -0,0 +1,3 @@ +# frozen_string_literal: true + +source 'https://rubygems.org' diff --git a/opentelemetry-auto-instrumentation/example/simple-example/app.rb b/opentelemetry-auto-instrumentation/example/simple-example/app.rb new file mode 100644 index 0000000000..4d6ce333ea --- /dev/null +++ b/opentelemetry-auto-instrumentation/example/simple-example/app.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +# Copyright The OpenTelemetry Authors +# +# SPDX-License-Identifier: Apache-2.0 + +url = URI.parse('http://catfact.ninja/fact') +req = Net::HTTP::Get.new(url.to_s) +Net::HTTP.start(url.host, url.port) do |http| + http.request(req) +end diff --git a/opentelemetry-auto-instrumentation/lib/opentelemetry-auto-instrumentation.rb b/opentelemetry-auto-instrumentation/lib/opentelemetry-auto-instrumentation.rb new file mode 100644 index 0000000000..1263154a43 --- /dev/null +++ b/opentelemetry-auto-instrumentation/lib/opentelemetry-auto-instrumentation.rb @@ -0,0 +1,161 @@ +# frozen_string_literal: true + +# Copyright The OpenTelemetry Authors +# +# SPDX-License-Identifier: Apache-2.0 + +# OTelBundlerPatch +module OTelBundlerPatch + # Nested module to handle OpenTelemetry initialization logic + module OTelInitializer + @initialized = false + + OTEL_INSTRUMENTATION_MAP = { + 'gruf' => 'OpenTelemetry::Instrumentation::Gruf', + 'trilogy' => 'OpenTelemetry::Instrumentation::Trilogy', + 'active_support' => 'OpenTelemetry::Instrumentation::ActiveSupport', + 'action_pack' => 'OpenTelemetry::Instrumentation::ActionPack', + 'active_job' => 'OpenTelemetry::Instrumentation::ActiveJob', + 'active_record' => 'OpenTelemetry::Instrumentation::ActiveRecord', + 'action_view' => 'OpenTelemetry::Instrumentation::ActionView', + 'action_mailer' => 'OpenTelemetry::Instrumentation::ActionMailer', + 'aws_sdk' => 'OpenTelemetry::Instrumentation::AwsSdk', + 'aws_lambda' => 'OpenTelemetry::Instrumentation::AwsLambda', + 'bunny' => 'OpenTelemetry::Instrumentation::Bunny', + 'lmdb' => 'OpenTelemetry::Instrumentation::LMDB', + 'http' => 'OpenTelemetry::Instrumentation::HTTP', + 'koala' => 'OpenTelemetry::Instrumentation::Koala', + 'active_model_serializers' => 'OpenTelemetry::Instrumentation::ActiveModelSerializers', + 'concurrent_ruby' => 'OpenTelemetry::Instrumentation::ConcurrentRuby', + 'dalli' => 'OpenTelemetry::Instrumentation::Dalli', + 'delayed_job' => 'OpenTelemetry::Instrumentation::DelayedJob', + 'ethon' => 'OpenTelemetry::Instrumentation::Ethon', + 'excon' => 'OpenTelemetry::Instrumentation::Excon', + 'faraday' => 'OpenTelemetry::Instrumentation::Faraday', + 'grape' => 'OpenTelemetry::Instrumentation::Grape', + 'graphql' => 'OpenTelemetry::Instrumentation::GraphQL', + 'http_client' => 'OpenTelemetry::Instrumentation::HttpClient', + 'mongo' => 'OpenTelemetry::Instrumentation::Mongo', + 'mysql2' => 'OpenTelemetry::Instrumentation::Mysql2', + 'net_http' => 'OpenTelemetry::Instrumentation::Net::HTTP', + 'pg' => 'OpenTelemetry::Instrumentation::PG', + 'que' => 'OpenTelemetry::Instrumentation::Que', + 'racecar' => 'OpenTelemetry::Instrumentation::Racecar', + 'rack' => 'OpenTelemetry::Instrumentation::Rack', + 'rails' => 'OpenTelemetry::Instrumentation::Rails', + 'rake' => 'OpenTelemetry::Instrumentation::Rake', + 'rdkafka' => 'OpenTelemetry::Instrumentation::Rdkafka', + 'redis' => 'OpenTelemetry::Instrumentation::Redis', + 'restclient' => 'OpenTelemetry::Instrumentation::RestClient', + 'resque' => 'OpenTelemetry::Instrumentation::Resque', + 'ruby_kafka' => 'OpenTelemetry::Instrumentation::RubyKafka', + 'sidekiq' => 'OpenTelemetry::Instrumentation::Sidekiq', + 'sinatra' => 'OpenTelemetry::Instrumentation::Sinatra' + }.freeze + + def self.detect_resource_from_env + env = ENV['OTEL_RUBY_RESOURCE_DETECTORS'].to_s + additional_resource = ::OpenTelemetry::SDK::Resources::Resource.create({}) + + env.split(',').each do |detector| + case detector + when 'container' + additional_resource = additional_resource.merge(::OpenTelemetry::Resource::Detector::Container.detect) if defined? ::OpenTelemetry::Resource::Detector::Container + when 'google_cloud_platform' + additional_resource = additional_resource.merge(::OpenTelemetry::Resource::Detector::GoogleCloudPlatform.detect) if defined? ::OpenTelemetry::Resource::Detector::GoogleCloudPlatform + when 'azure' + additional_resource = additional_resource.merge(::OpenTelemetry::Resource::Detector::Azure.detect) if defined? ::OpenTelemetry::Resource::Detector::Azure + when 'aws' + additional_resource = additional_resource.merge(::OpenTelemetry::Resource::Detector::AWS.detect(%i[ec2 ecs eks lambda])) if defined? ::OpenTelemetry::Resource::Detector::AWS + end + end + + additional_resource + end + + def self.determine_enabled_instrumentation + env = ENV['OTEL_RUBY_ENABLED_INSTRUMENTATIONS'].to_s + + env.split(',').map { |instrumentation| OTEL_INSTRUMENTATION_MAP[instrumentation] } + end + + def self.require_otel + return if @initialized + + @initialized = true + + lib = File.expand_path('..', __dir__) + $LOAD_PATH.reject! { |path| path.include?('opentelemetry-auto-instrumentation') } + $LOAD_PATH.unshift(lib) + + begin + required_instrumentation = determine_enabled_instrumentation + + OpenTelemetry::SDK.configure do |c| + c.resource = detect_resource_from_env + if required_instrumentation.empty? + c.use_all # enables all instrumentation! + else + required_instrumentation.each do |instrumentation| + c.use instrumentation + end + end + end + OpenTelemetry.logger.info { 'Auto-instrumentation initialized' } + rescue StandardError => e + warn "Auto-instrumentation failed to initialize. Error: #{e.message}" + end + end + end + + # ref: https://github.com/newrelic/newrelic-ruby-agent/blob/dev/lib/boot/strap.rb + def require(...) + super + OTelInitializer.require_otel + end +end + +require 'bundler' + +aws = ENV['OTEL_RUBY_RESOURCE_DETECTORS'].to_s.include?('aws') +container = ENV['OTEL_RUBY_RESOURCE_DETECTORS'].to_s.include?('container') +google_cloud_platform = ENV['OTEL_RUBY_RESOURCE_DETECTORS'].to_s.include?('google_cloud_platform') +azure = ENV['OTEL_RUBY_RESOURCE_DETECTORS'].to_s.include?('azure') + +# set OTEL_OPERATOR to false if not in autoinstrumentation-ruby image, default to /otel-auto-instrumentation-ruby +# /otel-auto-instrumentation-ruby is set in opentelemetry-operator ruby.go +operator_gem_path = ENV['OTEL_RUBY_OPERATOR'].nil? || ENV['OTEL_RUBY_OPERATOR'] == 'true' ? '/otel-auto-instrumentation-ruby' : nil +additional_gem_path = operator_gem_path || ENV['OTEL_RUBY_ADDITIONAL_GEM_PATH'] || Gem.dir +$stdout.puts "Loading the additional gem path from #{additional_gem_path}" if ENV['OTEL_RUBY_AUTO_INSTRUMENTATION_DEBUG'] == 'true' + +# use OTEL_RUBY_UNLOAD_LIBRARY to avoid certain gem preload (esp. google protobuf) +# e.g export OTEL_RUBY_UNLOAD_LIBRARY=google-protobuf;googleapis-common-protos-types +unload_libraries = ENV['OTEL_RUBY_UNLOAD_LIBRARY'].to_s.split(';') + +loaded_library_file_path = Dir.glob("#{additional_gem_path}/gems/*").select do |file_path| + gem_name = file_path.match(%r{/gems/([^/]+)-\d})[1] + (file_path.include?('opentelemetry') || file_path.include?('google')) && !unload_libraries.include?(gem_name) +end + +# rubocop:disable Style/StderrPuts +$stderr.puts "Loaded Library File Paths #{loaded_library_file_path.join(',')}" if ENV['OTEL_RUBY_AUTO_INSTRUMENTATION_DEBUG'] == 'true' +# rubocop:enable Style/StderrPuts + +loaded_library_file_path.each do |file_path| + $LOAD_PATH.unshift("#{file_path}/lib") +end + +$stdout.puts "$LOAD_PATH after unshift: #{$LOAD_PATH.join(',')}" if ENV['OTEL_RUBY_AUTO_INSTRUMENTATION_DEBUG'] == 'true' + +require 'opentelemetry-sdk' +require 'opentelemetry-instrumentation-all' +require 'opentelemetry-exporter-otlp' + +require 'opentelemetry-resource-detector-container' if container +require 'opentelemetry-resource-detector-google_cloud_platform' if google_cloud_platform +require 'opentelemetry-resource-detector-azure' if azure +require 'opentelemetry-resource-detector-aws' if aws + +Bundler::Runtime.prepend(OTelBundlerPatch) + +Bundler.require if ENV['OTEL_RUBY_REQUIRE_BUNDLER'] == 'true' diff --git a/opentelemetry-auto-instrumentation/lib/version.rb b/opentelemetry-auto-instrumentation/lib/version.rb new file mode 100644 index 0000000000..57bc2eb570 --- /dev/null +++ b/opentelemetry-auto-instrumentation/lib/version.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +# Copyright The OpenTelemetry Authors +# +# SPDX-License-Identifier: Apache-2.0 + +VERSION = '0.0.0' diff --git a/opentelemetry-auto-instrumentation/opentelemetry-auto-instrumentation.gemspec b/opentelemetry-auto-instrumentation/opentelemetry-auto-instrumentation.gemspec new file mode 100644 index 0000000000..07958a168a --- /dev/null +++ b/opentelemetry-auto-instrumentation/opentelemetry-auto-instrumentation.gemspec @@ -0,0 +1,46 @@ +# frozen_string_literal: true + +# Copyright The OpenTelemetry Authors +# +# SPDX-License-Identifier: Apache-2.0 + +lib = File.expand_path('lib', __dir__) +$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) +require 'version' + +Gem::Specification.new do |spec| + spec.name = 'opentelemetry-auto-instrumentation' + spec.version = VERSION + spec.authors = ['OpenTelemetry Authors'] + spec.email = ['cncf-opentelemetry-contributors@lists.cncf.io'] + + spec.summary = 'auto-instrumentation for opentelemetry ruby' + spec.description = 'auto-instrumentation for opentelemetry ruby' + spec.homepage = 'https://github.com/open-telemetry/opentelemetry-ruby-contrib' + spec.license = 'Apache-2.0' + + spec.files = Dir.glob('lib/**/*.rb') + + Dir.glob('*.md') + + ['LICENSE'] + + spec.require_paths = ['lib'] + spec.required_ruby_version = '>= 3.2' + + spec.add_dependency 'opentelemetry-api', '~> 1.7.0' + spec.add_dependency 'opentelemetry-exporter-otlp', '~> 0.31.1' + spec.add_dependency 'opentelemetry-helpers-mysql', '~> 0.4.0' + spec.add_dependency 'opentelemetry-helpers-sql-obfuscation', '~> 0.5.0' + spec.add_dependency 'opentelemetry-instrumentation-all', '~> 0.86.1' + spec.add_dependency 'opentelemetry-resource-detector-aws', '~> 0.5.0' + spec.add_dependency 'opentelemetry-resource-detector-azure', '~> 0.3.0' + spec.add_dependency 'opentelemetry-resource-detector-container', '~> 0.3.0' + spec.add_dependency 'opentelemetry-resource-detector-google_cloud_platform', '~> 0.3.0' + spec.add_dependency 'opentelemetry-sdk', '~> 1.10.0' + + if spec.respond_to?(:metadata) + spec.metadata['changelog_uri'] = "https://rubydoc.info/gems/#{spec.name}/#{spec.version}/file/CHANGELOG.md" + spec.metadata['source_code_uri'] = 'https://github.com/open-telemetry/opentelemetry-ruby-contrib/opentelemetry-auto-instrumentation' + spec.metadata['bug_tracker_uri'] = 'https://github.com/open-telemetry/opentelemetry-ruby-contrib/issues' + spec.metadata['documentation_uri'] = "https://rubydoc.info/gems/#{spec.name}/#{spec.version}" + end +end diff --git a/opentelemetry-auto-instrumentation/test/opentelemetry-auto-instrumentation_test.rb b/opentelemetry-auto-instrumentation/test/opentelemetry-auto-instrumentation_test.rb new file mode 100644 index 0000000000..6d93430da3 --- /dev/null +++ b/opentelemetry-auto-instrumentation/test/opentelemetry-auto-instrumentation_test.rb @@ -0,0 +1,103 @@ +# frozen_string_literal: true + +# Copyright The OpenTelemetry Authors +# +# SPDX-License-Identifier: Apache-2.0 + +require 'test_helper' + +describe 'AutoInstrumentation' do + let(:auto_instrumentation_path) { File.expand_path('../lib/opentelemetry-auto-instrumentation.rb', __dir__) } + + before do + ENV['OTEL_RUBY_ENABLED_INSTRUMENTATIONS'] = nil + ENV['OTEL_RUBY_INSTRUMENTATION_NET_HTTP_ENABLED'] = nil + ENV['OTEL_RUBY_RESOURCE_DETECTORS'] = nil + ENV['OTEL_RUBY_REQUIRE_BUNDLER'] = nil + ENV['OTEL_RUBY_AUTO_INSTRUMENTATION_DEBUG'] = nil + end + + after do + ENV['OTEL_RUBY_ENABLED_INSTRUMENTATIONS'] = nil + ENV['OTEL_RUBY_INSTRUMENTATION_NET_HTTP_ENABLED'] = nil + ENV['OTEL_RUBY_RESOURCE_DETECTORS'] = nil + ENV['OTEL_RUBY_REQUIRE_BUNDLER'] = nil + ENV['OTEL_RUBY_AUTO_INSTRUMENTATION_DEBUG'] = nil + end + + def run_in_subprocess(env_vars = {}) + # Run the test in a subprocess to avoid contaminating the test environment + read_pipe, write_pipe = IO.pipe + + pid = fork do + read_pipe.close + env_vars.each { |key, value| ENV[key] = value } + + ENV['OTEL_RUBY_REQUIRE_BUNDLER'] = 'false' + begin + # Load the auto-instrumentation library + load auto_instrumentation_path + Bundler.require + + # Get tracer provider information + tracer_provider = OpenTelemetry.tracer_provider + resource = tracer_provider.instance_variable_get(:@resource) + resource_attributes = resource.instance_variable_get(:@attributes) + registry = tracer_provider.instance_variable_get(:@registry) + instrumentation_names = registry.map { |entry| entry.first.name } + + # Serialize and send results back to parent + result = Marshal.dump({ + tracer_provider_class: tracer_provider.class.name, + resource_attributes: resource_attributes, + instrumentation_names: instrumentation_names + }) + write_pipe.write(result) + rescue StandardError => e + error_result = Marshal.dump({ error: e.message, backtrace: e.backtrace }) + write_pipe.write(error_result) + ensure + write_pipe.close + exit!(0) + end + end + + write_pipe.close + result_data = read_pipe.read + read_pipe.close + Process.wait(pid) + + # rubocop:disable Security/MarshalLoad + Marshal.load(result_data) + # rubocop:enable Security/MarshalLoad + end + + it 'simple_load_test' do + result = run_in_subprocess + + _(result[:error]).must_be_nil + _(result[:tracer_provider_class]).must_equal 'OpenTelemetry::SDK::Trace::TracerProvider' + _(result[:resource_attributes]['service.name']).must_equal 'unknown_service' + _(result[:resource_attributes]['telemetry.sdk.name']).must_equal 'opentelemetry' + _(result[:resource_attributes]['telemetry.sdk.language']).must_equal 'ruby' + _(result[:resource_attributes].key?('container.id')).must_equal false + _(result[:instrumentation_names]).must_include 'OpenTelemetry::Instrumentation::Net::HTTP' + _(result[:instrumentation_names]).must_include 'OpenTelemetry::Instrumentation::Rake' + end + + it 'simple_load_with_net_http_disabled' do + result = run_in_subprocess('OTEL_RUBY_INSTRUMENTATION_NET_HTTP_ENABLED' => 'false') + + _(result[:error]).must_be_nil + _(result[:instrumentation_names]).must_include 'OpenTelemetry::Instrumentation::Rake' + _(result[:instrumentation_names]).wont_include 'OpenTelemetry::Instrumentation::Net::HTTP' + end + + it 'simple_load_with_desired_instrument_only' do + result = run_in_subprocess('OTEL_RUBY_ENABLED_INSTRUMENTATIONS' => 'net_http') + + _(result[:error]).must_be_nil + _(result[:instrumentation_names]).must_include 'OpenTelemetry::Instrumentation::Net::HTTP' + _(result[:instrumentation_names]).wont_include 'OpenTelemetry::Instrumentation::Rake' + end +end diff --git a/opentelemetry-auto-instrumentation/test/test_helper.rb b/opentelemetry-auto-instrumentation/test/test_helper.rb new file mode 100644 index 0000000000..0296024b2b --- /dev/null +++ b/opentelemetry-auto-instrumentation/test/test_helper.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +# Copyright The OpenTelemetry Authors +# +# SPDX-License-Identifier: Apache-2.0 + +require 'rake' +require 'minitest' +require 'minitest/autorun' +require 'opentelemetry-sdk' +require 'opentelemetry-instrumentation-all' +require 'opentelemetry-test-helpers' +require 'opentelemetry/resource/detector' +require 'net/http'