Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(envelope) Add support for implicitly sized envelope items #1229

Merged
merged 5 commits into from Nov 3, 2021

Conversation

RaduW
Copy link
Contributor

@RaduW RaduW commented Oct 27, 2021

This PR adds support for implicitly sized envelope items, that is envelope items that do not contain the size in the
item header and the payload is inferred by the first \n character.

The support for implicitly sized envelopes is added so that the parsing of envelopes is compliant with the Envelope specification. Envelope Items

Additionally this PR adds the type metric_buckets as a recognised JSON item type ( it contains metrics).

@RaduW RaduW requested a review from untitaker October 27, 2021 14:12
@RaduW RaduW force-pushed the fix/implicit-size-envelope-items branch from 21dbd24 to fa9e2c1 Compare October 27, 2021 14:14
@RaduW RaduW requested a review from a team October 27, 2021 14:17
Copy link
Contributor

@rhcarvalho rhcarvalho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RaduW would you mind explaining in the description the motivation for this feature? The "what" is clear, but not the "why" we should add this feature. I know it is (an optional) part of the protocol, but why are we adding it to the Python SDK now?

else:
# if no length was specified we need to read up to the end of line
payload = f.readline().rstrip()
if headers.get("type") in ("event", "transaction", "metric_buckets"):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"metric_buckets" is this addition intentional? If so, please let's document it at least in the PR description as it seems unrelated to the title.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, sorry sneaked it in since I bumped into the problem while testing metrics.
"metric_buckets" is the new item type that contains metrics... will detail in description.

f.readline()
else:
# if no length was specified we need to read up to the end of line
payload = f.readline().rstrip()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aware that we didn't check the value of length before (we probably should), I think here we should be more defensive against reading too much data into memory. There should be an upper limit, because we know that above that envelopes won't make it through and can potentially bring apps down.

payload = f.read(length)
if headers.get("type") in ("event", "transaction"):
rv = cls(headers=headers, payload=PayloadRef(json=parse_json(payload)))
headers = json.loads(line)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parse_json seems to be a Python 2/3 compatibility helper, why replace its use with json.loads?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, don't even remember doing it (I guess, just from habit), will revert to parse_json.

'{"event_id":"9ec79c33ec9942ab8353589fcb2e04dc"}\n'
+ '{"type":"type1"}\n1234\n'
+ '{"type":"type2"}\nabcd\n'
+ '{"type":"type3"}\n\n'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we also add a case for when a \n is omitted as in "Envelope with 2 empty attachments, last newline omitted" in https://develop.sentry.dev/sdk/envelopes/

image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can do this, but this should be added to the explicitly specified length, test.
If we do aggre to support implicitly specified lengths I will extend the tests to cover all the cases I can think of.

@RaduW RaduW merged commit 49cae60 into master Nov 3, 2021
@RaduW RaduW deleted the fix/implicit-size-envelope-items branch November 3, 2021 10:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants