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

[FLASK] added request and response hook #416

Merged
merged 42 commits into from
Jun 1, 2021

Conversation

NickSulistio
Copy link
Contributor

@NickSulistio NickSulistio commented Apr 6, 2021

Description

Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change.

Fixes # (issue)

Type of change

Please delete options that are not relevant.

  • 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

Tested using tox -e test-instrumentation-flask, wrote two new tests

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

@NickSulistio NickSulistio requested review from a team, ocelotl and lzchen and removed request for a team April 6, 2021 02:10
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Apr 6, 2021

CLA Signed

The committers are authorized under a signed CLA.

@lzchen
Copy link
Contributor

lzchen commented Apr 6, 2021

@NickSulistio
I would mark this PR as a draft if it is not ready to review yet.

@NickSulistio NickSulistio marked this pull request as draft April 6, 2021 18:28
@NickSulistio NickSulistio changed the title added request and response hook - may be very buggy [FLASK] added request and response hook Apr 8, 2021
@NickSulistio NickSulistio marked this pull request as ready for review April 8, 2021 07:23

FlaskInstrumentor().instrument(otel_request_hook=request_hook, otel_response_hook=response_hook)
# pylint: disable=import-outside-toplevel,reimported,redefined-outer-name
from flask import Flask
Copy link
Contributor

Choose a reason for hiding this comment

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

isn't flask being imported globally above? why do we need this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey Owais, so if I don't import Flask here, no span will be created. I'm not sure exactly why this is the case but when I removed that import line, the span was no longer being created.

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 you'll be able to solve it by creating your flask app instance after calling instrument(). So for each test case, first call .instument() and then create flask app instance.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey Owais, if I'm not mistaken, aren't I already calling .instrument() before I import Flask and create the app instance?

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't see the app being created anywhere in this method. May be it is being created in the super-class in which case it would be created before since super().setUp() is being called before instrument.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey Owais, for this test, we're testing the hooks without the App being created. I'm not entirely sure how this is supposed to work, but previously there was a test for TestProgrammaticCustomSpanNameWithoutApp, which did the same. I believe that @lonewolf3739 may have a better understanding of what this is used for.

Copy link
Member

Choose a reason for hiding this comment

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

@NickSulistio Sorry for getting back late. I think If you do import from flask import Flask first and next call FlaskInstrumentor().instrument()then instrumentation doesn't happen because the way patching is done; I remember this was the reason but I may be wrong.

Copy link
Contributor

Choose a reason for hiding this comment

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

If this is a limitation with the instrumentation itself, I think we can live with it for now and try to improve the instrumentation later.

{"http.target": "/hello/123", "http.route": "/hello/<int:helloid>", "hook_attr":"hello world"}
)

self.client.get("/hello/123")
Copy link
Contributor

Choose a reason for hiding this comment

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

I would also test adding a header to the response and making sure it was seen 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.

If you don't mind, could you elaborate on how to do that?

Copy link
Contributor

Choose a reason for hiding this comment

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

You'd need to update the post response hook and inject headers into the response there. Look at Flask or WSGI docs (which ever object is passed to the response hook). Then you'd capture the result from client.get() call here and verify the result has the same headers you set in the hook earlier.

CHANGELOG.md Outdated Show resolved Hide resolved
@NickSulistio
Copy link
Contributor Author

@ocelotl Would you like to review this PR?

@lzchen
Copy link
Contributor

lzchen commented Apr 27, 2021

@owais
Have your concerns been addressed?

@owais
Copy link
Contributor

owais commented Apr 27, 2021

Couple of minor changes/additions suggested. Please also remove the commented out code.

Copy link
Contributor

@ocelotl ocelotl left a comment

Choose a reason for hiding this comment

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

Looks good 👍 Minor changes requested

NickSulistio and others added 2 commits May 27, 2021 13:12
…lemetry/instrumentation/flask/__init__.py

Co-authored-by: Diego Hurtado <ocelotl@users.noreply.github.com>
Co-authored-by: Diego Hurtado <ocelotl@users.noreply.github.com>
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.

6 participants