From 5558d786c050cf16c9e59e760f50855c83e606fa Mon Sep 17 00:00:00 2001 From: Denis Andrasec Date: Mon, 24 Apr 2023 17:24:58 +0200 Subject: [PATCH 01/13] add hint api --- dart/lib/src/hint.dart | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/dart/lib/src/hint.dart b/dart/lib/src/hint.dart index f88be70a65..99f5191bb2 100644 --- a/dart/lib/src/hint.dart +++ b/dart/lib/src/hint.dart @@ -23,12 +23,28 @@ import 'sentry_attachment/sentry_attachment.dart'; class Hint { final Map _internalStorage = {}; + final List _attachments = []; + + List get attachments => _attachments; + SentryAttachment? screenshot; SentryAttachment? viewHierarchy; Hint(); + factory Hint.withAttachment(SentryAttachment attachment) { + final hint = Hint(); + hint._attachments.add(attachment); + return hint; + } + + factory Hint.withAttachments(List attachments) { + final hint = Hint(); + hint._attachments.addAll(attachments); + return hint; + } + factory Hint.withMap(Map map) { final hint = Hint(); hint.addAll(map); @@ -47,6 +63,23 @@ class Hint { return hint; } + void addAttachment(SentryAttachment attachment) { + _attachments.add(attachment); + } + + void addAttachments(List attachments) { + _attachments.addAll(attachments); + } + + void replaceAttachments(List attachments) { + clearAttachments(); + addAttachments(attachments); + } + + void clearAttachments() { + _attachments.clear(); + } + // Objects void addAll(Map keysAndValues) { From a603593dd3d07a03cf3292c06f6e7b8d84b91f8a Mon Sep 17 00:00:00 2001 From: Denis Andrasec Date: Tue, 25 Apr 2023 09:10:24 +0200 Subject: [PATCH 02/13] test new hint apis --- dart/test/hint_test.dart | 57 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/dart/test/hint_test.dart b/dart/test/hint_test.dart index e0b686820f..49f2f3b045 100644 --- a/dart/test/hint_test.dart +++ b/dart/test/hint_test.dart @@ -1,7 +1,14 @@ -import 'package:sentry/src/hint.dart'; +import 'package:sentry/sentry.dart'; import 'package:test/test.dart'; void main() { + + late Fixture fixture; + + setUp(() { + fixture = Fixture(); + }); + test('Hint init with map', () { final hint = Hint.withMap({'fixture-key': 'fixture-value'}); expect("fixture-value", hint.get("fixture-key")); @@ -60,4 +67,52 @@ void main() { expect(hint.get("hint1"), null); expect(hint.get("hint2"), null); }); + + test('add attachment', () { + final attachment = SentryAttachment.fromIntList([], "fixture-fileName"); + + final sut = fixture.givenSut(); + sut.addAttachment(attachment); + + expect(sut.attachments.contains(attachment), true); + }); + + test('add attachments', () { + final attachmentA = SentryAttachment.fromIntList([], "fixture-fileName-A"); + final attachmentB = SentryAttachment.fromIntList([], "fixture-fileName-B"); + + final sut = fixture.givenSut(); + sut.addAttachments([attachmentA, attachmentB]); + + expect(sut.attachments.contains(attachmentA), true); + expect(sut.attachments.contains(attachmentB), true); + }); + + test('replace attachments', () { + final attachmentA = SentryAttachment.fromIntList([], "fixture-fileName-A"); + final attachmentB = SentryAttachment.fromIntList([], "fixture-fileName-B"); + + final sut = fixture.givenSut(); + sut.addAttachment(attachmentA); + sut.replaceAttachments([attachmentB]); + + expect(sut.attachments.contains(attachmentA), false); + expect(sut.attachments.contains(attachmentB), true); + }); + + test('clear attachments', () { + final attachment = SentryAttachment.fromIntList([], "fixture-fileName"); + + final sut = fixture.givenSut(); + sut.addAttachment(attachment); + sut.clearAttachments(); + + expect(sut.attachments.contains(attachment), false); + }); +} + +class Fixture { + Hint givenSut() { + return Hint(); + } } From 3fa09f097db4f6547aebe9953dd50636ca07e802 Mon Sep 17 00:00:00 2001 From: Denis Andrasec Date: Tue, 25 Apr 2023 09:14:57 +0200 Subject: [PATCH 03/13] add additional test --- dart/test/hint_test.dart | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/dart/test/hint_test.dart b/dart/test/hint_test.dart index 49f2f3b045..90005ae4ea 100644 --- a/dart/test/hint_test.dart +++ b/dart/test/hint_test.dart @@ -109,6 +109,21 @@ void main() { expect(sut.attachments.contains(attachment), false); }); + + test('clear does not remove attachments, screenshot & viewHierarchy', () { + final attachment = SentryAttachment.fromIntList([], "fixture-fileName"); + + final sut = fixture.givenSut(); + sut.addAttachment(attachment); + sut.screenshot = attachment; + sut.viewHierarchy = attachment; + + sut.clear(); + + expect(sut.attachments.contains(attachment), true); + expect(sut.screenshot, attachment); + expect(sut.viewHierarchy, attachment); + }); } class Fixture { From fb43a8f54ba58eb5fc98489a68fb008d27e8b355 Mon Sep 17 00:00:00 2001 From: Denis Andrasec Date: Tue, 25 Apr 2023 09:23:33 +0200 Subject: [PATCH 04/13] add attachmets from hint in client --- dart/lib/src/sentry_client.dart | 4 ++++ dart/test/hint_test.dart | 1 - dart/test/sentry_client_test.dart | 14 ++++++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/dart/lib/src/sentry_client.dart b/dart/lib/src/sentry_client.dart index fef17b6eab..ed87066f2b 100644 --- a/dart/lib/src/sentry_client.dart +++ b/dart/lib/src/sentry_client.dart @@ -111,6 +111,7 @@ class SentryClient { preparedEvent = _eventWithoutBreadcrumbsIfNeeded(preparedEvent); var attachments = List.from(scope?.attachments ?? []); + attachments.addAll(hint.attachments); var screenshot = hint.screenshot; if (screenshot != null) { attachments.add(screenshot); @@ -323,6 +324,9 @@ class SentryClient { final attachments = scope?.attachments .where((element) => element.addToTransactions) .toList(); + + // TODO Do we need to add attahcments? + final envelope = SentryEnvelope.fromTransaction( preparedTransaction, _options.sdk, diff --git a/dart/test/hint_test.dart b/dart/test/hint_test.dart index 90005ae4ea..8723845c52 100644 --- a/dart/test/hint_test.dart +++ b/dart/test/hint_test.dart @@ -2,7 +2,6 @@ import 'package:sentry/sentry.dart'; import 'package:test/test.dart'; void main() { - late Fixture fixture; setUp(() { diff --git a/dart/test/sentry_client_test.dart b/dart/test/sentry_client_test.dart index 1669e9049e..c055f3449f 100644 --- a/dart/test/sentry_client_test.dart +++ b/dart/test/sentry_client_test.dart @@ -1413,6 +1413,20 @@ void main() { expect(envelope.header.traceContext, isNotNull); }); + test('captureEvent adds attachments from hint', () async { + final attachment = SentryAttachment.fromIntList([], "fixture-fileName"); + final hint = Hint.withAttachment(attachment); + + final sut = fixture.getSut(); + await sut.captureEvent(fakeEvent, hint: hint); + + final capturedEnvelope = (fixture.transport).envelopes.first; + final attachmentItem = capturedEnvelope.items.firstWhereOrNull( + (element) => element.header.type == SentryItemType.attachment); + expect(attachmentItem?.header.attachmentType, + SentryAttachment.typeAttachmentDefault); + }); + test('captureEvent adds screenshot from hint', () async { final client = fixture.getSut(); final screenshot = From b144d4476846900b8d357944e6a2238cfaf6f164 Mon Sep 17 00:00:00 2001 From: Denis Andrasec Date: Tue, 25 Apr 2023 09:26:49 +0200 Subject: [PATCH 05/13] add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 99eef6e809..cda8f53224 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ - Sync `connectionTimeout` and `readTimeout` to Android ([#1397](https://github.com/getsentry/sentry-dart/pull/1397)) - Set User `name` and `geo` in native plugins ([#1393](https://github.com/getsentry/sentry-dart/pull/1393)) - Add processor count to device info ([#1402](https://github.com/getsentry/sentry-dart/pull/1402)) +- Add attachments to `Hint` ([#1404](https://github.com/getsentry/sentry-dart/pull/1404)) ### Fixes From 7b920fe6857208ae2f44f9a9d85dd8912804683e Mon Sep 17 00:00:00 2001 From: Denis Andrasec Date: Tue, 25 Apr 2023 09:33:18 +0200 Subject: [PATCH 06/13] remove redundant getters/setters --- dart/lib/src/hint.dart | 25 +++-------------------- dart/test/hint_test.dart | 44 +--------------------------------------- 2 files changed, 4 insertions(+), 65 deletions(-) diff --git a/dart/lib/src/hint.dart b/dart/lib/src/hint.dart index 99f5191bb2..29e1c45b8c 100644 --- a/dart/lib/src/hint.dart +++ b/dart/lib/src/hint.dart @@ -23,9 +23,7 @@ import 'sentry_attachment/sentry_attachment.dart'; class Hint { final Map _internalStorage = {}; - final List _attachments = []; - - List get attachments => _attachments; + final List attachments = []; SentryAttachment? screenshot; @@ -35,13 +33,13 @@ class Hint { factory Hint.withAttachment(SentryAttachment attachment) { final hint = Hint(); - hint._attachments.add(attachment); + hint.attachments.add(attachment); return hint; } factory Hint.withAttachments(List attachments) { final hint = Hint(); - hint._attachments.addAll(attachments); + hint.attachments.addAll(attachments); return hint; } @@ -63,23 +61,6 @@ class Hint { return hint; } - void addAttachment(SentryAttachment attachment) { - _attachments.add(attachment); - } - - void addAttachments(List attachments) { - _attachments.addAll(attachments); - } - - void replaceAttachments(List attachments) { - clearAttachments(); - addAttachments(attachments); - } - - void clearAttachments() { - _attachments.clear(); - } - // Objects void addAll(Map keysAndValues) { diff --git a/dart/test/hint_test.dart b/dart/test/hint_test.dart index 8723845c52..a1d56ecdd1 100644 --- a/dart/test/hint_test.dart +++ b/dart/test/hint_test.dart @@ -67,53 +67,11 @@ void main() { expect(hint.get("hint2"), null); }); - test('add attachment', () { - final attachment = SentryAttachment.fromIntList([], "fixture-fileName"); - - final sut = fixture.givenSut(); - sut.addAttachment(attachment); - - expect(sut.attachments.contains(attachment), true); - }); - - test('add attachments', () { - final attachmentA = SentryAttachment.fromIntList([], "fixture-fileName-A"); - final attachmentB = SentryAttachment.fromIntList([], "fixture-fileName-B"); - - final sut = fixture.givenSut(); - sut.addAttachments([attachmentA, attachmentB]); - - expect(sut.attachments.contains(attachmentA), true); - expect(sut.attachments.contains(attachmentB), true); - }); - - test('replace attachments', () { - final attachmentA = SentryAttachment.fromIntList([], "fixture-fileName-A"); - final attachmentB = SentryAttachment.fromIntList([], "fixture-fileName-B"); - - final sut = fixture.givenSut(); - sut.addAttachment(attachmentA); - sut.replaceAttachments([attachmentB]); - - expect(sut.attachments.contains(attachmentA), false); - expect(sut.attachments.contains(attachmentB), true); - }); - - test('clear attachments', () { - final attachment = SentryAttachment.fromIntList([], "fixture-fileName"); - - final sut = fixture.givenSut(); - sut.addAttachment(attachment); - sut.clearAttachments(); - - expect(sut.attachments.contains(attachment), false); - }); - test('clear does not remove attachments, screenshot & viewHierarchy', () { final attachment = SentryAttachment.fromIntList([], "fixture-fileName"); final sut = fixture.givenSut(); - sut.addAttachment(attachment); + sut.attachments.add(attachment); sut.screenshot = attachment; sut.viewHierarchy = attachment; From 17fd7732ffc7b5ddca19dfe03d0383db7e89b404 Mon Sep 17 00:00:00 2001 From: Denis Andrasec Date: Tue, 25 Apr 2023 09:33:57 +0200 Subject: [PATCH 07/13] remove todo --- dart/lib/src/sentry_client.dart | 3 --- 1 file changed, 3 deletions(-) diff --git a/dart/lib/src/sentry_client.dart b/dart/lib/src/sentry_client.dart index ed87066f2b..09f98e4d02 100644 --- a/dart/lib/src/sentry_client.dart +++ b/dart/lib/src/sentry_client.dart @@ -324,9 +324,6 @@ class SentryClient { final attachments = scope?.attachments .where((element) => element.addToTransactions) .toList(); - - // TODO Do we need to add attahcments? - final envelope = SentryEnvelope.fromTransaction( preparedTransaction, _options.sdk, From f59554518cc8e42aa9019cafeef0d95deac1a389 Mon Sep 17 00:00:00 2001 From: Denis Andrasec Date: Tue, 25 Apr 2023 09:34:50 +0200 Subject: [PATCH 08/13] fix imports --- dart/test/hint_test.dart | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dart/test/hint_test.dart b/dart/test/hint_test.dart index a1d56ecdd1..d46a022405 100644 --- a/dart/test/hint_test.dart +++ b/dart/test/hint_test.dart @@ -1,4 +1,5 @@ -import 'package:sentry/sentry.dart'; +import 'package:sentry/src/hint.dart'; +import 'package:sentry/src/sentry_attachment/sentry_attachment.dart'; import 'package:test/test.dart'; void main() { From 4f1b6869930541c522228e5398db11fc7a838c6a Mon Sep 17 00:00:00 2001 From: Denis Andrasec Date: Tue, 25 Apr 2023 09:51:23 +0200 Subject: [PATCH 09/13] add code snippet --- dart/lib/src/hint.dart | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/dart/lib/src/hint.dart b/dart/lib/src/hint.dart index 29e1c45b8c..2b564f133f 100644 --- a/dart/lib/src/hint.dart +++ b/dart/lib/src/hint.dart @@ -1,3 +1,5 @@ +import 'dart:convert'; + import 'sentry_attachment/sentry_attachment.dart'; /// Hints are used in [BeforeSendCallback], [BeforeBreadcrumbCallback] and @@ -20,6 +22,25 @@ import 'sentry_attachment/sentry_attachment.dart'; /// }; /// } /// ``` +/// +/// The hint can also be used to add attachments to events. +/// +/// Example: +/// +/// ```dart +/// import 'dart:convert'; +/// +/// options.beforeSend = (event, {hint}) { +/// final text = 'This event should not be sent happen in prod. Investigate.'; +/// final textAttachment = SentryAttachment.fromIntList( +/// utf8.encode(text), +/// 'event_info.txt', +/// contentType: 'text/plain', +/// ); +/// hint?.attachments.add(textAttachment); +/// return event; +/// }; +/// ``` class Hint { final Map _internalStorage = {}; From f8c371b498ef2528538120cef62ae357df35ba73 Mon Sep 17 00:00:00 2001 From: Denis Andrasec Date: Tue, 25 Apr 2023 10:07:20 +0200 Subject: [PATCH 10/13] link hint class in doc --- dart/lib/src/hint.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dart/lib/src/hint.dart b/dart/lib/src/hint.dart index 2b564f133f..84a765a508 100644 --- a/dart/lib/src/hint.dart +++ b/dart/lib/src/hint.dart @@ -23,7 +23,7 @@ import 'sentry_attachment/sentry_attachment.dart'; /// } /// ``` /// -/// The hint can also be used to add attachments to events. +/// The [Hint] can also be used to add attachments to events. /// /// Example: /// From a9181efe4e5e3539ce0e37e35e06569bfdeb6c1a Mon Sep 17 00:00:00 2001 From: Denis Andrasec Date: Tue, 25 Apr 2023 10:13:42 +0200 Subject: [PATCH 11/13] remove unused import --- dart/lib/src/hint.dart | 2 -- 1 file changed, 2 deletions(-) diff --git a/dart/lib/src/hint.dart b/dart/lib/src/hint.dart index 84a765a508..87620d7ba1 100644 --- a/dart/lib/src/hint.dart +++ b/dart/lib/src/hint.dart @@ -1,5 +1,3 @@ -import 'dart:convert'; - import 'sentry_attachment/sentry_attachment.dart'; /// Hints are used in [BeforeSendCallback], [BeforeBreadcrumbCallback] and From a8e2293a9293311beab321ec73893849527364ec Mon Sep 17 00:00:00 2001 From: Denis Andrasec Date: Tue, 25 Apr 2023 10:58:36 +0200 Subject: [PATCH 12/13] add code snippet to changelog --- CHANGELOG.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index cda8f53224..8940bbeb0d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,21 @@ - Add processor count to device info ([#1402](https://github.com/getsentry/sentry-dart/pull/1402)) - Add attachments to `Hint` ([#1404](https://github.com/getsentry/sentry-dart/pull/1404)) +```dart + import 'dart:convert'; + + options.beforeSend = (event, {hint}) { + final text = 'This event should not be sent happen in prod. Investigate.'; + final textAttachment = SentryAttachment.fromIntList( + utf8.encode(text), + 'event_info.txt', + contentType: 'text/plain', + ); + hint?.attachments.add(textAttachment); + return event; + }; +``` + ### Fixes - Screenshots and View Hierarchy should only be added to errors ([#1385](https://github.com/getsentry/sentry-dart/pull/1385)) From 7670e7378d10982f3799c65d9d6123c82e07802d Mon Sep 17 00:00:00 2001 From: Denis Andrasec Date: Tue, 25 Apr 2023 11:19:27 +0200 Subject: [PATCH 13/13] format code snippet --- CHANGELOG.md | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8940bbeb0d..9c1f5617b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,18 +16,18 @@ - Add attachments to `Hint` ([#1404](https://github.com/getsentry/sentry-dart/pull/1404)) ```dart - import 'dart:convert'; - - options.beforeSend = (event, {hint}) { - final text = 'This event should not be sent happen in prod. Investigate.'; - final textAttachment = SentryAttachment.fromIntList( - utf8.encode(text), - 'event_info.txt', - contentType: 'text/plain', - ); - hint?.attachments.add(textAttachment); - return event; - }; +import 'dart:convert'; + +options.beforeSend = (event, {hint}) { + final text = 'This event should not be sent happen in prod. Investigate.'; + final textAttachment = SentryAttachment.fromIntList( + utf8.encode(text), + 'event_info.txt', + contentType: 'text/plain', + ); + hint?.attachments.add(textAttachment); + return event; +}; ``` ### Fixes