Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix profiler wrongly detecting that google-protobuf is installed #2595

Merged
merged 1 commit into from
Feb 3, 2023

Conversation

ivoanjo
Copy link
Member

@ivoanjo ivoanjo commented Feb 2, 2023

What does this PR do?:

The google-protobuf gem is a dependency needed by the profiler (that we plan on removing when we fully move to libdatadog), but that is not a dependency of the ddtrace gem.

Because it's not a dependency of ddtrace, we need to gracefully handle the case where this gem is missing. Thus, before requiring any files from this gem, the profiler uses rubygems to check if the gem is available.

But, in #1506, a customer asked us to allow skipping this API call to rubygems, since it didn't work on their setup. Instead, we added an extra check -- if the ::Google::Protobuf module is available, then we assume protobuf is installed.

Very very unfortunately, there's actually another gem, called protobuf (or protobuf-cucumber, its fork) that is not compatible with ddtrace BUT STILL defines ::Google::Protobuf.

This is horrifying -- having two different gems that define different versions of a module with the same name.

But in this very bizarre situation, the google-protobuf detection code got confused, and one internal customer reported that he saw the following error on their logs:

[DDTRACE] Error while loading google-protobuf gem. Cause: 'LoadError cannot load such file -- google/protobuf' Location: 'lib/datadog/profiling.rb:106:in `require''. This can happen when google-protobuf is missing its native components. [...]

To fix this, I've tweaked the google-protobuf detection to also check for the ::Protobuf module. This top-level module is never defined by google-protobuf, and we can use it to distinguish between the two gems.

Motivation:

Avoid misleading error message when loading ddtrace with protobuf/protobuf-cucumber installed.

Additional Notes:

I'm looking forward to dropping this whole google-protobuf code once the new profiler becomes the default for all customers.

How to test the change?:

The following trivial Ruby app is able to trigger the issue.

  • gems.rb:
source 'http://rubygems.org'
gem 'protobuf-cucumber'
gem 'ddtrace'
  • example.rb:
require 'protobuf'
require 'ddtrace'
puts "Works!"

**What does this PR do?**:

The `google-protobuf` gem is a dependency needed by the profiler
(that we plan on removing when we fully move to libdatadog), but that
is not a dependency of the `ddtrace` gem.

Because it's not a dependency of `ddtrace`, we need to gracefully handle
the case where this gem is missing. Thus, before requiring any files
from this gem, the profiler uses rubygems to check if the gem is
available.

But, in #1506, a customer asked us to allow skipping this API call
to rubygems, since it didn't work on their setup. Instead, we added
an extra check -- if the `::Google::Protobuf` module is available,
then we assume protobuf is installed.

**Very very unfortunately**, there's actually another gem, called
`protobuf` (or `protobuf-cucumber`, its fork) that is not compatible
with `ddtrace` BUT STILL defines `::Google::Protobuf`.

This is _horrifying_ -- having two different gems that define different
versions of a module with the same name.

But in this very bizarre situation, the `google-protobuf` detection
code got confused, and one internal customer reported that he saw
the following error on their logs:

> [DDTRACE] Error while loading google-protobuf gem. Cause: 'LoadError
> cannot load such file -- google/protobuf' Location:
> 'lib/datadog/profiling.rb:106:in `require''. This can happen when
> google-protobuf is missing its native components. [...]
```

To fix this, I've tweaked the `google-protobuf` detection to also
check for the `::Protobuf` module. This top-level module is never
defined by `google-protobuf`, and we can use it to distinguish
between the two gems.

**Motivation**:

Avoid misleading error message when loading `ddtrace` with
`protobuf`/`protobuf-cucumber` installed.

**Additional Notes**:

I'm looking forward to dropping this whole `google-protobuf` code
once the new profiler becomes the default for all customers.

**How to test the change?**:

The following trivial Ruby app is able to trigger the issue.

* gems.rb:

```ruby
source 'http://rubygems.org'
gem 'protobuf-cucumber'
gem 'ddtrace'
```

* example.rb:

```ruby
require 'protobuf'
require 'ddtrace'
puts "Works!"
```
@ivoanjo ivoanjo requested a review from a team February 2, 2023 15:24
@codecov-commenter
Copy link

Codecov Report

Merging #2595 (47340ba) into master (2f31cd8) will decrease coverage by 0.01%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master    #2595      +/-   ##
==========================================
- Coverage   98.04%   98.04%   -0.01%     
==========================================
  Files        1143     1143              
  Lines       62015    62021       +6     
  Branches     2789     2789              
==========================================
+ Hits        60804    60809       +5     
- Misses       1211     1212       +1     
Impacted Files Coverage Δ
lib/datadog/profiling.rb 100.00% <100.00%> (ø)
spec/datadog/profiling_spec.rb 100.00% <100.00%> (ø)
lib/datadog/core/workers/polling.rb 96.55% <0.00%> (-3.45%) ⬇️
lib/datadog/tracing/distributed/propagation.rb 91.11% <0.00%> (-2.23%) ⬇️
...atadog/tracing/contrib/grpc/support/grpc_helper.rb 98.24% <0.00%> (-1.76%) ⬇️
...ng/contrib/active_support/cache/instrumentation.rb 86.20% <0.00%> (-0.10%) ⬇️
...dog/profiling/collectors/cpu_and_wall_time_spec.rb 97.88% <0.00%> (+0.42%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@ivoanjo ivoanjo merged commit bbdb316 into master Feb 3, 2023
@ivoanjo ivoanjo deleted the ivoanjo/fix-google-protobuf-detection branch February 3, 2023 08:46
@github-actions github-actions bot added this to the 1.10.0 milestone Feb 3, 2023
@TonyCTHsu TonyCTHsu mentioned this pull request Mar 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants