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

Enable passing explicit urls to exclude in instrumentation in FastAPI #486

Merged
merged 19 commits into from
Jun 15, 2021

Conversation

cdvv7788
Copy link
Contributor

@cdvv7788 cdvv7788 commented May 2, 2021

Description

Proposal to solve #375
Currently, only environment variables are supported to exclude URLs. With this change, the users can pass a list explicitly during the instrumentation, so other sources can be easily plugged in (hardcoded, config files, settings, etc).

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?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • Test A: Instrument the app manually passing the excluded_urls list. Verify that no traces are being generated for those urls.
  • Test B: Instrument app automatically (using instrument). You can pass the explicit_urls list to it so. Verify that no traces are being generated for those urls

Does This PR Require a Core Repo Change?

  • Yes. - Link to PR:
  • No.

Checklist:

See contributing.md for styleguide, changelog guidelines, and more.

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

@cdvv7788 cdvv7788 requested review from a team, owais and srikanthccv and removed request for a team May 2, 2021 18:04
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented May 2, 2021

CLA Signed

The committers are authorized under a signed CLA.

@cdvv7788 cdvv7788 changed the title Enable passing explicit urls to exclude from instrumentation in FastAPI Enable passing explicit urls to exclude in instrumentation in FastAPI May 2, 2021
Copy link
Contributor

@owais owais left a comment

Choose a reason for hiding this comment

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

Suggest to rename the global _excluded_urls to _excluded_urls_from_env. It'd make the logic below a lot more readable.

… instrumentation to make implementation more readable
@@ -47,6 +53,7 @@ def instrument_app(app: fastapi.FastAPI, tracer_provider=None):
def _instrument(self, **kwargs):
self._original_fastapi = fastapi.FastAPI
_InstrumentedFastAPI._tracer_provider = kwargs.get("tracer_provider")
_InstrumentedFastAPI._excluded_urls = kwargs.get("excluded_urls")
Copy link
Contributor

Choose a reason for hiding this comment

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

Since _INstrumentedFastAPI is private and not supposed to be used directly, can we move env vs kwargs logic here instead?


.. code-block:: python
from opentelemetry.util.http import ExcludeList
FastAPIInstrumentor.instrument_app(app, ExcludeList("client/.*/info,healthcheck"))
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not allow users to pass in a sequence of strings instead? Basically what can be passed into the env var should be replicated here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No real reason, I can change it to work that 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 think it is preferred. Also users don't have to import ExcludeList either.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@lzchen
Copy link
Contributor

lzchen commented May 4, 2021

@cdvv7788
Are you planning on adding this feature to the rest of the url related instrumentations? If not, I do not believe the original issue is scoped properly. Please create another issue to add this for the remaining instrumentations, and note that FastAPI has already implemented this.

@cdvv7788
Copy link
Contributor Author

cdvv7788 commented May 9, 2021

@lzchen I created a new issue to track this in a more general way: #490

I updated the PR with the recommendations. I still have an issue: https://github.com/open-telemetry/opentelemetry-python-contrib/pull/486/checks?check_run_id=2502217693
That seems to be related to sphinx. What am I doing wrong in there?

@lzchen
Copy link
Contributor

lzchen commented May 10, 2021

@owais
Are your questions addressed?

_excluded_urls_from_env = get_excluded_urls("FASTAPI")


def parse_urls(urls_str):
Copy link
Contributor

Choose a reason for hiding this comment

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

Private method

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@cdvv7788
Copy link
Contributor Author

@ocelotl @lzchen Resolved your comments. Please review.

@cdvv7788 cdvv7788 requested review from ocelotl and lzchen May 30, 2021 16:52
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.

The PR looks good, if you could take a look at fixing the tests, would be happy to merge this as soon as that's done. Thanks for the contribution!

_InstrumentedFastAPI._excluded_urls = (
_excluded_urls_from_env
if _excluded_urls is None
else parse_urls(_excluded_urls)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be parse_excluded_urls?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it should. My bad.
The linter should be passing now.

@lzchen lzchen merged commit 7cae4d3 into open-telemetry:main Jun 15, 2021
@cdvv7788 cdvv7788 deleted the fastapi-excluded_urls branch June 15, 2021 03:33
andresbeckruiz pushed a commit to open-o11y/opentelemetry-python-contrib that referenced this pull request Jun 25, 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.

5 participants