Skip to content

Commit 2db04c4

Browse files
feat: Scope sync attachments (#5038)
Co-authored-by: sentry-warden[bot] <258096371+sentry-warden[bot]@users.noreply.github.com>
1 parent 7ce8693 commit 2db04c4

14 files changed

+275
-8
lines changed

src/Sentry/ByteAttachmentContent.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@ namespace Sentry;
55
/// </summary>
66
public class ByteAttachmentContent : IAttachmentContent
77
{
8-
private readonly byte[] _bytes;
8+
/// <summary>
9+
/// The raw bytes of the attachment.
10+
/// </summary>
11+
internal byte[] Bytes { get; }
912

1013
/// <summary>
1114
/// Creates a new instance of <see cref="ByteAttachmentContent"/>.
1215
/// </summary>
13-
public ByteAttachmentContent(byte[] bytes) => _bytes = bytes;
16+
public ByteAttachmentContent(byte[] bytes) => Bytes = bytes;
1417

1518
/// <inheritdoc />
16-
public Stream GetStream() => new MemoryStream(_bytes);
19+
public Stream GetStream() => new MemoryStream(Bytes);
1720
}

src/Sentry/FileAttachmentContent.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@ namespace Sentry;
77
/// </summary>
88
public class FileAttachmentContent : IAttachmentContent
99
{
10-
private readonly string _filePath;
1110
private readonly bool _readFileAsynchronously;
1211

12+
/// <summary>
13+
/// The path to the file to attach.
14+
/// </summary>
15+
internal string FilePath { get; }
16+
1317
/// <summary>
1418
/// Creates a new instance of <see cref="FileAttachmentContent"/>.
1519
/// </summary>
@@ -25,13 +29,13 @@ public FileAttachmentContent(string filePath) : this(filePath, true)
2529
/// <param name="readFileAsynchronously">Whether to use async file I/O to read the file.</param>
2630
public FileAttachmentContent(string filePath, bool readFileAsynchronously)
2731
{
28-
_filePath = filePath;
32+
FilePath = filePath;
2933
_readFileAsynchronously = readFileAsynchronously;
3034
}
3135

3236
/// <inheritdoc />
3337
public Stream GetStream() => new FileStream(
34-
_filePath,
38+
FilePath,
3539
FileMode.Open,
3640
FileAccess.Read,
3741
FileShare.ReadWrite,

src/Sentry/IScopeObserver.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,14 @@ public interface IScopeObserver
3434
/// Sets the current trace
3535
/// </summary>
3636
public void SetTrace(SentryId traceId, SpanId parentSpanId);
37+
38+
/// <summary>
39+
/// Adds an attachment.
40+
/// </summary>
41+
public void AddAttachment(SentryAttachment attachment);
42+
43+
/// <summary>
44+
/// Clears all attachments.
45+
/// </summary>
46+
public void ClearAttachments();
3747
}

src/Sentry/Internal/ScopeObserver.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,22 @@ public void SetTrace(SentryId traceId, SpanId parentSpanId)
9494
}
9595

9696
public abstract void SetTraceImpl(SentryId traceId, SpanId parentSpanId);
97+
98+
public void AddAttachment(SentryAttachment attachment)
99+
{
100+
_options.DiagnosticLogger?.Log(SentryLevel.Debug,
101+
"{0} Scope Sync - Adding attachment '{1}'", null, _name, attachment.FileName);
102+
AddAttachmentImpl(attachment);
103+
}
104+
105+
public abstract void AddAttachmentImpl(SentryAttachment attachment);
106+
107+
public void ClearAttachments()
108+
{
109+
_options.DiagnosticLogger?.Log(
110+
SentryLevel.Debug, "{0} Scope Sync - Clearing attachments", null, _name);
111+
ClearAttachmentsImpl();
112+
}
113+
114+
public abstract void ClearAttachmentsImpl();
97115
}

src/Sentry/Platforms/Android/AndroidScopeObserver.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,28 @@ public void SetTrace(SentryId traceId, SpanId parentSpanId)
104104
{
105105
// TODO: This requires sentry-java 8.4.0
106106
}
107+
108+
public void AddAttachment(SentryAttachment attachment)
109+
{
110+
try
111+
{
112+
// TODO: Missing corresponding functionality on the Android SDK
113+
}
114+
finally
115+
{
116+
_innerObserver?.AddAttachment(attachment);
117+
}
118+
}
119+
120+
public void ClearAttachments()
121+
{
122+
try
123+
{
124+
// TODO: Missing corresponding functionality on the Android SDK
125+
}
126+
finally
127+
{
128+
_innerObserver?.ClearAttachments();
129+
}
130+
}
107131
}

