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

fix(opentelemetry-instrumentation-asgi): Correct http.url attribute generation #2477

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

dhofstetter
Copy link

@dhofstetter dhofstetter commented Apr 29, 2024

Description

Correct http.url and http.target attribute generation even with sub apps (fixes #2476)

  • modify the instrumentation logic
  • add unittests on starlette instrumentation
  • add unittests on fastapi instrumentation

Fixes #2476

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)

From my point of view existing logic won't break. Simply attributes generated will change according to their correct intended values.

How Has This Been Tested?

Does This PR Require a Core Repo Change?

  • 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 (imho not required)

@dhofstetter dhofstetter requested a review from a team as a code owner April 29, 2024 21:49
@dhofstetter dhofstetter force-pushed the fix/asgi-sub-apps branch 3 times, most recently from 4271c3e to ffe44f8 Compare April 29, 2024 22:11
@dhofstetter
Copy link
Author

pylint forced me to split the fastapi instrumentation tests, therefore I made a split into two files

@dhofstetter
Copy link
Author

pipeline is already running 100% green within my fork, so its safe to approve workflows

@xrmx
Copy link
Contributor

xrmx commented Apr 30, 2024

@samuelcolvin any chance you could take a look at this?

@Kludex
Copy link

Kludex commented May 3, 2024

I'll check this today.

@xrmx
Copy link
Contributor

xrmx commented May 15, 2024

@Kludex it would be great if you can take a look at this, thanks

Copy link

@Kludex Kludex left a comment

Choose a reason for hiding this comment

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

I don't think that amount of explanation is needed, besides a link to the docs, or issue on asgiref, but besides that... All good. 👍

…e generation even with sub apps (fixes open-telemetry#2476)

- modify the instrumentation logic
- add unittests on starlette instrumentation
- add unittests on fastapi instrumentation
@dhofstetter
Copy link
Author

I don't think that amount of explanation is needed, besides a link to the docs, or issue on asgiref, but besides that... All good. 👍

Changed the comments to prevent non required verbosity :)

@dhofstetter
Copy link
Author

Refactored as suggested and rebased to main

@@ -485,6 +580,58 @@ def tearDown(self):
self._instrumentor.uninstrument()
super().tearDown()

def test_sub_app_fastapi_call(self):
Copy link
Contributor

@xrmx xrmx May 28, 2024

Choose a reason for hiding this comment

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

If this is the very same code of the TestAutoInstrumentation method we should probably rethink the inheritance of these tests cases

Copy link
Contributor

@xrmx xrmx May 28, 2024

Choose a reason for hiding this comment

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

something like this unless we are supposed to rerun the same tests many times:

diff --git a/instrumentation/opentelemetry-instrumentation-fastapi/tests/test_fastapi_instrumentation.py b/instrumentation/opentelemetry-instrumentation-fastapi/tests/test_fastapi_instrumentation.py
index 4269dfa2..9c396c66 100644
--- a/instrumentation/opentelemetry-instrumentation-fastapi/tests/test_fastapi_instrumentation.py
+++ b/instrumentation/opentelemetry-instrumentation-fastapi/tests/test_fastapi_instrumentation.py
@@ -61,7 +61,7 @@ _recommended_attrs = {
 }
 
 
-class TestFastAPIManualInstrumentation(TestBase):
+class TestBaseFastAPI:
     def _create_app(self):
         app = self._create_fastapi_app()
         self._instrumentor.instrument_app(
@@ -109,6 +109,30 @@ class TestFastAPIManualInstrumentation(TestBase):
             self._instrumentor.uninstrument()
             self._instrumentor.uninstrument_app(self._app)
 
+    @staticmethod
+    def _create_fastapi_app():
+        app = fastapi.FastAPI()
+
+        @app.get("/foobar")
+        async def _():
+            return {"message": "hello world"}
+
+        @app.get("/user/{username}")
+        async def _(username: str):
+            return {"message": username}
+
+        @app.get("/exclude/{param}")
+        async def _(param: str):
+            return {"message": param}
+
+        @app.get("/healthzz")
+        async def _():
+            return {"message": "ok"}
+
+        return app
+
+
+class TestFastAPIManualInstrumentation(TestBaseFastAPI, TestBase):
     def test_instrument_app_with_instrument(self):
         if not isinstance(self, TestAutoInstrumentation):
             self._instrumentor.instrument()
@@ -310,30 +334,8 @@ class TestFastAPIManualInstrumentation(TestBase):
                 if isinstance(point, NumberDataPoint):
                     self.assertEqual(point.value, 0)
 
-    @staticmethod
-    def _create_fastapi_app():
-        app = fastapi.FastAPI()
-
-        @app.get("/foobar")
-        async def _():
-            return {"message": "hello world"}
-
-        @app.get("/user/{username}")
-        async def _(username: str):
-            return {"message": username}
-
-        @app.get("/exclude/{param}")
-        async def _(param: str):
-            return {"message": param}
-
-        @app.get("/healthzz")
-        async def _():
-            return {"message": "ok"}
-
-        return app
-
 
-class TestFastAPIManualInstrumentationHooks(TestFastAPIManualInstrumentation):
+class TestFastAPIManualInstrumentationHooks(TestBaseFastAPI, TestBase):
     _server_request_hook = None
     _client_request_hook = None
     _client_response_hook = None
@@ -383,7 +385,7 @@ class TestFastAPIManualInstrumentationHooks(TestFastAPIManualInstrumentation):
             )
 
 
-class TestAutoInstrumentation(TestFastAPIManualInstrumentation):
+class TestAutoInstrumentation(TestBaseFastAPI, TestBase):
     """Test the auto-instrumented variant
 
     Extending the manual instrumentation as most test cases apply
@@ -445,7 +447,7 @@ class TestAutoInstrumentation(TestFastAPIManualInstrumentation):
         super().tearDown()
 
 
-class TestAutoInstrumentationHooks(TestFastAPIManualInstrumentationHooks):
+class TestAutoInstrumentationHooks(TestBaseFastAPI, TestBase):
     """
     Test the auto-instrumented variant for request and response hooks
 

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.

opentelemetry-instrumentation-asgi: root_path is handled not compliant to asgi spec
3 participants