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

Meaningful message when span name is not a string #3955

Closed
wants to merge 8 commits into from

Conversation

soumyadeepm04
Copy link
Contributor

Description

When the span name is not a string, the error message is confusing. Logs a warning message "span name must be a string" with the stack trace to help identify where the issue is. Converts the span name to a string and continues execution.

Fixes #3918

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Ran the code given in the issue to reproduce the issue.

Does This PR Require a Contrib Repo Change?

Answer the following question based on these examples of changes that would require a Contrib Repo Change:

  • The OTel specification has changed which prompted this PR to update the method interfaces of opentelemetry-api/ or opentelemetry-sdk/

  • The method interfaces of test/util have changed

  • Scripts in scripts/ that were copied over to the Contrib repo have changed

  • Configuration files that were copied over to the Contrib repo have changed (when consistency between repositories is applicable) such as in

    • pyproject.toml
    • isort.cfg
    • .flake8
  • When a new .github/CODEOWNER is added

  • Major changes to project information, such as in:

    • README.md
    • CONTRIBUTING.md
  • Yes. - Link to PR:

  • No.

Checklist:

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

@soumyadeepm04 soumyadeepm04 requested a review from a team as a code owner June 5, 2024 22:18
@soumyadeepm04 soumyadeepm04 marked this pull request as draft June 5, 2024 22:41
@soumyadeepm04 soumyadeepm04 marked this pull request as ready for review June 5, 2024 23:12
Copy link
Member

@emdneto emdneto left a comment

Choose a reason for hiding this comment

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

It would be nice if you also provide some tests for this. It makes it easier for some people to review your change

logger.warning(
"span name must be a string.\n Stack trace: %s", stack_trace
)
name = str(name)
Copy link
Member

Choose a reason for hiding this comment

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

Did you consider cases where converting the name to a string might fail?

Copy link
Contributor Author

@soumyadeepm04 soumyadeepm04 Jun 6, 2024

Choose a reason for hiding this comment

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

I cannot really think of any case where this could fail. Did you have any such cases in mind?

Copy link
Member

Choose a reason for hiding this comment

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

It's good that we're logging something, but I'm not sure that we want to log a stack trace here. Is logging a stacktrace done elsewhere in this repo?

Regarding testing, I think we should test passing in a string, and test passing in a non-string, making sure both scenarios produce expected results.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't see logging a stacktrace being done anywhere in the repo. Would you like me to change it and if so, how would you like me to change it?

Sure, working on it now.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe this should just raise a TypeError. Anybody have thoughts?

        if not isinstance(name, str):
            raise TypeError(f"Expected 'name' to be a string, but got {type(name).__name__}")

Copy link
Contributor

Choose a reason for hiding this comment

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

Seems like there's two thing we can do:

  1. Treat this as a misconfiguration/misuse of the api (even though it's not explictly stated that span name is a string) similar to here and raise a typeerror in the create span api
  2. Handle this gracefully and log a warning instead -> attempt to convert the value to a str in the api and throw an error if this attempt fails.

I personally am more for (1), this seems more like a misuse of an api in which every we expect name to be a string. Otherwise, we must do type checks every we use span name.

Copy link
Contributor

Choose a reason for hiding this comment

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

Raising an error here is against the OTel basic error handling principles i believe. Since ReadableSpan.__init__ is called on Span.end a raised error will escape the API/SDK and fail the user's application.
Converting to str is probably fine but should be handled with care since e.g. a custom __str__ implementation might also raise an error.
Another option might be to just replace the span name with some hardcoded "i'm not a str pls fix me" message so users would see in their backend that something isn't quite right.

Copy link
Contributor

Choose a reason for hiding this comment

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

Given the specification quotes here #3955 (comment) and that we have the proper type on the method parameter I'll just leave the code as is. Hopefully in the future users would be able to catch these with a type checker?

Copy link
Contributor

Choose a reason for hiding this comment

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

@xrmx

Should we close the pr in this case? @mariojonke wdyt

Copy link
Contributor

Choose a reason for hiding this comment

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

Should we close the pr in this case? @mariojonke wdyt

Yes

@lzchen
Copy link
Contributor

lzchen commented Jul 31, 2024

@soumyadeepm04

I'll be closing this pr due to this comment, feel free to comment if you want to continue this discussion.

@lzchen lzchen closed this Jul 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
7 participants