src/Sentry/Platforms/Cocoa/CocoaScopeObserver.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,28 @@ public void SetTrace(SentryId traceId, SpanId parentSpanId)
112112
{
113113
// TODO: Missing corresponding functionality on the Cocoa SDK
114114
}
115+
116+
public void AddAttachment(SentryAttachment attachment)
117+
{
118+
try
119+
{
120+
// TODO: Missing corresponding functionality on the Cocoa SDK
121+
}
122+
finally
123+
{
124+
_innerObserver?.AddAttachment(attachment);
125+
}
126+
}
127+
128+
public void ClearAttachments()
129+
{
130+
try
131+
{
132+
// TODO: Missing corresponding functionality on the Cocoa SDK
133+
}
134+
finally
135+
{
136+
_innerObserver?.ClearAttachments();
137+
}
138+
}
115139
}

src/Sentry/Platforms/Native/NativeScopeObserver.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ public override void SetUserImpl(SentryUser user)
4343
public override void SetTraceImpl(SentryId traceId, SpanId parentSpanId) =>
4444
C.sentry_set_trace(traceId.ToString(), parentSpanId.ToString());
4545

46+
public override void AddAttachmentImpl(SentryAttachment attachment)
47+
{
48+
// TODO: Missing corresponding functionality on the Native SDK
49+
}
50+
51+
public override void ClearAttachmentsImpl()
52+
{
53+
// TODO: Missing corresponding functionality on the Native SDK
54+
}
55+
4656
private static string GetTimestamp(DateTimeOffset timestamp) =>
4757
// "o": Using ISO 8601 to make sure the timestamp makes it to the bridge correctly.
4858
// https://docs.microsoft.com/en-gb/dotnet/standard/base-types/standard-date-and-time-format-strings#Roundtrip

src/Sentry/Scope.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,14 @@ public void UnsetTag(string key)
390390
/// <summary>
391391
/// Adds an attachment.
392392
/// </summary>
393-
public void AddAttachment(SentryAttachment attachment) => _attachments.Add(attachment);
393+
public void AddAttachment(SentryAttachment attachment)
394+
{
395+
_attachments.Add(attachment);
396+
if (Options.EnableScopeSync)
397+
{
398+
Options.ScopeObserver?.AddAttachment(attachment);
399+
}
400+
}
394401

395402
internal void SetPropagationContext(SentryPropagationContext propagationContext)
396403
{
@@ -433,6 +440,10 @@ public void ClearAttachments()
433440
#else
434441
_attachments.Clear();
435442
#endif
443+
if (Options.EnableScopeSync)
444+
{
445+
Options.ScopeObserver?.ClearAttachments();
446+
}
436447
}
437448

438449
/// <summary>
@@ -535,7 +546,8 @@ public void Apply(Scope other)
535546

536547
foreach (var attachment in Attachments)
537548
{
538-
other.AddAttachment(attachment);
549+
// Set the attachment directly to avoid triggering a scope sync
550+
other._attachments.Add(attachment);
539551
}
540552
}
541553

test/Sentry.Tests/ApiApprovalTests.Run.DotNet10_0.verified.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,9 @@ namespace Sentry
217217
}
218218
public interface IScopeObserver
219219
{
220+
void AddAttachment(Sentry.SentryAttachment attachment);
220221
void AddBreadcrumb(Sentry.Breadcrumb breadcrumb);
222+
void ClearAttachments();
221223
void SetExtra(string key, object? value);
222224
void SetTag(string key, string value);
223225
void SetTrace(Sentry.SentryId traceId, Sentry.SpanId parentSpanId);

test/Sentry.Tests/ApiApprovalTests.Run.DotNet8_0.verified.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,9 @@ namespace Sentry
217217
}
218218
public interface IScopeObserver
219219
{
220+
void AddAttachment(Sentry.SentryAttachment attachment);
220221
void AddBreadcrumb(Sentry.Breadcrumb breadcrumb);
222+
void ClearAttachments();
221223
void SetExtra(string key, object? value);
222224
void SetTag(string key, string value);
223225
void SetTrace(Sentry.SentryId traceId, Sentry.SpanId parentSpanId);

0 commit comments

Comments
 (0)