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

Open
wants to merge 6 commits into
base: main
Choose a base branch
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
Member

Choose a reason for hiding this comment

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

@pmcollins, and in that case, we don't try to convert to str, right?
I was wondering about this:

API methods MUST NOT throw unhandled exceptions when used incorrectly by end users. The API and SDK SHOULD provide safe defaults for missing or invalid arguments. For instance, a name like empty may be used if the user passes in null as the span name argument during Span construction.

I didn't find a clarification on what is a valid span name other than this:

The span name SHOULD be the most general string that identifies a (statistically) interesting class of Spans rather than individual Span instances while still being human-readable. That is, "get_user" is a reasonable name, while "get_user/314159", where "314159" is a user ID, is not a good name due to its high cardinality. Generality SHOULD be prioritized over human-readability.

Copy link
Member

@pmcollins pmcollins Jun 10, 2024

Choose a reason for hiding this comment

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

Yes, good point, the exception would have to be handled. And in this case there are two call sites, so perhaps it's better to handle it here in the 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.

Sure, will do that. Also, should I just raise a TypeError as you stated before or should I convert it to a string and continue execution like it is now?

Copy link
Member

@emdneto emdneto Jun 12, 2024

Choose a reason for hiding this comment

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

I think we shouldn't convert to str and we must also be careful not to generate excessive warnings. I would suggest you bring this to next SIG call and get some opinions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants