Skip to content

【opentelemetry-util-http】【detect_synthetic_user_agent】not supports bytes-like headers #4001

@eriendeng

Description

@eriendeng

Describe your environment

OS: CentOS
Python version: 3.11
Package version: 0.60b0

What happened?

I'm requesting a S3-like cloud server(qcloud_cos), and when after i setup [opentelemetry-instrumentation-requests], requests went wrong and i got this exception


  | Traceback (most recent call last):
-- | --
  | File "/usr/local/lib/python3.11/site-packages/qcloud_cos/cos_client.py", line 427, in send_request
  | res = self._session.head(url, timeout=timeout, proxies=self._conf._proxies, **kwargs)
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  | File "/usr/local/lib/python3.11/site-packages/requests/sessions.py", line 624, in head
  | return self.request("HEAD", url, **kwargs)
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  | File "/usr/local/lib/python3.11/site-packages/requests/sessions.py", line 589, in request
  | resp = self.send(prep, **send_kwargs)
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  | File "/usr/local/lib/python3.11/site-packages/opentelemetry/instrumentation/requests/__init__.py", line 256, in instrumented_send
  | synthetic_type = detect_synthetic_user_agent(user_agent)
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  | File "/usr/local/lib/python3.11/site-packages/opentelemetry/util/http/__init__.py", line 329, in detect_synthetic_user_agent
  | if any(test_pattern in user_agent_lower for test_pattern in TEST_PATTERNS):
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  | File "/usr/local/lib/python3.11/site-packages/opentelemetry/util/http/__init__.py", line 329, in <genexpr>
  | if any(test_pattern in user_agent_lower for test_pattern in TEST_PATTERNS):
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  | TypeError: a bytes-like object is required, not 'str'
  | 


Notice that, a S3-like client sdk would format header into bytes before calling requests, and opentelemetry-util-http add user-agent detection in version 0.60b0(which never happened <=0.59b0)

Steps to Reproduce

from opentelemetry.instrumentation.requests import RequestsInstrumentor
RequestsInstrumentor().instrument()

from qcloud_cos import CosConfig, CosS3Client
config = CosConfig(SecretId=ak, SecretKey=sk, Endpoint=endpoint, Scheme='http', KeepAlive=False)
cos_client = CosS3Client(config)
cos_client.head_object(bucket, key)

Expected Result

i expect the code run normally and would not raise and Exception

Actual Result

Exception thrown due to incompatible of bytes-like headers

Additional context

No response

Would you like to implement a fix?

None

Tip

React with 👍 to help prioritize this issue. Please use comments to provide useful context, avoiding +1 or me too, to help us triage it. Learn more here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions