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

feat: Drop invalid attachments #1134

Merged
merged 6 commits into from
Dec 23, 2020
Merged

Conversation

philipphofmann
Copy link
Member

@philipphofmann philipphofmann commented Dec 22, 2020

📜 Description

SentryEnvelopeItem.fromAttachment throws now a SentryEnvelopeException when it fails
to create an envelope item from an attachment. The GsonSerializer logs the exception
and drops the envelope item. So we avoid ingesting empty attachments.

This should have been a draft PR. It is not completely finished yet.

💡 Motivation and Context

Fixes GH-1123

💚 How did you test it?

Unit tests and emulator

📝 Checklist

  • I reviewed the submitted code
  • I added tests to verify changes
  • All tests passing
  • No breaking changes

🔮 Next steps

SentryEnvelopeItem.fromAttachment throws now a SentryEnvelopeException when it fails
to create an envelope item from an attachment. The GsonSerializer logs the exception
and drops the envelope item. So we avoid ingesting empty attachments.

Fixes GH-1123
@codecov-io
Copy link

codecov-io commented Dec 22, 2020

Codecov Report

Merging #1134 (1e50ba7) into main (082b4b5) will increase coverage by 0.12%.
The diff coverage is 100.00%.

Impacted file tree graph

@@             Coverage Diff              @@
##               main    #1134      +/-   ##
============================================
+ Coverage     74.93%   75.06%   +0.12%     
- Complexity     1606     1612       +6     
============================================
  Files           165      167       +2     
  Lines          5593     5602       +9     
  Branches        547      545       -2     
============================================
+ Hits           4191     4205      +14     
+ Misses         1150     1146       -4     
+ Partials        252      251       -1     
Impacted Files Coverage Δ Complexity Δ
sentry/src/main/java/io/sentry/GsonSerializer.java 96.96% <100.00%> (+0.19%) 9.00 <0.00> (ø)
sentry/src/main/java/io/sentry/SentryClient.java 87.45% <100.00%> (-0.05%) 68.00 <0.00> (ø)
...ry/src/main/java/io/sentry/SentryEnvelopeItem.java 89.47% <100.00%> (-0.63%) 26.00 <0.00> (-3.00)
...a/io/sentry/exception/SentryEnvelopeException.java 100.00% <100.00%> (ø) 1.00 <1.00> (?)
sentry/src/main/java/io/sentry/protocol/User.java 100.00% <0.00%> (ø) 15.00% <0.00%> (+1.00%)
...g/boot/SentryLogbackAppenderAutoConfiguration.java 100.00% <0.00%> (ø) 2.00% <0.00%> (-6.00%)
...o/sentry/spring/boot/SentryLogbackInitializer.java 96.15% <0.00%> (ø) 10.00% <0.00%> (?%)
sentry/src/main/java/io/sentry/Scope.java 91.70% <0.00%> (+0.08%) 64.00% <0.00%> (+1.00%)
...in/java/io/sentry/SentryEnvelopeHeaderAdapter.java 79.06% <0.00%> (+3.48%) 18.00% <0.00%> (+1.00%)
... and 1 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 082b4b5...1e50ba7. Read the comment docs.

@philipphofmann philipphofmann added this to the 4.0.0 milestone Dec 22, 2020
@philipphofmann philipphofmann self-assigned this Dec 22, 2020
Copy link
Member

@bruno-garcia bruno-garcia left a comment

Choose a reason for hiding this comment

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

LGTM!

gson.toJson(item.getHeader(), SentryEnvelopeItemHeader.class, writer);
writer.write("\n");
writer.flush();
try {
Copy link
Member

Choose a reason for hiding this comment

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

It's open up for the case of sending envelopes without items but if the stream in question is the network stream, it would be too late to do anything about it anyway unless we supported some trailing header to communicate the failure

Copy link
Contributor

Choose a reason for hiding this comment

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

I think its already done but if not yet, it'd be nice to check if the envelope items are not empty before calling this method

Copy link
Member Author

Choose a reason for hiding this comment

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

@marandaneto what exactly do you mean by checking if the envelope items are not empty?

Copy link
Member Author

@philipphofmann philipphofmann Dec 23, 2020

Choose a reason for hiding this comment

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

Good point @bruno-garcia, but I think we already had the problem before. I think this change slightly improves it.

Given an envelope with two items and the first to send throws an exception.
Old
Send header
Send 1st item -> Exception
Stop

We just sent an envelope without items.

New
Send header
Send 1st item -> Exception, Drop the item
Send 2nd

We send the envelope but dropped the 1st item.

Given an envelope with one item that throws.
Old and New
Send header
Send 1st item -> Exception

We just sent an envelope without items.

For output streams that are all or nothing we just changed the behavior, because if there is one error in any of the items we previously discarded the whole envelope and now we keep the envelope. Therefore we keep an envelope without any items. I think we could solve this by throwing an exception when we serialize an envelope and we can't serialize a single item, but according to our docs an envelope with just a header, such as {"event_id":"12c2d058d58442709aa2eca08bf20986"} is still a valid envelope: https://develop.sentry.dev/sdk/envelopes/#envelopes. Valid but useless.

Copy link
Contributor

Choose a reason for hiding this comment

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

@marandaneto what exactly do you mean by checking if the envelope items are not empty?

I just checked up and we don't do it:

so here:
https://github.com/getsentry/sentry-java/blob/main/sentry/src/main/java/io/sentry/transport/AsyncConnection.java#L156

we could do:

if (!envelope.getItems().isEmpty()) {
      executor.submit(new EnvelopeSender(envelope, hint, currentEnvelopeCache));
}

ps: getItems is an iterator so I don't think isEmpty would work, just a pseudocode

so we'd never try to send an envelope without items.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think we already check that in

, so I think it is not needed.

Copy link
Contributor

Choose a reason for hiding this comment

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

@philipphofmann this condition is only met if at least 1 item of the envelope is rate limited

Copy link
Contributor

Choose a reason for hiding this comment

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

it's not related to this PR though, and the backend accepts an empty envelope, so feel free to ignore it, I just wrote it because I thought that the backend would trigger an error, it doesn't, as said by Armin.

Comment on lines 38 to 45
private lateinit var logger: ILogger
private lateinit var serializer: GsonSerializer

@BeforeTest
fun before() {
logger = mock()
serializer = GsonSerializer(logger, EnvelopeReader())
}
Copy link
Contributor

Choose a reason for hiding this comment

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

M: what's about refactoring this and adding the Fixture?

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.

Log errors while reading attachment
4 participants