---
title: "Instrument HTTP Requests"
description: "Learn how to manually instrument your code to use Sentry's Requests module."
url: https://docs.sentry.io/platforms/python/tracing/instrumentation/custom-instrumentation/requests-module/
---

# Instrument HTTP Requests for Python

As a prerequisite to setting up [Requests](https://docs.sentry.io/product/dashboards/sentry-dashboards/outbound-api-requests.md), you’ll need to first [set up tracing](https://docs.sentry.io/platforms/python/tracing.md). 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](https://sentry.io/orgredirect/organizations/:orgslug/dashboards/). If that doesn't fit your use case, you can set up using custom instrumentation described below.

The examples on this page are for SDK `3.x`. The **SDK 2.x** tabs show the equivalent code for the default transaction mode of SDK `2.x`. If you use [stream mode](https://docs.sentry.io/platforms/python/tracing/streamed-spans.md) in SDK `2.x`, follow the **SDK 3.x** examples, but call the tracing functions on `sentry_sdk.traces` (for example, `sentry_sdk.traces.start_span()`) and set `trace_lifecycle="stream"` in `sentry_sdk.init()`.

## [Custom Instrumentation](https://docs.sentry.io/platforms/python/tracing/instrumentation/custom-instrumentation/requests-module.md#custom-instrumentation)

For detailed information about which data can be set, see the [Requests Module developer specifications](https://develop.sentry.dev/sdk/performance/modules/requests/).

### [Wrap HTTP Requests in a Span](https://docs.sentry.io/platforms/python/tracing/instrumentation/custom-instrumentation/requests-module.md#wrap-http-requests-in-a-span)

NOTE: Refer to [HTTP Span Data Conventions](https://develop.sentry.dev/sdk/performance/span-data-conventions/#http) for a full list of the span data attributes.

Here is an example of an instrumented function that makes HTTP requests:

```python
from urllib.parse import urlparse
import requests
import sentry_sdk

def make_request(method, url):
    span = sentry_sdk.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.end()
    return response
```

*Other available variations of the above snippet: SDK 2.x*

---

Find other pages: [Sitemap](https://docs.sentry.io/sitemap.md) lists every page with a one-line summary, and [llms.txt](https://docs.sentry.io/llms.txt) gives an overview with guidance for agents.
