-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Starlette Instrumentation Updates (#254)
* Fix starlette background task bug Co-authored-by: Uma Annamalai <[email protected]> * Formatting Co-authored-by: Uma Annamalai <[email protected]>
- Loading branch information
1 parent
b891adc
commit 208344e
Showing
7 changed files
with
365 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# Copyright 2010 New Relic, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from starlette.applications import Starlette | ||
from starlette.background import BackgroundTasks | ||
from starlette.middleware.base import BaseHTTPMiddleware | ||
from starlette.responses import PlainTextResponse | ||
from starlette.routing import Route | ||
from testing_support.asgi_testing import AsgiTest | ||
|
||
|
||
class ASGIStyleMiddleware: | ||
def __init__(self, app): | ||
self.app = app | ||
|
||
async def __call__(self, scope, receive, send): | ||
response = await self.app(scope, receive, send) | ||
return response | ||
|
||
|
||
class BaseHTTPStyleMiddleware(BaseHTTPMiddleware): | ||
async def dispatch(self, request, call_next): | ||
# simple middleware that does absolutely nothing | ||
response = await call_next(request) | ||
return response | ||
|
||
|
||
async def run_async_bg_task(request): | ||
tasks = BackgroundTasks() | ||
tasks.add_task(async_bg_task) | ||
return PlainTextResponse("Hello, world!", background=tasks) | ||
|
||
|
||
async def run_sync_bg_task(request): | ||
tasks = BackgroundTasks() | ||
tasks.add_task(sync_bg_task) | ||
return PlainTextResponse("Hello, world!", background=tasks) | ||
|
||
|
||
async def async_bg_task(): | ||
pass | ||
|
||
|
||
async def sync_bg_task(): | ||
pass | ||
|
||
|
||
routes = [ | ||
Route("/async", run_async_bg_task), | ||
Route("/sync", run_sync_bg_task), | ||
] | ||
|
||
# Generating target applications | ||
target_application = {} | ||
|
||
app = Starlette(routes=routes) | ||
app.add_middleware(ASGIStyleMiddleware) | ||
target_application["asgi"] = AsgiTest(app) | ||
|
||
app = Starlette(routes=routes) | ||
app.add_middleware(BaseHTTPStyleMiddleware) | ||
target_application["basehttp"] = AsgiTest(app) | ||
|
||
app = Starlette(routes=routes) | ||
target_application["none"] = AsgiTest(app) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.