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

Add Span.set_attributes method #1520

Merged

Conversation

anton-ryzhov
Copy link
Contributor

Description

Only static span attributes could be added via start_as_current_span when it's used as decorator. If then many dynamic attributes should be added, multiple calls of set_attribute have to be made. Every call acquires/releases a lock, checks if span is ended.

Use case:

@tracer.start_as_current_span('handle_request')
def handle(request):
    current_span = trace_api.get_current_span()
    current_span.set_attribute('http.method', request.method)
    current_span.set_attribute('http.url', request.url)
    ...

vs

@tracer.start_as_current_span('handle_request')
def handle(request):
    trace_api.get_current_span().set_attributes({
        'http.method': request.method,
        'http.url': request.url
    })
    ...

Not less lines, but less copypaste and much more readable (from my perspective).

Also useful when attributes are [conditionally, dynamically] gathered as a dict in many places of a method and then set to span at one call.

Type of change

  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update

How Has This Been Tested?

Unit tests updated respectively

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 opentelemetry-instrumentation/ have changed

  • 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

@anton-ryzhov anton-ryzhov force-pushed the add-set-attributes-method branch from 45d399c to d553806 Compare January 11, 2021 14:56
@anton-ryzhov anton-ryzhov marked this pull request as ready for review January 11, 2021 15:01
@anton-ryzhov anton-ryzhov requested review from a team, codeboten and aabmass and removed request for a team January 11, 2021 15:01
Copy link
Contributor

@codeboten codeboten left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@codeboten codeboten merged commit 6489bf5 into open-telemetry:master Jan 21, 2021
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.

3 participants