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 Litestar example to documentation #284

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

Conversation

myslak71
Copy link

@myslak71 myslak71 commented Dec 12, 2023

Describes the process of integrating pyinstrument into Litestar middleware

Copy link
Owner

@joerick joerick left a comment

Choose a reason for hiding this comment

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

Thanks!

docs/guide.md Outdated
Comment on lines 288 to 303
profiler.start()

async def send_wrapper(message: Message) -> None:
if message["type"] == "http.response.body":
message["body"] = profiler.output_html().encode()

elif message["type"] == "http.response.start":
profiler.stop()
message["headers"] = [
(b"content-type", b"text/html; charset=utf-8"),
(b"content-length", str(len(profiler.output_html())).encode()),
]

await send(message)

await self.app(scope, receive, send_wrapper)
Copy link
Owner

Choose a reason for hiding this comment

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

It seems maybe a bit wasteful to render the session twice. Does this work?

Suggested change
profiler.start()
async def send_wrapper(message: Message) -> None:
if message["type"] == "http.response.body":
message["body"] = profiler.output_html().encode()
elif message["type"] == "http.response.start":
profiler.stop()
message["headers"] = [
(b"content-type", b"text/html; charset=utf-8"),
(b"content-length", str(len(profiler.output_html())).encode()),
]
await send(message)
await self.app(scope, receive, send_wrapper)
profiler.start()
profile_html: str | None = None
async def send_wrapper(message: Message) -> None:
if message["type"] == "http.response.start":
profiler.stop()
nonlocal profile_html
profile_html = profiler.output_html()
message["headers"] = [
(b"content-type", b"text/html; charset=utf-8"),
(b"content-length", str(len(profile_html)).encode()),
]
elif message["type"] == "http.response.body":
message["body"] = profile_html.encode()
await send(message)
await self.app(scope, receive, send_wrapper)

Also I flipped the order around in the if statement to match the order it (presumably) executes in.

Copy link
Author

Choose a reason for hiding this comment

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

of course!

@myslak71
Copy link
Author

@joerick fixed but seems that sphinx need version bump or smth

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.

None yet

2 participants