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

Split Instrumentation.Run to Load and Run #1245

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

RonFed
Copy link
Contributor

@RonFed RonFed commented Nov 6, 2024

Resolves #1240

  • Split Instrumentation.Run to 2 parts - Load and Run.
  • Remove loadedIndicator.
  • Add Manager.Stop which should be called if Instrumentation.Close is called in between Load and Run.
  • Improve state handling inside the manager and protect it with a mutex.

@RonFed RonFed marked this pull request as ready for review November 6, 2024 12:48
@RonFed RonFed requested a review from a team as a code owner November 6, 2024 12:48
@MrAlias MrAlias added this to the v0.18.0-alpha milestone Nov 7, 2024
instrumentation.go Show resolved Hide resolved
@@ -306,6 +308,26 @@ func (m *Manager) Run(ctx context.Context, target *process.TargetDetails) error
return <-done
}

// Stop stops all probes and cleans up all the resources associated with them.
func (m *Manager) Stop() error {
Copy link
Contributor

Choose a reason for hiding this comment

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

Calling this will not stop Run. Run is waiting for the context to be done. This will clear probes without having that process end.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, this is intentional. However I wasn't sure about this approach.
The idea was to be able to clean up when Stop is called between Load and Run (so just to cleanup all the load stuff). After Rum is called the context is used to clean up everything.
How do you think we can improve this? I think it's quite confusing this way.

Copy link
Contributor

Choose a reason for hiding this comment

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

I would expect most users would want the Manager to stop if they call this, meaning the Run call should return. Given you have state checking added here, I think we could wrap the passed context to Run and sync the stop methods.

For example, we can add a stop field to the Manager.

--- a/internal/pkg/instrumentation/manager.go
+++ b/internal/pkg/instrumentation/manager.go
@@ -58,6 +58,7 @@ type Manager struct {
        cp              ConfigProvider
        exe             *link.Executable
        td              *process.TargetDetails
+       stop            context.CancelCauseFunc
        runningProbesWG sync.WaitGroup
        telemetryCh     chan ptrace.ScopeSpans
        currentConfig   Config

And then in Run:

@@ -283,6 +284,8 @@ func (m *Manager) Run(ctx context.Context) error {
                return errors.New("manager is not loaded, call Load before Run")
        }

+       ctx, m.stop = context.WithCancelCause(ctx)
+
        for id, p := range m.probes {
                if isProbeEnabled(id, m.currentConfig) {
                        m.runProbe(p)
@@ -299,7 +302,10 @@ func (m *Manager) Run(ctx context.Context) error {
                <-ctx.Done()

                err := m.Stop()
-               done <- errors.Join(err, ctx.Err())
+               if e := context.Cause(ctx); !errors.Is(e, errStop) {
+                       err = errors.Join(err, e)
+               }
+               done <- err
        }()

        for e := range m.telemetryCh {

Then we can close this child context when Stop is called:

@@ -308,12 +314,16 @@ func (m *Manager) Run(ctx context.Context) error {
        return <-done
 }

+var errStop = errors.New("stopped called")
+
 // Stop stops all probes and cleans up all the resources associated with them.
 func (m *Manager) Stop() error {
        if m.state == managerStateUninitialized || m.state == managerStateStopped {
                return nil
        }

+       m.stop(errStop)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated, PTAL.

Do you think that after #1248, we can simplify this closing mechanism?

internal/pkg/instrumentation/manager.go Outdated Show resolved Hide resolved
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.

Improve Instrumentation.Run API
3 participants