Skip to content

Attachments can be manipulated via hints #2046

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

Conversation

adinauer
Copy link
Member

📜 Description

Attachments can now be manipulated via hints

💡 Motivation and Context

This builds on top of #2045 to enable use cases mentioned in getsentry/sentry-javascript#5036

Closes #1467

💚 How did you test it?

Unit Tests

📝 Checklist

  • I reviewed the submitted code
  • I added tests to verify the changes
  • I updated the docs if needed
  • No breaking changes

🔮 Next steps

Update docs

@adinauer adinauer changed the base branch from main to feat/replace-map-with-hints-object May 13, 2022 06:07
@@ -80,6 +80,11 @@ private boolean shouldApplyScopeData(
hints = new Hints();
}

// TODO what does cached etc mean for devs manipulating hints in beforeSend and eventProcessor?
if (shouldApplyScopeData(event, hints)) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Do we have to prevent manipulation of attachments if !shouldApplyScopeData?

Copy link
Member

Choose a reason for hiding this comment

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

What you did makes sense to only get the attachments from the scope into the hints with this condition.

If we're not applying the scope to the event/hint, an attachment still could show up in the hint because the user added it directly while calling capture. And that's totally fine.

}

@Test
fun `passing attachments via hint into breadcrumb ignores them`() {
Copy link
Member Author

Choose a reason for hiding this comment

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

This test case and the one after it show that attachments in hints have no effect on breadcrumbs. I assume this is what we want. Correct?

Copy link
Member

Choose a reason for hiding this comment

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

h: Why do we have a hint in setBeforeBreadcrumb? I don't get this. This might be frankly confusing. You could do addAttachment in setBeforeBreadcrumb, and nothing would happen.

Copy link
Member

Choose a reason for hiding this comment

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

@marandaneto to answer both I guess 😄

Copy link
Contributor

Choose a reason for hiding this comment

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

The hint for crumbs are only for filtering, they are not part of the event processing pipeline, I'd say that we don't even need this test.

Copy link
Member Author

Choose a reason for hiding this comment

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

Is there anything we need to change here? Like passing a different Interface that doesn't have the attachment manipulation methods instead of Hints? Or is it ok if these are basically NOOP for breadcrumbs?

@adinauer
Copy link
Member Author

@mattjohnsonpint @timfish fyi

Copy link
Member

@philipphofmann philipphofmann left a comment

Choose a reason for hiding this comment

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

I like where this is going, but I have a few major items to discuss about the API.

Comment on lines 16 to 26
public static @NotNull Hints withAttachment(@Nullable Attachment attachment) {
@NotNull final Hints hints = new Hints();
hints.getAttachmentContainer().add(attachment);
return hints;
}

public static @NotNull Hints withAttachments(@Nullable List<Attachment> attachments) {
@NotNull final Hints hints = new Hints();
hints.getAttachmentContainer().addAll(attachments);
return hints;
}
Copy link
Member

Choose a reason for hiding this comment

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

m: I'm not sure if we really need to add these convenience methods. The user saves one line of code and we have to add two methods for that. I would rather keep it simple.

Copy link
Member Author

Choose a reason for hiding this comment

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

My intention was to make it as easy as possible to pass an attachment to the captureX() methods.

Co-authored-by: Philipp Hofmann <[email protected]>
@adinauer adinauer changed the title Attachments can now be manipulated via hints Attachments can be manipulated via hints May 13, 2022
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.

The indirection to access the attachments from the Hint IMHO could be dropped in favor of methods hanging straight from the Hints class.

Other than that, LGTM

@@ -80,6 +80,11 @@ private boolean shouldApplyScopeData(
hints = new Hints();
}

// TODO what does cached etc mean for devs manipulating hints in beforeSend and eventProcessor?
if (shouldApplyScopeData(event, hints)) {
Copy link
Member

Choose a reason for hiding this comment

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

What you did makes sense to only get the attachments from the scope into the hints with this condition.

If we're not applying the scope to the event/hint, an attachment still could show up in the hint because the user added it directly while calling capture. And that's totally fine.

Copy link
Member

@philipphofmann philipphofmann left a comment

Choose a reason for hiding this comment

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

I will give this another full pass later today.

Copy link
Member

@philipphofmann philipphofmann left a comment

Choose a reason for hiding this comment

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

Only the setBeforeBreadcrumb issue concerns me. Otherwise, I like this. Good job.

@@ -1282,7 +1283,7 @@ class SentryClientTest {
fun `screenshot is added to the envelope from the hint`() {
val sut = fixture.getSut()
val attachment = Attachment.fromScreenshot(byteArrayOf())
val hints = Hints().also { it.set(SENTRY_SCREENSHOT, attachment) }
Copy link
Member

Choose a reason for hiding this comment

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

The new API is way nicer 👏 .

}

@Test
fun `passing attachments via hint into breadcrumb ignores them`() {
Copy link
Member

Choose a reason for hiding this comment

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

h: Why do we have a hint in setBeforeBreadcrumb? I don't get this. This might be frankly confusing. You could do addAttachment in setBeforeBreadcrumb, and nothing would happen.

}

@Test
fun `passing attachments via hint into breadcrumb ignores them`() {
Copy link
Member

Choose a reason for hiding this comment

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

@marandaneto to answer both I guess 😄

public void remove(@NotNull String hintType) {
internalStorage.remove(hintType);
public Hints() {
primitiveMappings = new HashMap<>();
Copy link
Member

Choose a reason for hiding this comment

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

can this be done in a static block (and can it be a static map)? Not sure how many instances of HInts we'll have during the lifetime of an app.

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public final class Hints {

private final @NotNull Map<String, Object> internalStorage = new HashMap<String, Object>();
private final @NotNull List<Attachment> attachments = new CopyOnWriteArrayList<>();
private @Nullable Attachment screenshot = null;
Copy link
Member

Choose a reason for hiding this comment

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

do we need some kind of synchronization around this field (looking at attachments - it's CopyOnWriteArrayList)

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah no, I don't think it needs to be synchronized, can replace with a different implementation.

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated

@adinauer
Copy link
Member Author

Merging now to work on some changes in #2045 and not have to deal with merge conflicts. If anything needs to be changed, we can change it in #2045 before merging that.

@adinauer adinauer merged commit ac1e895 into feat/replace-map-with-hints-object May 20, 2022
@adinauer adinauer deleted the feat/allow-attachment-manipulation-via-hints branch May 20, 2022 05:32
adinauer added a commit that referenced this pull request May 23, 2022
…EventProcessor as @NotNull Hints object (#2045)

* Replace hint Map with Hints object

* Add changelog

* Fix typo in changelog

* Fix link in changelog

* Attachments can be manipulated via hints (#2046)

* Store attachments in hints and allow manipulation in beforeSend and eventProcessor

* Add changelog

* Add tests for breadcrumbs and attachments via hints

* Update CHANGELOG.md

Co-authored-by: Philipp Hofmann <[email protected]>

* Rename AttachmentContainer to Attachments

* Use long for test

* Move attachments into Hints

* Fix kotlin/java interop for Hints

* Convert screenshot from map entry to property

* Rename hint name param

* Rename clear to clearAttachments

* Use kotlin short version access for getScreenshot

* Move AttachmentsTest into HintsTest; add param names

* Make primitiveMapping table static

* Use ArrayList as there should not be a synchronization issue for hints

Co-authored-by: Philipp Hofmann <[email protected]>

* Rename Hints to Hint

* Remove commented out code

* Code Review

* Rename SentryFallbackConsumer as it is Hints specific

Co-authored-by: Philipp Hofmann <[email protected]>
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.

Add the ability to attach on hard crash only
5 participants