Skip to content

feat: add gem for auto-instrumentation in otel-operator#1384

Open
xuan-cao-swi wants to merge 85 commits intoopen-telemetry:mainfrom
xuan-cao-swi:auto-instrumentation
Open

feat: add gem for auto-instrumentation in otel-operator#1384
xuan-cao-swi wants to merge 85 commits intoopen-telemetry:mainfrom
xuan-cao-swi:auto-instrumentation

Conversation

@xuan-cao-swi
Copy link
Copy Markdown
Contributor

@xuan-cao-swi xuan-cao-swi commented Jan 30, 2025

Description

This PR aims to contribute a script/gem that can load the necessary OpenTelemetry-related gems and initialize the SDK for the opentelemetry-operator. The script/gem also includes a resource detector as part of its features.

The main idea was inspired by new_relic, which prepends the bundler.require function and places the loading script at the end of the loading procedure. For apps or scripts that don't use Bundler or a Gemfile, more details on variations of app loading can be found in the README. Currently, the goal is to ensure that Rails apps can work, with future improvements in mind.

The reason behind creating this gem is to facilitate easy installation and dependency enforcement. Although for the OpenTelemetry-Operator Node.js, it puts the loading script directly in the src folder (and then copy over to binding volume), it still relies on the auto-instrumentation-node package to install all required packages (e.g., API/SDK and instrumentation packages).

The main purpose of the gem is to serve the OpenTelemetry-Operator, which requires more configuration changes (see the configuration section in the README). Users don't need to modify their Ruby app codebase while using the instrumentation from OpenTelemetry-Ruby, but anyone can still use it in any environment.

The gem name, file structure, and implementation need more suggestions to make them more reasonable. I have tested it with rails and sinatra (through rackup), and I currently still in the process of testing against the opentelemetry operator cluster locally. Once the operator testing done, I will mark the pr as ready.

Related to open-telemetry/opentelemetry-operator#3756

@xuan-cao-swi
Copy link
Copy Markdown
Contributor Author

We don't need to push to make the gem ready/release, but the script file need to get some review first.

@github-actions
Copy link
Copy Markdown
Contributor

👋 This pull request has been marked as stale because it has been open with no activity. You can: comment on the issue or remove the stale label to hold stale off for a while, add the keep label to hold stale off permanently, or do nothing. If you do nothing this pull request will be closed eventually by the stale bot

@github-actions github-actions bot added the stale Marks an issue/PR stale label Mar 29, 2025
@kaylareopelle kaylareopelle removed the stale Marks an issue/PR stale label Apr 2, 2025
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented May 3, 2025

👋 This pull request has been marked as stale because it has been open with no activity. You can: comment on the issue or remove the stale label to hold stale off for a while, add the keep label to hold stale off permanently, or do nothing. If you do nothing this pull request will be closed eventually by the stale bot

@github-actions github-actions bot added the stale Marks an issue/PR stale label May 3, 2025
@xuan-cao-swi xuan-cao-swi requested a review from marcotc February 5, 2026 16:13
@kaylareopelle kaylareopelle dismissed their stale review March 3, 2026 16:59

There have been a lot of changes since my last review. Removing my approval to take another look.

@arielvalentin
Copy link
Copy Markdown
Contributor

@xuan-cao-swi can you rebase main? I will test this ASAP and get it finished this Friday

@github-actions github-actions bot added the ci label Apr 9, 2026
Copy link
Copy Markdown
Contributor

@arielvalentin arielvalentin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One last set of requests.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whenever we patch and introduce methods that are not decorators including class, module, or instance variables we should prefix them with _otel for methods or @_otel for variables.

Any Constants should be marked as private and likely best to prefix with OTEL_ as well.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated with prefix and private_constant

Copy link
Copy Markdown
Contributor

@thompson-tomo thompson-tomo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add it to the auto labeller config

Comment on lines +12 to +19
gem 'bundler'
gem 'minitest'
gem 'rake'
gem 'rubocop'
gem 'rubocop-performance'
gem 'simplecov'
gem 'yard'
gem 'opentelemetry-test-helpers'
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add versions like the others

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

