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

Remove automatic ::shutdown(), add ShutdownHandler::register() #760

Conversation

Nevay
Copy link
Contributor

@Nevay Nevay commented Jul 11, 2022

See #759, removes automatic shutdown handling. Users should keep a reference to the TracerProvider and call ::shutdown() explicitely. ShutdownHandler::register([$tracerProvider, 'shutdown']) can be used to protect against process timeout / exit, but also requires users to keep a reference to the TracerProvider.

The tracer provider should be in scope anyways to be globally accessible in the current execution context (somewhat depends on how instrumentation registration is solved?).

@codecov
Copy link

codecov bot commented Jul 11, 2022

Codecov Report

Merging #760 (e9ab294) into main (37a8c8e) will decrease coverage by 1.16%.
The diff coverage is 30.30%.

Impacted file tree graph

@@             Coverage Diff              @@
##               main     #760      +/-   ##
============================================
- Coverage     79.59%   78.43%   -1.17%     
- Complexity     1295     1331      +36     
============================================
  Files           150      153       +3     
  Lines          3195     3278      +83     
============================================
+ Hits           2543     2571      +28     
- Misses          652      707      +55     
Flag Coverage Δ
7.4 78.43% <30.61%> (-1.15%) ⬇️
8.0 78.50% <30.61%> (-1.15%) ⬇️
8.1 78.50% <30.61%> (-1.15%) ⬇️

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

Impacted Files Coverage Δ
src/SDK/Common/Util/WeakMap.php 0.00% <0.00%> (ø)
src/SDK/Trace/TracerProvider.php 100.00% <ø> (+35.00%) ⬆️
src/SDK/Common/Util/ShutdownHandler.php 63.63% <63.63%> (ø)
src/SDK/Common/Util/functions.php 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

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

@Nevay
Copy link
Contributor Author

Nevay commented Jul 12, 2022

shutdown is called if no new traces can be created

This approach won't work for metrics; the metrics API allows registration of permanent callbacks. Asynchronous Instrument API:

The API MUST support creation of asynchronous instruments by passing zero or more callback functions to be permanently registered to the newly created instrument.

I see three options:

  1. shutdown tracer provider after no new traces can be created, shut down meter provider after last synchronous writer (and non-permanent asynchronous callback?) goes out of scope, which might be unexpected if the user registers only permanent callbacks
  2. keep the current behavior and shut down when tracer provider / meter provider are destroyed
  3. remove automatic shutdown behavior (at least on __destruct(), keep register_shutdown_function() handler?)

We should also consider that ::shutdown() is a blocking operation, triggering this at an arbitrary point might lead to an unexpected delay for the application.

@brettmc
Copy link
Collaborator

brettmc commented Jul 12, 2022

I see three options ....

I'm inclined to think option 3: if you want to shut down a tracer provider, you must keep a reference to it and explicitly call shutdown(). Does that resurrect any tracer provider leaks that you fixed up in the earlier PR?

@Nevay Nevay marked this pull request as draft July 12, 2022 17:13
@Nevay Nevay force-pushed the fix/delay-automatic-tracer-provider-shutdown-until-tracers-out-of-scope branch from 8bc39a5 to a98fcba Compare July 12, 2022 17:19
Copy link
Contributor Author

@Nevay Nevay left a comment

Choose a reason for hiding this comment

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

Does that resurrect any tracer provider leaks that you fixed up in the earlier PR?

No, __destruct() could simply call ::unregisterShutdownFunction() instead of ::shutdown().

Converting this to draft for now; I've decided to create a POC that allows users to opt-in instead of enabling it by default.

ShutdownHandler::register($tracerProvider->shutdown(...)); // Closure::fromCallable([$provider, 'shutdown']) if php < 8.1

/**
* @see https://github.com/amphp/amp/blob/f682341c856b1f688026f787bef4f77eaa5c7970/src/functions.php#L140-L191
*/
private static function weaken(Closure $closure, ?object &$target = null): Closure
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is also very useful for the metrics API; we can add a $weak parameter to asynchronous instruments ::observe() method that would allow users to provide normal callbacks with bound $this instead of forcing them to take care of weakening the $this reference themselves.

@Nevay Nevay force-pushed the fix/delay-automatic-tracer-provider-shutdown-until-tracers-out-of-scope branch from 6a41de0 to 14e6909 Compare July 14, 2022 16:27
@Nevay Nevay force-pushed the fix/delay-automatic-tracer-provider-shutdown-until-tracers-out-of-scope branch from 14e6909 to 7d003ee Compare July 14, 2022 16:33
@Nevay Nevay changed the title Postpone automatic ::shutdown() until tracers/not ended spans are out of scope Remove automatic ::shutdown(), add ShutdownHandler::register() Jul 14, 2022
@Nevay Nevay marked this pull request as ready for review July 14, 2022 16:35
@brettmc
Copy link
Collaborator

brettmc commented Jul 15, 2022

@Nevay let us know if it's ok to merge this now.

@Nevay
Copy link
Contributor Author

Nevay commented Jul 15, 2022

Yes, this can be merged.

@tidal tidal merged commit ae6b620 into open-telemetry:main Jul 16, 2022
wadjei added a commit to wadjei/opentelemetry-php-contrib that referenced this pull request Aug 12, 2022
Since changes in open-telemetry/opentelemetry-php#760,
the original Kernel Listener example no longer works...

Following those changes, the stored Tracer instance gets deleted before the
application terminates so mainSpan can't be ended and none of the collected
trace info gets sent.
By refactoring to use TracerProvider instead, mainSpan's Tracer remains valid
during KernelEvents::TERMINATE so the trace session can be sent successfully.
wadjei added a commit to wadjei/opentelemetry-php-contrib that referenced this pull request Aug 12, 2022
Since changes in open-telemetry/opentelemetry-php#760,
the original Kernel Listener example no longer works...

Following those changes, the stored Tracer instance gets deleted before the
application terminates so mainSpan can't be ended and none of the collected
trace info gets sent.
By refactoring to use TracerProvider instead, mainSpan's Tracer remains valid
during KernelEvents::TERMINATE so the trace session can be sent successfully.
otel-php-bot pushed a commit to opentelemetry-php/contrib-sdk-bundle that referenced this pull request Aug 12, 2022
Since changes in open-telemetry/opentelemetry-php#760,
the original Kernel Listener example no longer works...

Following those changes, the stored Tracer instance gets deleted before the
application terminates so mainSpan can't be ended and none of the collected
trace info gets sent.
By refactoring to use TracerProvider instead, mainSpan's Tracer remains valid
during KernelEvents::TERMINATE so the trace session can be sent successfully.
otel-php-bot pushed a commit to opentelemetry-php/contrib-sdk-bundle that referenced this pull request Oct 23, 2022
Since changes in open-telemetry/opentelemetry-php#760,
the original Kernel Listener example no longer works...

Following those changes, the stored Tracer instance gets deleted before the
application terminates so mainSpan can't be ended and none of the collected
trace info gets sent.
By refactoring to use TracerProvider instead, mainSpan's Tracer remains valid
during KernelEvents::TERMINATE so the trace session can be sent successfully.
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.

None yet

3 participants