Skip to content

Commit 8d216b6

Browse files
committed
feat(profiles): accept compressed data
1 parent ee2a56f commit 8d216b6

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

src/sentry/profiles/task.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from __future__ import annotations
22

33
import io
4+
import logging
5+
import zlib
46
from base64 import b64decode, b64encode
57
from copy import deepcopy
68
from datetime import datetime, timezone
@@ -86,8 +88,29 @@ def _get_profiles_producer_from_topic(topic: Topic) -> KafkaProducer:
8688
max_futures=settings.SENTRY_PROFILE_CHUNKS_FUTURES_MAX_LIMIT,
8789
)
8890

91+
logger = logging.getLogger(__name__)
8992

90-
def decode_payload(encoded: str) -> dict[str, Any]:
93+
94+
def decode_payload(encoded: str | bytes) -> dict[str, Any]:
95+
if isinstance(encoded, bytes):
96+
return msgpack.unpackb(encoded, use_list=False)
97+
98+
# It's been b64encoded for taskworker
99+
100+
# compressed
101+
if encoded.startswith("!"):
102+
try:
103+
res = msgpack.unpackb(
104+
zlib.decompress(b64decode(encoded[1:].encode("utf-8"))), use_list=False
105+
)
106+
metrics.incr("profiling.profile_metrics.decompress", tags={"status": "ok"})
107+
return res
108+
except Exception as e:
109+
logger.exception("Failed to decompress compressed profile", extra={"error": e})
110+
metrics.incr("profiling.profile_metrics.decompress", tags={"status": "err"})
111+
raise
112+
113+
# not compressed
91114
return msgpack.unpackb(b64decode(encoded.encode("utf-8")), use_list=False)
92115

93116

@@ -125,10 +148,7 @@ def process_profile_task(
125148
return
126149

127150
if payload:
128-
if isinstance(payload, str): # It's been b64encoded for taskworker
129-
message_dict = decode_payload(payload)
130-
else:
131-
message_dict = msgpack.unpackb(payload, use_list=False)
151+
message_dict = decode_payload(payload)
132152

133153
profile = json.loads(message_dict["payload"], use_rapid_json=True)
134154

0 commit comments

Comments
 (0)