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 "circular require considered harmful" warnings in profiling and tracing integrations #1998

Merged
merged 4 commits into from
May 6, 2022

Conversation

ivoanjo
Copy link
Member

@ivoanjo ivoanjo commented May 3, 2022

The warnings can be spotted by running Ruby with warnings enabled, e.g.

$ bundle exec ruby -W2 -e 'require "ddtrace"'

See individual commits for details.

ivoanjo added 4 commits May 3, 2022 12:20
…tions

A bunch of integrations had a structure where the `integration.rb` file
required `patcher.rb` and then `patcher.rb` did the same thing.

When running Ruby with warnings enabled, this generated a lot of
similar-looking warnings:

```
$ bundle exec ruby -W2 -S -e 'require "ddtrace"'

.../lib/datadog/tracing/contrib/action_view/patcher.rb:7: warning: loading in progress, circular require considered harmful - .../lib/datadog/tracing/contrib/action_view/integration.rb
  from -e:1:in  `<main>'
  from -e:1:in  `require'
  from .../lib/ddtrace.rb:5:in  `<top (required)>'
  from .../lib/ddtrace.rb:5:in  `require'
  from .../lib/datadog/tracing/contrib.rb:36:in  `<top (required)>'
  from .../lib/datadog/tracing/contrib.rb:36:in  `require'
  from .../lib/datadog/tracing/contrib/action_view/integration.rb:4:in  `<top (required)>'
  from .../lib/datadog/tracing/contrib/action_view/integration.rb:4:in  `require'
  from .../lib/datadog/tracing/contrib/action_view/patcher.rb:7:in  `<top (required)>'
  from .../lib/datadog/tracing/contrib/action_view/patcher.rb:7:in  `require'
```

I checked all integrations and these were the ones where the patcher
still required the `integration.rb` file. On all others this was
already correct.
The `datadog/profiling/tasks/setup` file is only directly required by
`datadog/profiling` and so it does not need to itself require
`datadog/profiling` back.

This eliminates yet another warning:

```
.../lib/datadog/profiling/tasks/setup.rb:4: warning: loading in progress, circular require considered harmful - .../lib/datadog/profiling.rb
  from -e:1:in  `<main>'
  from -e:1:in  `require'
  from .../lib/ddtrace.rb:11:in  `<top (required)>'
  from .../lib/ddtrace.rb:11:in  `require'
  from .../lib/datadog/profiling.rb:7:in  `<top (required)>'
  from .../lib/datadog/profiling.rb:9:in  `<module:Datadog>'
  from .../lib/datadog/profiling.rb:166:in  `<module:Profiling>'
  from .../lib/datadog/profiling.rb:155:in  `load_profiling'
  from .../lib/datadog/profiling.rb:155:in  `require'
  from .../lib/datadog/profiling/tasks/setup.rb:4:in  `<top (required)>'
  from .../lib/datadog/profiling/tasks/setup.rb:4:in  `require'
```
…ions

The `tracing/contrib` file already requires `tracing/contrib/extensions`
so the latter should not require the former again.

This eliminates this warning:

```
.../lib/datadog/tracing/contrib/extensions.rb:7: warning: loading in progress, circular require considered harmful - .../lib/datadog/tracing/contrib.rb
  from -e:1:in  `<main>'
  from -e:1:in  `require'
  from .../lib/ddtrace.rb:5:in  `<top (required)>'
  from .../lib/ddtrace.rb:5:in  `require'
  from .../lib/datadog/tracing/contrib.rb:5:in  `<top (required)>'
  from .../lib/datadog/tracing/contrib.rb:5:in  `require'
  from .../lib/datadog/tracing/contrib/extensions.rb:7:in  `<top (required)>'
  from .../lib/datadog/tracing/contrib/extensions.rb:7:in  `require'
```
The `datadog/profiling` file already requires the core, so
`datadog/profiling/encoding/profile` shouldn't require it as well.

Furthermore, `ddtrace.rb` and `datadog/profiling` already require
the core, so `core/configuration/components` should not require
`datadog/profiling` back.

Fixes the following warnings:
```
.../lib/datadog/profiling.rb:3: warning: loading in progress, circular require considered harmful - .../lib/datadog/core.rb
  from -e:1:in  `<main>'
  from -e:1:in  `require'
  from .../lib/ddtrace.rb:4:in  `<top (required)>'
  from .../lib/ddtrace.rb:4:in  `require'
  from .../lib/datadog/tracing.rb:3:in  `<top (required)>'
  from .../lib/datadog/tracing.rb:3:in  `require'
  from .../lib/datadog/core.rb:57:in  `<top (required)>'
  from .../lib/datadog/core.rb:57:in  `require'
  from .../lib/datadog/core/extensions.rb:3:in  `<top (required)>'
  from .../lib/datadog/core/extensions.rb:3:in  `require'
  from .../lib/datadog/core/configuration.rb:4:in  `<top (required)>'
  from .../lib/datadog/core/configuration.rb:4:in  `require'
  from .../lib/datadog/core/configuration/components.rb:14:in  `<top (required)>'
  from .../lib/datadog/core/configuration/components.rb:14:in  `require'
  from .../lib/datadog/profiling.rb:3:in  `<top (required)>'
  from .../lib/datadog/profiling.rb:3:in  `require'

.../lib/datadog/profiling/encoding/profile.rb:6: warning: loading in progress, circular require considered harmful - .../lib/datadog/core.rb
  from -e:1:in  `<main>'
  from -e:1:in  `require'
  from .../lib/ddtrace.rb:4:in  `<top (required)>'
  from .../lib/ddtrace.rb:4:in  `require'
  from .../lib/datadog/tracing.rb:3:in  `<top (required)>'
  from .../lib/datadog/tracing.rb:3:in  `require'
  from .../lib/datadog/core.rb:57:in  `<top (required)>'
  from .../lib/datadog/core.rb:57:in  `require'
  from .../lib/datadog/core/extensions.rb:3:in  `<top (required)>'
  from .../lib/datadog/core/extensions.rb:3:in  `require'
  from .../lib/datadog/core/configuration.rb:4:in  `<top (required)>'
  from .../lib/datadog/core/configuration.rb:4:in  `require'
  from .../lib/datadog/core/configuration/components.rb:14:in  `<top (required)>'
  from .../lib/datadog/core/configuration/components.rb:14:in  `require'
  from .../lib/datadog/profiling.rb:7:in  `<top (required)>'
  from .../lib/datadog/profiling.rb:9:in  `<module:Datadog>'
  from .../lib/datadog/profiling.rb:166:in  `<module:Profiling>'
  from .../lib/datadog/profiling.rb:156:in  `load_profiling'
  from .../lib/datadog/profiling.rb:156:in  `require'
  from .../lib/datadog/profiling/transport/io.rb:4:in  `<top (required)>'
  from .../lib/datadog/profiling/transport/io.rb:4:in  `require'
  from .../lib/datadog/profiling/encoding/profile.rb:6:in  `<top (required)>'
  from .../lib/datadog/profiling/encoding/profile.rb:6:in  `require'
```
Copy link
Member

@marcotc marcotc left a comment

Choose a reason for hiding this comment

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

🧹

@ivoanjo ivoanjo merged commit 99ef8ba into master May 6, 2022
@ivoanjo ivoanjo deleted the ivoanjo/fix-circular-require branch May 6, 2022 07:52
@github-actions github-actions bot added this to the 1.1.0 milestone May 6, 2022
ivoanjo added a commit that referenced this pull request May 9, 2022
…ng core

The fix in #1998, specifically in
2e5d1ee
broke using `datadog/core` directly without `require 'ddtrace'`.

For instance:

```
bundle exec ruby -e "require 'datadog/core'; Datadog.configure { }"
Traceback (most recent call last):
  9: from -e:1:in `<main>'
  8: from .../lib/datadog/core/configuration.rb:89:in `configure'
  7: from .../lib/datadog/core/configuration.rb:223:in `safely_synchronize'
  6: from .../lib/datadog/core/configuration.rb:223:in `synchronize'
  5: from .../lib/datadog/core/configuration.rb:225:in `block in safely_synchronize'
  4: from .../lib/datadog/core/configuration.rb:94:in `block in configure'
  3: from .../lib/datadog/core/configuration.rb:243:in `build_components'
  2: from .../lib/datadog/core/configuration.rb:243:in `new'
  1: from .../lib/datadog/core/configuration/components.rb:345:in `initialize'
.../lib/datadog/core/configuration/components.rb:202:in `build_profiler': undefined method `supported?' for Datadog::Profiling:Module (NoMethodError)
```

As a workaround, I've added a `require 'datadog/profiling'` in
`build_profiler`: this avoids re-adding the circular dependency issue,
but fixes this use-case.

Additionally, I've made is such that profiling is only loaded if it
is enabled, which means that customers not using it will not need to
load it, which is a nice bonus vs what we did prior to #1998.
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.

2 participants