Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/Sentry/ByteAttachmentContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ namespace Sentry;
/// </summary>
public class ByteAttachmentContent : IAttachmentContent
{
private readonly byte[] _bytes;
/// <summary>
/// The raw bytes of the attachment.
/// </summary>
internal byte[] Bytes { get; }

/// <summary>
/// Creates a new instance of <see cref="ByteAttachmentContent"/>.
/// </summary>
public ByteAttachmentContent(byte[] bytes) => _bytes = bytes;
public ByteAttachmentContent(byte[] bytes) => Bytes = bytes;

/// <inheritdoc />
public Stream GetStream() => new MemoryStream(_bytes);
public Stream GetStream() => new MemoryStream(Bytes);
}
10 changes: 7 additions & 3 deletions src/Sentry/FileAttachmentContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ namespace Sentry;
/// </summary>
public class FileAttachmentContent : IAttachmentContent
{
private readonly string _filePath;
private readonly bool _readFileAsynchronously;

/// <summary>
/// The path to the file to attach.
/// </summary>
internal string FilePath { get; }

/// <summary>
/// Creates a new instance of <see cref="FileAttachmentContent"/>.
/// </summary>
Expand All @@ -25,13 +29,13 @@ public FileAttachmentContent(string filePath) : this(filePath, true)
/// <param name="readFileAsynchronously">Whether to use async file I/O to read the file.</param>
public FileAttachmentContent(string filePath, bool readFileAsynchronously)
{
_filePath = filePath;
FilePath = filePath;
_readFileAsynchronously = readFileAsynchronously;
}

/// <inheritdoc />
public Stream GetStream() => new FileStream(
_filePath,
FilePath,
FileMode.Open,
FileAccess.Read,
FileShare.ReadWrite,
Expand Down
10 changes: 10 additions & 0 deletions src/Sentry/IScopeObserver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,14 @@ public interface IScopeObserver
/// Sets the current trace
/// </summary>
public void SetTrace(SentryId traceId, SpanId parentSpanId);

/// <summary>
/// Adds an attachment.
/// </summary>
public void AddAttachment(SentryAttachment attachment);

/// <summary>
/// Clears all attachments.
/// </summary>
public void ClearAttachments();
}
18 changes: 18 additions & 0 deletions src/Sentry/Internal/ScopeObserver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,22 @@ public void SetTrace(SentryId traceId, SpanId parentSpanId)
}

public abstract void SetTraceImpl(SentryId traceId, SpanId parentSpanId);

public void AddAttachment(SentryAttachment attachment)
{
_options.DiagnosticLogger?.Log(SentryLevel.Debug,
"{0} Scope Sync - Adding attachment '{1}'", null, _name, attachment.FileName);
AddAttachmentImpl(attachment);
}

public abstract void AddAttachmentImpl(SentryAttachment attachment);

public void ClearAttachments()
{
_options.DiagnosticLogger?.Log(
SentryLevel.Debug, "{0} Scope Sync - Clearing attachments", null, _name);
ClearAttachmentsImpl();
}

public abstract void ClearAttachmentsImpl();
}
24 changes: 24 additions & 0 deletions src/Sentry/Platforms/Android/AndroidScopeObserver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,28 @@ public void SetTrace(SentryId traceId, SpanId parentSpanId)
{
// TODO: This requires sentry-java 8.4.0
}

public void AddAttachment(SentryAttachment attachment)
{
try
{
// TODO: Missing corresponding functionality on the Android SDK
}
finally
{
_innerObserver?.AddAttachment(attachment);
}
}

public void ClearAttachments()
{
try
{
// TODO: Missing corresponding functionality on the Android SDK
}
finally
{
_innerObserver?.ClearAttachments();
}
}
}
24 changes: 24 additions & 0 deletions src/Sentry/Platforms/Cocoa/CocoaScopeObserver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,28 @@ public void SetTrace(SentryId traceId, SpanId parentSpanId)
{
// TODO: Missing corresponding functionality on the Cocoa SDK
}

public void AddAttachment(SentryAttachment attachment)
{
try
{
// TODO: Missing corresponding functionality on the Cocoa SDK
}
finally
{
_innerObserver?.AddAttachment(attachment);
}
}

public void ClearAttachments()
{
try
{
// TODO: Missing corresponding functionality on the Cocoa SDK
}
finally
{
_innerObserver?.ClearAttachments();
}
}
}
10 changes: 10 additions & 0 deletions src/Sentry/Platforms/Native/NativeScopeObserver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ public override void SetUserImpl(SentryUser user)
public override void SetTraceImpl(SentryId traceId, SpanId parentSpanId) =>
C.sentry_set_trace(traceId.ToString(), parentSpanId.ToString());

