Repository: getsentry/sentry-python

Issue:
quote_from_bytes() expected bytes
### How do you use Sentry?

Self-hosted/on-premise

### Version

1.9.8

### Steps to Reproduce

None, all API requests fail to this

### Expected Result

It is OK to log the error about sentry-tracing not being able to do its magic, but it should not make the full application error out

### Actual Result

```python
Traceback (most recent call last):
  File "/var/task/botocore/httpsession.py", line 448, in send
    urllib_response = conn.urlopen(
  File "/var/task/urllib3/connectionpool.py", line 703, in urlopen
    httplib_response = self._make_request(
  File "/var/task/urllib3/connectionpool.py", line 398, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/var/task/urllib3/connection.py", line 239, in request
    super(HTTPConnection, self).request(method, url, body=body, headers=headers)
  File "/usr/local/python/lib/python3.9/http/client.py", line 1285, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/var/task/botocore/awsrequest.py", line 94, in _send_request
    rval = super()._send_request(
  File "/usr/local/python/lib/python3.9/http/client.py", line 1296, in _send_request
    self.putrequest(method, url, **skips)
  File "/var/task/urllib3/connection.py", line 219, in putrequest
    return _HTTPConnection.putrequest(self, method, url, *args, **kwargs)
  File "/var/task/sentry_sdk/integrations/stdlib.py", line 88, in putrequest
    for key, value in hub.iter_trace_propagation_headers(span):
  File "/var/task/sentry_sdk/hub.py", line 717, in iter_trace_propagation_headers
    for header in span.iter_headers():
  File "/var/task/sentry_sdk/tracing.py", line 322, in iter_headers
    baggage = self.containing_transaction.get_baggage().serialize()
  File "/var/task/sentry_sdk/tracing_utils.py", line 541, in serialize
    item = Baggage.SENTRY_PREFIX + quote(key) + "=" + quote(val)
  File "/usr/local/python/lib/python3.9/urllib/parse.py", line 871, in quote
    return quote_from_bytes(string, safe)
  File "/usr/local/python/lib/python3.9/urllib/parse.py", line 896, in quote_from_bytes
    raise TypeError("quote_from_bytes() expected bytes")
```


Discussion:
This issue was first introduced when I upgraded from 1.9.5 -> 1.9.8 and whole production went down. I would like to see some try: except since this is a monitoring tool that should not cause whole production to go down.
@lehmat thx for reporting. Can you give me your sentry init options? Just trying to see why this could happen in the first place.
```
sentry_sdk.init(
    dsn=self.app.config["SENTRY_URL"],
    integrations=[
        FlaskIntegration(transaction_style="url"),
    ],
    release=self.app.config["SENTRY_RELEASE"],
    send_default_pii=True,
    traces_sample_rate=0.5,
)
```

And we are running on-premise sentry with release version: Sentry 22.9.0.dev0

Resolving patch:
diff --git a/sentry_sdk/tracing_utils.py b/sentry_sdk/tracing_utils.py
--- a/sentry_sdk/tracing_utils.py
+++ b/sentry_sdk/tracing_utils.py
@@ -459,14 +459,16 @@ def from_incoming_header(cls, header):
             for item in header.split(","):
                 if "=" not in item:
                     continue
-                item = item.strip()
-                key, val = item.split("=")
-                if Baggage.SENTRY_PREFIX_REGEX.match(key):
-                    baggage_key = unquote(key.split("-")[1])
-                    sentry_items[baggage_key] = unquote(val)
-                    mutable = False
-                else:
-                    third_party_items += ("," if third_party_items else "") + item
+
+                with capture_internal_exceptions():
+                    item = item.strip()
+                    key, val = item.split("=")
+                    if Baggage.SENTRY_PREFIX_REGEX.match(key):
+                        baggage_key = unquote(key.split("-")[1])
+                        sentry_items[baggage_key] = unquote(val)
+                        mutable = False
+                    else:
+                        third_party_items += ("," if third_party_items else "") + item
 
         return Baggage(sentry_items, third_party_items, mutable)
 
@@ -538,8 +540,9 @@ def serialize(self, include_third_party=False):
         items = []
 
         for key, val in iteritems(self.sentry_items):
-            item = Baggage.SENTRY_PREFIX + quote(key) + "=" + quote(val)
-            items.append(item)
+            with capture_internal_exceptions():
+                item = Baggage.SENTRY_PREFIX + quote(key) + "=" + quote(str(val))
+                items.append(item)
 
         if include_third_party:
             items.append(self.third_party_items)


Test patch: