open-telemetry / open-telemetry/opentelemetry-python-contrib

requests package treats >= 300 status code as an error

Open
#2,165 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
1.1k
Forks
1.1k
Avg merge
4d 15h
Merged PRs (30d)
16

Description

Describe your environment
python 3.9
opentelemetry-api==1.22.0
opentelemetry-instrumentation-requests==0.43b0

Steps to reproduce
Make an http call with requests and get back any http status code >= 300

What is the expected behavior?
This could very much be a misunderstanding on my part! When making an http call with requests, I would expect only >= 500 status codes to be treated as exceptions on traces but right now all codes above 299 are treated as exceptions. I didn't expect redirects or 400s to be logged as errors on my traces.

What is the actual behavior?
In the requests library, when it creates a span it sets the type to be client (https://github.com/open-telemetry/opentelemetry-python-contrib/blob/8fd2105ceae604cf39a67f1c4dd154753b43fcd1/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py#L208-L210). When it converts this to an otel status code, it uses:

def http_status_to_status_code(
    status: int,
    allow_redirect: bool = True,
    server_span: bool = False,
) -> StatusCode:
    """Converts an HTTP status code to an OpenTelemetry canonical status code

    Args:
        status (int): HTTP status code
    """
    # See: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/http.md#status
    if not isinstance(status, int):
        return StatusCode.UNSET

    if status < 100:
        return StatusCode.ERROR
    if status <= 299:
        return StatusCode.UNSET
    if status <= 399 and allow_redirect:
        return StatusCode.UNSET
    if status <= 499 and server_span:
        return StatusCode.UNSET
    return StatusCode.ERROR

and doesn't pass in a server_span so all non 2** status codes are treated as errors.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/init.py at the span creation around lines 208-210, then trace how http_status_to_status_code is called. Verify the expected status handling for redirects and 4xx responses, with completion defined as those responses no longer being recorded as trace errors while 5xx responses remain errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
observability-sre
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.