Skip to content

Commit

Permalink
fixed tracer flare send error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
erikayasuda committed May 2, 2024
1 parent 8b4d202 commit 64f48ef
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions ddtrace/internal/flare/flare.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ class FlareSendRequest:
source: str = "tracer_python"


class TracerFlareSendError(Exception):
def __init__(self, message: str):
super().__init__(message)
self.message = message


class Flare:
def __init__(
self,
Expand Down Expand Up @@ -105,27 +111,24 @@ def send(self, flare_send_req: FlareSendRequest):
try:
client = get_connection(self.url, timeout=self.timeout)
headers, body = self._generate_payload(flare_send_req.__dict__)
print("headers: %s", headers)
client.request("POST", TRACER_FLARE_ENDPOINT, body, headers)
response = client.getresponse()
if response.status == 200:
log.info("Successfully sent the flare to Zendesk ticket %s", flare_send_req.case_id)
else:
log.error(
"Tracer flare upload to Zendesk ticket %s failed with %s status code:(%s) %s",
flare_send_req.case_id,
msg = "Tracer flare upload responded with status code %s:(%s) %s" % (
response.status,
response.reason,
response.read().decode(),
)
raise TracerFlareSendError(msg)
except Exception as e:
log.error("Failed to send tracer flare to Zendesk ticket %s: %s", flare_send_req.case_id, e)
raise e
finally:
client.close()
# Clean up files regardless of success/failure
self.clean_up_files()
return

def _generate_config_file(self, config: dict, pid: int):
config_file = self.flare_dir / f"tracer_config_{pid}.json"
Expand Down Expand Up @@ -166,16 +169,15 @@ def _generate_payload(self, params: Dict[str, str]) -> Tuple[dict, bytes]:
boundary = binascii.hexlify(os.urandom(16))
body = io.BytesIO()
for key, value in params.items():
print(key, value)
encoded_key = key.encode()
encoded_value = value.encode()
body.write(b"--" + boundary + newline)
body.write(b'Content-Disposition: form-data; name="{%s}"{%s}{%s}' % (encoded_key, newline, newline))
body.write(b"{%s}{%s}" % (encoded_value, newline))
body.write(b'Content-Disposition: form-data; name="%s"%s%s' % (encoded_key, newline, newline))
body.write(b"%s%s" % (encoded_value, newline))

body.write(b"--" + boundary + newline)
body.write((b'Content-Disposition: form-data; name="flare_file"; filename="flare.tar"{%s}' % newline))
body.write(b"Content-Type: application/octet-stream{%s}{%s}" % (newline, newline))
body.write((b'Content-Disposition: form-data; name="flare_file"; filename="flare.tar"%s' % newline))
body.write(b"Content-Type: application/octet-stream%s%s" % (newline, newline))
body.write(tar_stream.getvalue() + newline)
body.write(b"--" + boundary + b"--")
headers = {
Expand Down

0 comments on commit 64f48ef

Please sign in to comment.