diff --git a/.cspell.yml b/.cspell.yml index c63eaf0f6..e217851c6 100644 --- a/.cspell.yml +++ b/.cspell.yml @@ -82,4 +82,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 37149bda2..25bad1673 100644 --- a/.github/workflows/ci-contrib.yml +++ b/.github/workflows/ci-contrib.yml @@ -240,3 +240,69 @@ jobs: with: gem: "opentelemetry-sampler-${{ matrix.gem }}" ruby: "jruby-9.4.12.0" + + auto-instrumentation: + name: "auto-instrumentation / ubuntu-latest" + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: "Test Ruby 3.4" + uses: ruby/setup-ruby@v1.247.0 + with: + ruby-version: "3.4" + - name: Test Gem + shell: bash + run: | + bundle install --quiet --jobs=3 --retry=4 + bundle exec rake test + rm -f Gemfile.lock + working-directory: './opentelemetry-auto-instrumentation' + - name: "Test Ruby 3.3" + uses: ruby/setup-ruby@v1.247.0 + with: + ruby-version: "3.3" + - name: Test Gem + shell: bash + run: | + bundle install --quiet --jobs=3 --retry=4 + bundle exec rake test + rm -f Gemfile.lock + working-directory: './opentelemetry-auto-instrumentation' + - name: "Test Ruby 3.2" + uses: ruby/setup-ruby@v1.247.0 + with: + ruby-version: "3.2" + - name: Test Gem + shell: bash + run: | + bundle install --quiet --jobs=3 --retry=4 + bundle exec rake test + rm -f Gemfile.lock + working-directory: './opentelemetry-auto-instrumentation' + + - name: YARD + shell: bash + run: | + # 📄 Yard Docs 📄 + bundle exec rake yard + working-directory: './opentelemetry-auto-instrumentation' + + - name: Rubocop + shell: bash + run: | + # 🤖 Rubocop 🤖 + bundle exec rake rubocop + working-directory: './opentelemetry-auto-instrumentation' + + - name: Coverage + shell: bash + run: | + bundle exec ruby -e 'require "simplecov"; SimpleCov.minimum_coverage(85)' + working-directory: './opentelemetry-auto-instrumentation' + + - name: Build Gem + shell: bash + run: | + # 📦 Build Gem 📦 + gem build opentelemetry-auto-instrumentation.gemspec + working-directory: './opentelemetry-auto-instrumentation' diff --git a/opentelemetry-auto-instrumentation/.rubocop.yml b/opentelemetry-auto-instrumentation/.rubocop.yml new file mode 100644 index 000000000..154b948f5 --- /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 000000000..d068b6d9e --- /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 000000000..228d70066 --- /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 000000000..1ef7dad2c --- /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 000000000..980709752 --- /dev/null +++ b/opentelemetry-auto-instrumentation/README.md @@ -0,0 +1,152 @@ +# 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 initialize the SDK. 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 +``` + +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 +``` + +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 +``` + +## 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 000000000..1a64ba842 --- /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 000000000..480a7c00e --- /dev/null +++ b/opentelemetry-auto-instrumentation/example/README.md @@ -0,0 +1,39 @@ +# 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 +``` diff --git a/opentelemetry-auto-instrumentation/example/rails-example/Gemfile b/opentelemetry-auto-instrumentation/example/rails-example/Gemfile new file mode 100644 index 000000000..5318a25bf --- /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 000000000..9ffc35f86 --- /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 000000000..3ca0d8657 --- /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 000000000..ebc45888f --- /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 000000000..4d6ce333e --- /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 000000000..374c38200 --- /dev/null +++ b/opentelemetry-auto-instrumentation/lib/opentelemetry-auto-instrumentation.rb @@ -0,0 +1,153 @@ +# 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 + 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 + 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 + +$stdout.puts "Loaded Library File Paths #{loaded_library_file_path.join(',')}" if ENV['OTEL_RUBY_AUTO_INSTRUMENTATION_DEBUG'] == 'true' + +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 000000000..57bc2eb57 --- /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 000000000..47ad6d51f --- /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.4.0' + spec.add_dependency 'opentelemetry-exporter-otlp', '~> 0.29.1' + spec.add_dependency 'opentelemetry-helpers-mysql', '~> 0.2.0' + spec.add_dependency 'opentelemetry-helpers-sql-obfuscation', '~> 0.3.0' + spec.add_dependency 'opentelemetry-instrumentation-all', '~> 0.72.0' + spec.add_dependency 'opentelemetry-resource-detector-aws', '~> 0.2.0' + spec.add_dependency 'opentelemetry-resource-detector-azure', '~> 0.2.0' + spec.add_dependency 'opentelemetry-resource-detector-container', '~> 0.2.0' + spec.add_dependency 'opentelemetry-resource-detector-google_cloud_platform', '~> 0.2.0' + spec.add_dependency 'opentelemetry-sdk', '~> 1.6.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 000000000..dc6b38271 --- /dev/null +++ b/opentelemetry-auto-instrumentation/test/opentelemetry-auto-instrumentation_test.rb @@ -0,0 +1,109 @@ +# frozen_string_literal: true + +# Copyright The OpenTelemetry Authors +# +# SPDX-License-Identifier: Apache-2.0 + +require 'test_helper' + +describe 'AutoInstrumentation' do + before do + OpenTelemetry::TestHelpers.reset_opentelemetry + end + + after do + # Clean up constants and methods if they exist + OTelBundlerPatch::Initializer.send(:remove_const, :OTEL_INSTRUMENTATION_MAP) if defined?(OTelBundlerPatch::Initializer::OTEL_INSTRUMENTATION_MAP) + + # Remove singleton methods from Initializer + if defined?(OTelBundlerPatch::Initializer) + %i[detect_resource_from_env determine_enabled_instrumentation require_otel].each do |method| + OTelBundlerPatch::Initializer.singleton_class.send(:undef_method, method) if OTelBundlerPatch::Initializer.respond_to?(method) + end + end + + # Remove the Initializer module + OTelBundlerPatch.send(:remove_const, :Initializer) if defined?(OTelBundlerPatch::Initializer) + + # Remove instance methods from OTelBundlerPatch + %i[require].each do |method| + OTelBundlerPatch.send(:undef_method, method) if OTelBundlerPatch.method_defined?(method) + end + + # Reset instrumentation installation state + [ + OpenTelemetry::Instrumentation::Net::HTTP::Instrumentation, + OpenTelemetry::Instrumentation::Rake::Instrumentation + ].each do |instrumentation| + instance = instrumentation.instance_variable_get(:@instance) + instance&.instance_variable_set(:@installed, false) + end + + # Clean up environment variables + ENV['OTEL_RUBY_ENABLED_INSTRUMENTATIONS'] = nil + ENV['OTEL_RUBY_INSTRUMENTATION_NET_HTTP_ENABLED'] = nil + ENV['OTEL_RUBY_RESOURCE_DETECTORS'] = nil + end + + it 'simple_load_test' do + load './lib/opentelemetry-auto-instrumentation.rb' + Bundler.require + + _(OpenTelemetry.tracer_provider.class).must_equal OpenTelemetry::SDK::Trace::TracerProvider + + resource_attributes = OpenTelemetry.tracer_provider.instance_variable_get(:@resource).instance_variable_get(:@attributes) + + _(resource_attributes['service.name']).must_equal 'unknown_service' + _(resource_attributes['telemetry.sdk.name']).must_equal 'opentelemetry' + _(resource_attributes['telemetry.sdk.language']).must_equal 'ruby' + _(resource_attributes.key?('container.id')).must_equal false + + registry = OpenTelemetry.tracer_provider.instance_variable_get(:@registry) + instrumentation_names = registry.map { |entry| entry.first.name }.sort + + _(instrumentation_names).must_include 'OpenTelemetry::Instrumentation::Net::HTTP' + _(instrumentation_names).must_include 'OpenTelemetry::Instrumentation::Rake' + end + + it 'simple_load_with_net_http_disabled' do + ENV['OTEL_RUBY_INSTRUMENTATION_NET_HTTP_ENABLED'] = 'false' + + load './lib/opentelemetry-auto-instrumentation.rb' + Bundler.require + + registry = OpenTelemetry.tracer_provider.instance_variable_get(:@registry) + instrumentation_names = registry.map { |entry| entry.first.name } + + _(instrumentation_names).must_include 'OpenTelemetry::Instrumentation::Rake' + _(instrumentation_names).wont_include 'OpenTelemetry::Instrumentation::Net::HTTP' + + ENV['OTEL_RUBY_INSTRUMENTATION_NET_HTTP_ENABLED'] = nil + end + + it 'simple_load_with_desired_instrument_only' do + ENV['OTEL_RUBY_ENABLED_INSTRUMENTATIONS'] = 'net_http' + + load './lib/opentelemetry-auto-instrumentation.rb' + Bundler.require + + registry = OpenTelemetry.tracer_provider.instance_variable_get(:@registry) + instrumentation_names = registry.map { |entry| entry.first.name } + + _(instrumentation_names).must_include 'OpenTelemetry::Instrumentation::Net::HTTP' + _(instrumentation_names).wont_include 'OpenTelemetry::Instrumentation::Rake' + end + + it 'simple_load_with_additional_resource' do + ENV['OTEL_RUBY_RESOURCE_DETECTORS'] = 'container' + + load './lib/opentelemetry-auto-instrumentation.rb' + Bundler.require + + resource_attributes = OpenTelemetry.tracer_provider.instance_variable_get(:@resource).instance_variable_get(:@attributes) + _(resource_attributes.key?('container.id')).must_equal true + _(resource_attributes['telemetry.sdk.name']).must_equal 'opentelemetry' + _(resource_attributes['telemetry.sdk.language']).must_equal 'ruby' + + ENV['OTEL_RUBY_RESOURCE_DETECTORS'] = nil + 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 000000000..0296024b2 --- /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'