.cspell.yml Outdated
Comment on lines +84 to +91
- Triager
- triagers
- Untrace
- vitess
- webmocks
- yardoc
- rackup
- traceparent
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure all of these are needed, I believe I added some of these to cspell ie yard

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure about what is needed; feel free to suggest which one to remove.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My suggestion would be to remove them all & then see which one's you get warnings for.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed these words

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just checking on this one as I can still see them.

Copy link
Copy Markdown
Contributor

@thompson-tomo thompson-tomo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we also need to add it to toys for the release process?

xuan-cao-swi and others added 6 commits April 13, 2026 11:10
Co-authored-by: Ariel Valentin <arielvalentin@users.noreply.github.com>
Co-authored-by: James Thompson <thompson.tomo@outlook.com>
…lemetry-ruby-contrib into auto-instrumentation
Copy link
Copy Markdown
Contributor

@thompson-tomo thompson-tomo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note we are still needing it to be added to labeller & release process. I have drafted updated docs to help guide this process aka #2219.

Comment on lines +2 to +5
Naming/FileName:
Enabled: false
Gemspec/DevelopmentDependencies:
Enabled: false
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Naming/FileName:
Enabled: false
Gemspec/DevelopmentDependencies:
Enabled: false

If naming is an issue add an exclusion rather than disable entire cop.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.


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/auto-instrumentation'
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update to match existing instrumentations ie use tag and directory.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

gemspec

group :test do
gem 'bundler'
Copy link
Copy Markdown
Contributor

@thompson-tomo thompson-tomo Apr 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should have a version if we in fact need it no other gem list it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the bundler

Copy link
Copy Markdown
Contributor

@kaylareopelle kaylareopelle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is so close! I noticed a few things in my review today.

module OTelInitializer
@initialized = false

OTEL_INSTRUMENTATION_MAP = {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any new instrumentations we should add here?

require 'version'

Gem::Specification.new do |spec|
spec.name = 'auto-instrumentation'
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about calling the gem opentelemetry-auto-instrumentation? Without the opentelemetry prefix it feels very broad.

I remember talking about naming a long time ago, but haven't found that conversation.

@thompson-tomo made a similar comment in the changelog about updating the name.

opentelemetry-instrumentation-all
opentelemetry-exporter-otlp
opentelemetry-helpers-mysql
opentelemetry-helpers-sql-obfuscation
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we want to replace the sql-obfuscation gem with opentelemetry-helpers-sql-processor

opentelemetry-helpers-sql-obfuscation
opentelemetry-resource-detector-azure
opentelemetry-resource-detector-container
opentelemetry-resource-detector-aws
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also have a GCP resource detector. Does that make sense to include?


## Configuration

The following environment variables are specific to this gem (not standard OpenTelemetry variables):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about mentioning that all other OTEL environment variables can still be used for configuration with this gem?


require 'test_helper'

describe 'AutoInstrumentation' do
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
describe 'AutoInstrumentation' do
describe 'OpenTelemetry::AutoInstrumentation' do

Comment on lines +10 to +11
require 'opentelemetry-sdk'
require 'opentelemetry-instrumentation-all'
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we require these and opentelemetry/resource/detector here, could we be getting false positives? (just thinking about open-telemetry/opentelemetry-ruby#1956)

The tests seem to pass even if we remove those requires.

Comment on lines +88 to +93
warn '[OpenTelemetry] WARNING: Detected OpenTelemetry gems in your Gemfile: ' \
"#{gem_names}. When using auto-instrumentation, OpenTelemetry gems are loaded " \
'from the auto-instrumentation gem path, NOT from your bundle. The gem versions ' \
'in your Gemfile/Gemfile.lock are not used and may cause version conflicts or ' \
'unexpected behavior. Please remove these gems from your Gemfile when using ' \
'auto-instrumentation.'
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean that people who want to use metrics or logs can't use auto instrumentation? If that's the case, what do you think about adding a note to the README?

Comment on lines +8 to +10
gem 'rake', '13.0.6'
gem 'bigdecimal', '3.1.3'
gem 'logger', '1.5.3'
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these versions locked?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the main problem I encountered with the rails example. I was using Ruby 3.4 and these versions are older than the latest versions. If I commented these versions out and regenerated the Gemfile.lock, everything worked fine.

When I tried to follow the instructions in the example README and installed the versions it asked for specifically, I kept getting the same error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci keep Ensures stale-bot keeps this issue/PR open

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants