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

auto-instrumentation registration #1304

Merged

Conversation

brettmc
Copy link
Collaborator

@brettmc brettmc commented May 6, 2024

  • porting Nevay's PoC registering auto-instrumentation packages via SPI with file-based config.
  • extend to support auto-loading:
    • use the not-yet-released v0.3 of file-based configuration (which combines SDK and instrumentation config)
    • The existence of the OTEL_EXPERIMENTAL_CONFIG_FILE defines where the SDK config file resides (relative to CWD), and also acts as a trigger to autoload via SPI rather than our current env-based version
    • move single-use code (create hook manager + config parsing + actual registration) into classes
    • support general instrumentation config (which is described in the file-based config spec)
    • support map-based array-like config (which is used by instrumentation configuration)

todo:

  • SDK autoloader should use noop hook manager if opentelemetry extension not available

Copy link

codecov bot commented May 6, 2024

Codecov Report

Attention: Patch coverage is 52.78810% with 127 lines in your changes missing coverage. Please review.

Project coverage is 74.20%. Comparing base (17ad56a) to head (933dce7).

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff              @@
##               main    #1304      +/-   ##
============================================
- Coverage     74.61%   74.20%   -0.41%     
- Complexity     2504     2562      +58     
============================================
  Files           355      372      +17     
  Lines          7180     7386     +206     
============================================
+ Hits           5357     5481     +124     
- Misses         1823     1905      +82     
Flag Coverage Δ
8.1 73.88% <52.78%> (-0.43%) ⬇️
8.2 74.13% <52.78%> (-0.38%) ⬇️
8.3 74.11% <52.78%> (-0.42%) ⬇️
8.4 74.05% <52.78%> (-0.45%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
src/API/Globals.php 100.00% <100.00%> (+5.88%) ⬆️
...tion/AutoInstrumentation/ConfigurationRegistry.php 100.00% <100.00%> (ø)
...PI/Instrumentation/AutoInstrumentation/Context.php 100.00% <100.00%> (ø)
...nstrumentation/AutoInstrumentation/HookManager.php 100.00% <100.00%> (ø)
src/API/Logs/LateBindingLogger.php 100.00% <100.00%> (ø)
src/API/Logs/LateBindingLoggerProvider.php 100.00% <100.00%> (ø)
src/API/Metrics/LateBindingMeterProvider.php 100.00% <100.00%> (ø)
src/API/Trace/LateBindingTracer.php 100.00% <100.00%> (ø)
src/API/Trace/LateBindingTracerProvider.php 100.00% <100.00%> (ø)
.../Config/SDK/Configuration/ConfigurationFactory.php 100.00% <100.00%> (+25.49%) ⬆️
... and 13 more

... and 9 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 17ad56a...933dce7. Read the comment docs.

src/SDK/Sdk.php Outdated Show resolved Hide resolved
src/API/Globals.php Outdated Show resolved Hide resolved
- deptrac was rightly complaining that API (indirectly) depended on SDK through config/sdk. For now,
remove usage of Config\SDK\Configuration\Context
- update deptrac config to allow some new dependencies
psalm doesn't complain now, so that should be good
@brettmc brettmc marked this pull request as ready for review May 8, 2024 00:54
@brettmc brettmc requested a review from a team May 8, 2024 00:54
- make "register global" a function of Sdk, but keep the sdk builder's interface intact
- invent an API instrumentation context, similar to the one in config/sdk, to pass providers
  to instrumentations
- add an initial test of autoloading from a config file
in passing, remove some dead code for handling invalid booleans - config already handles this correctly
src/SDK/SdkAutoloader.php Outdated Show resolved Hide resolved
src/SDK/SdkAutoloader.php Outdated Show resolved Hide resolved
@brettmc
Copy link
Collaborator Author

brettmc commented Jun 25, 2024

@open-telemetry/php-approvers @Nevay I think I'm happy with this and it's ready for review!

NB that we should not merge until a new version of opentelemetry-config is tagged, which should be v0.3

Comment on lines +137 to +141
foreach ($value as $name => $v) {
$provider = $this->providers[$type][$name];
$this->resources?->addClassResource($provider);
$validated[] = new ComponentPlugin($v, $this->providers[$type][$name]);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

If we could work out a way to provide the default config rather than empty (ie, by processing the InstrumentationConfiguration without input), it might work a bit better?

We could add an additional flags that allows instantiating all component plugins by iterating over $this->providers[$type] instead of $value to provide default configurations for instrumentations.

;
});
if (Configuration::has(Variables::OTEL_EXPERIMENTAL_CONFIG_FILE)) {
Globals::registerInitializer(fn ($configurator) => self::fileBasedInitializer($configurator));
Copy link
Contributor

Choose a reason for hiding this comment

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

We'll need to disable the hook manager during initialization to avoid autoinstrumenting the SDK.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Discussed at SIG, and we couldn't think of a scenario where SDK would be instrumented only during initialization?

Copy link
Contributor

Choose a reason for hiding this comment

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

The SDK might use classes/methods that are autoinstrumented by a user (http clients etc.); SDK exports use the context that was active during the SDK initialization (related: #1304 (comment)) -> disabling the hooks during initialization disables them for all SDK exports.

(AFAIK Java avoids this issue by loading autoinstrumentation classes in a separate classloader)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Understood (and I now remember this happening in #878 ). I think I've done this (by making hookmanager enable/disable static methods which seemed the cleanest solution) - WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

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

If we want to make enabling/disabling static: methods should be moved out of ExtensionHookManager.
Alternatively we could provide an accessor to the global hook manager and still allow enabling / disabling hook managers independently.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ok, re-jiggered things a little by moving the static methods into HookManager. How does this approach look?

The main wart I see is that I renamed the existing interface, HookManager to HookManagerInterface which looks untidy in composer.json's extra.spi. But, HookManager::disable() seemed like the most obvious approach.

The most obvious place to do this is in a class named HookManager, which _was_ an
interface for hook managers. Rename existing HookManager to HookManagerInterface,
then re-purpose HookManager for globally enabling/disabling hook managers as well
as determining the disabled status.
@bobstrecansky bobstrecansky merged commit 68b1b43 into open-telemetry:main Jul 22, 2024
9 of 10 checks passed
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.

4 participants