Instrument HTTP Requests
Learn how to manually instrument your code to use Sentry's Requests module.
As a prerequisite to setting up Requests, you’ll need to first set up tracing. Once this is done, the Python SDK will automatically instrument outgoing HTTP requests made via HTTPConnection and show the data in the requests-monitoring dashboard. If that doesn't fit your use case, you can set up using custom instrumentation described below.
This page covers both transaction mode (default) and stream mode. See Streamed Spans to learn more.
For detailed information about which data can be set, see the Requests Module developer specifications.
NOTE: Refer to HTTP Span Data Conventions for a full list of the span data attributes.
Here is an example of an instrumented function that makes HTTP requests:
Copied
from urllib.parse import urlparse
import requests
import sentry_sdk
def make_request(method, url):
span = sentry_sdk.start_span(
op="http.client",
name=f"{method} {url}",
)
span.set_data("http.request.method", method)
parsed_url = urlparse(url)
span.set_data("url", url)
span.set_data("server.address", parsed_url.hostname)
span.set_data("server.port", parsed_url.port)
response = requests.request(method=method, url=url)
span.set_data("http.response.status_code", response.status_code)
span.set_data("http.response_content_length", response.headers.get("content-length"))
span.finish()
return response
from urllib.parse import urlparse
import requests
import sentry_sdk
def make_request(method, url):
span = sentry_sdk.start_span(
op="http.client",
name=f"{method} {url}",
)
span.set_data("http.request.method", method)
parsed_url = urlparse(url)
span.set_data("url", url)
span.set_data("server.address", parsed_url.hostname)
span.set_data("server.port", parsed_url.port)
response = requests.request(method=method, url=url)
span.set_data("http.response.status_code", response.status_code)
span.set_data("http.response_content_length", response.headers.get("content-length"))
span.finish()
return response
from urllib.parse import urlparse
import requests
import sentry_sdk
def make_request(method, url):
span = sentry_sdk.traces.start_span(
name=f"{method} {url}",
attributes={"sentry.op": "http.client"},
)
parsed_url = urlparse(url)
span.set_attributes({
"http.request.method": method,
"url": url,
"server.address": parsed_url.hostname,
"server.port": parsed_url.port,
})
response = requests.request(method=method, url=url)
span.set_attribute("http.response.status_code", response.status_code)
content_length = response.headers.get("content-length")
if content_length is not None:
span.set_attribute("http.response.header.content-length", content_length)
span.finish()
return response
Was this helpful?
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").