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

feat: add tracing helper #650

Merged
merged 1 commit into from Dec 31, 2022
Merged

feat: add tracing helper #650

merged 1 commit into from Dec 31, 2022

Conversation

alnr
Copy link
Contributor

@alnr alnr commented Dec 29, 2022

No description provided.

Copy link
Member

@aeneasr aeneasr left a comment

Choose a reason for hiding this comment

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

What an interesting solution! I was not aware that we can solve the overshadowing problem by pointing to the error variable. The only rare cases I can think of where this would not work is when we create a second (inner) error variable:

func foo() (err error) {
  if err := baz(); err != nil {
    a, innerErr := bar()
    if innerErr != nil {
      return innerErr
    }
    return err
  }
}

But in those cases, we can deal with the result in the code itself by introducing for example a second helper:

func foo() (err error) {
  span, ctx := ...
  defer End(span, &err)
  if err := baz(); err != nil {
    a, innerErr := bar()
    if innerErr != nil {
      return tracerx.TraceError(span, innerErr)
    }
    return err
  }
}

Generally speaking, this solution is a 10x improvement to the callback one. I really like it :)

otelx/withspan.go Outdated Show resolved Hide resolved
Copy link
Contributor

@CaptainStandby CaptainStandby left a comment

Choose a reason for hiding this comment

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

Neat little trick 👍

@alnr alnr force-pushed the tracing-helper branch 2 times, most recently from 675e2f8 to 8c2bcf5 Compare December 30, 2022 09:11
@alnr
Copy link
Contributor Author

alnr commented Dec 30, 2022

What an interesting solution! I was not aware that we can solve the overshadowing problem by pointing to the error variable. The only rare cases I can think of where this would not work is when we create a second (inner) error variable:

func foo() (err error) {
  if err := baz(); err != nil {
    a, innerErr := bar()
    if innerErr != nil {
      return innerErr
    }
    return err
  }
}

This works with my solution 😊 I've added a test to show that. Golang will assign the return value (whatever it's name, if it even has one) to the named return value before running deferred calls, so this solution is always correct.

Generally speaking, this solution is a 10x improvement to the callback one. I really like it :)

Thanks 🥰. WithSpan and End are complementary in my mind. We should use what's more appropriate.

@aeneasr aeneasr merged commit 71af750 into master Dec 31, 2022
@aeneasr aeneasr deleted the tracing-helper branch December 31, 2022 15:31
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