public override void AddAttachmentImpl(SentryAttachment attachment)
{
// TODO: Missing corresponding functionality on the Native SDK
}

public override void ClearAttachmentsImpl()
{
// TODO: Missing corresponding functionality on the Native SDK
}

private static string GetTimestamp(DateTimeOffset timestamp) =>
// "o": Using ISO 8601 to make sure the timestamp makes it to the bridge correctly.
// https://docs.microsoft.com/en-gb/dotnet/standard/base-types/standard-date-and-time-format-strings#Roundtrip
Expand Down
16 changes: 14 additions & 2 deletions src/Sentry/Scope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,14 @@ public void UnsetTag(string key)
/// <summary>
/// Adds an attachment.
/// </summary>
public void AddAttachment(SentryAttachment attachment) => _attachments.Add(attachment);
public void AddAttachment(SentryAttachment attachment)
{
_attachments.Add(attachment);
if (Options.EnableScopeSync)
{
Options.ScopeObserver?.AddAttachment(attachment);
}
}

internal void SetPropagationContext(SentryPropagationContext propagationContext)
{
Expand Down Expand Up @@ -433,6 +440,10 @@ public void ClearAttachments()
#else
_attachments.Clear();
#endif
if (Options.EnableScopeSync)
{
Options.ScopeObserver?.ClearAttachments();
}
}

/// <summary>
Expand Down Expand Up @@ -535,7 +546,8 @@ public void Apply(Scope other)

foreach (var attachment in Attachments)
{
other.AddAttachment(attachment);
// Set the attachment directly to avoid triggering a scope sync
other._attachments.Add(attachment);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The pushing and popping the scope does not get propagated to the native layer. So syncing the same attachments again doesn't make sense.

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,9 @@ namespace Sentry
}
public interface IScopeObserver
{
void AddAttachment(Sentry.SentryAttachment attachment);
void AddBreadcrumb(Sentry.Breadcrumb breadcrumb);
void ClearAttachments();
void SetExtra(string key, object? value);
void SetTag(string key, string value);
void SetTrace(Sentry.SentryId traceId, Sentry.SpanId parentSpanId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,9 @@ namespace Sentry
}
public interface IScopeObserver
{
void AddAttachment(Sentry.SentryAttachment attachment);
void AddBreadcrumb(Sentry.Breadcrumb breadcrumb);
void ClearAttachments();
void SetExtra(string key, object? value);
void SetTag(string key, string value);
void SetTrace(Sentry.SentryId traceId, Sentry.SpanId parentSpanId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,9 @@ namespace Sentry
}
public interface IScopeObserver
{
void AddAttachment(Sentry.SentryAttachment attachment);
void AddBreadcrumb(Sentry.Breadcrumb breadcrumb);
void ClearAttachments();
void SetExtra(string key, object? value);
void SetTag(string key, string value);
void SetTrace(Sentry.SentryId traceId, Sentry.SpanId parentSpanId);
Expand Down
2 changes: 2 additions & 0 deletions test/Sentry.Tests/ApiApprovalTests.Run.Net4_8.verified.txt
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,9 @@ namespace Sentry
}
public interface IScopeObserver
{
void AddAttachment(Sentry.SentryAttachment attachment);
void AddBreadcrumb(Sentry.Breadcrumb breadcrumb);
void ClearAttachments();
void SetExtra(string key, object? value);
void SetTag(string key, string value);
void SetTrace(Sentry.SentryId traceId, Sentry.SpanId parentSpanId);
Expand Down
31 changes: 31 additions & 0 deletions test/Sentry.Tests/AttachmentTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,38 @@
namespace Sentry.Tests;

public class ByteAttachmentContentTests
{
[Fact]
public void Bytes_ReturnsConstructorValue()
{
var data = new byte[] { 1, 2, 3 };
var content = new ByteAttachmentContent(data);
Assert.Same(data, content.Bytes);
}

[Fact]
public void GetStream_ReturnsBytesContent()
{
var data = new byte[] { 10, 20, 30 };
var content = new ByteAttachmentContent(data);

using var stream = content.GetStream();
using var ms = new MemoryStream();
stream.CopyTo(ms);

Assert.Equal(data, ms.ToArray());
}
}

public class FileAttachmentContentTests
{
[Fact]
public void FilePath_ReturnsConstructorValue()
{
var attachment = new FileAttachmentContent("/some/path/file.txt");
Assert.Equal("/some/path/file.txt", attachment.FilePath);
}

[Fact]
public void DoesNotLock()
{
Expand Down
Loading
Loading