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 context to trace functions #932

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Conversation

karpetrosyan
Copy link
Member

This PR adds context to the traces, giving us more control over them and providing context for particular events. It allows us to store variables specifically for that event and access them through the started, complete, or failed stages.

Originally, I tried to add metrics for the httpx client to measure how much time the SSL handshake took. I thought it would be amazing to have some context for the trace functions.

Here is the example of usecase, from the documentation:

import httpcore
import time


def log(event_name, info, context):
    _, event_name, stage = event_name.split(".")
    if event_name == "start_tls":
        if stage == "started":
            context["start_time"] = time.monotonic()
        elif stage == "complete":
            elapsed = time.monotonic() - context["start_time"]
            print(f"TLS handshake took {elapsed:.2f} seconds")


r = httpcore.request("GET", "https://www.encode.io/", extensions={"trace": log})

@karpetrosyan karpetrosyan added the enhancement New feature or request label Jun 23, 2024
@karpetrosyan karpetrosyan requested a review from a team June 23, 2024 10:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant