Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
31 changes: 31 additions & 0 deletions src/Sentry/Http/HttpTransportBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,37 @@ private void ProcessEnvelopeItem(DateTimeOffset now, EnvelopeItem item, List<Env
item.TryGetFileName(),
item.TryGetLength());

if (item.LocalFilePath is { } localFilePath &&
item.Header.TryGetValue("attachment_type", out var attachmentType) &&
string.Equals(attachmentType as string, "event.heapdump", StringComparison.Ordinal))
{
item.Dispose();

try
{
if (_options.FileSystem.DeleteFile(localFilePath))
{
_options.LogDebug(
"{0}: Local heap dump file '{1}' exceeded the attachment size limit and was deleted.",
_typeName,
localFilePath);
}
else
{
_options.LogError(
"{0}: Failed to delete local heap dump file '{1}' after exceeding the attachment size limit.",
_typeName,
localFilePath);
}
}
catch (Exception ex)
{
_options.LogError(ex,
"{0}: Failed to delete local heap dump file '{1}'.",
_typeName,
localFilePath);
}
}
Comment thread
cursor[bot] marked this conversation as resolved.
Outdated
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Sentry/Internal/Hub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ internal void CaptureHeapDump(string dumpFile)
Level = _options.HeapDumpOptions?.Level ?? SentryLevel.Warning,
};
var hint = new SentryHint(_options);
hint.AddAttachment(dumpFile);
hint.AddAttachment(dumpFile, AttachmentType.HeapDump);
CaptureEvent(evt, CurrentScope, hint);
}
catch (Exception e)
Expand Down
13 changes: 11 additions & 2 deletions src/Sentry/Protocol/Envelopes/EnvelopeItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public sealed class EnvelopeItem : ISerializable, IDisposable
private const string LengthKey = "length";
private const string FileNameKey = "filename";

/// <summary>
/// The local file path associated with this envelope item.
/// </summary>
internal string? LocalFilePath { get; }

/// <summary>
/// Header associated with this envelope item.
/// </summary>
Expand Down Expand Up @@ -72,10 +77,11 @@ public sealed class EnvelopeItem : ISerializable, IDisposable
/// <summary>
/// Initializes an instance of <see cref="EnvelopeItem"/>.
/// </summary>
public EnvelopeItem(IReadOnlyDictionary<string, object?> header, ISerializable payload)
public EnvelopeItem(IReadOnlyDictionary<string, object?> header, ISerializable payload, string? localFilePath = null)
{
Header = header;
Payload = payload;
LocalFilePath = localFilePath;
}

/// <summary>
Expand Down Expand Up @@ -342,6 +348,7 @@ internal static EnvelopeItem FromAttachment(SentryAttachment attachment, Stream
AttachmentType.UnrealContext => "unreal.context",
AttachmentType.UnrealLogs => "unreal.logs",
AttachmentType.ViewHierarchy => "event.view_hierarchy",
AttachmentType.HeapDump => "event.heapdump",
_ => "event.attachment"
};

Expand All @@ -354,7 +361,9 @@ internal static EnvelopeItem FromAttachment(SentryAttachment attachment, Stream
["content_type"] = attachment.ContentType
};

return new EnvelopeItem(header, new StreamSerializable(stream));
var localFilePath = attachment.Content is FileAttachmentContent fileContent ? fileContent.FilePath : null;

return new EnvelopeItem(header, new StreamSerializable(stream), localFilePath);
}

/// <summary>
Expand Down
9 changes: 8 additions & 1 deletion src/Sentry/SentryAttachment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,14 @@ public enum AttachmentType
/// <summary>
/// A JSON attachment containing the View Hierarchy
/// </summary>
ViewHierarchy
ViewHierarchy,

/// <summary>
/// A .gcdump file captured when a configured memory threshold is exceeded.
/// Used internally to allow the SDK to clean up the file from disk if it can't be sent to Sentry
/// (e.g. because it exceeds the attachment size limit).
/// </summary>
HeapDump
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Sentry
UnrealContext = 3,
UnrealLogs = 4,
ViewHierarchy = 5,
HeapDump = 6,
}
public class BaggageHeader
{
Expand Down Expand Up @@ -2048,7 +2049,7 @@ namespace Sentry.Protocol.Envelopes
}
public sealed class EnvelopeItem : Sentry.Protocol.Envelopes.ISerializable, System.IDisposable
{
public EnvelopeItem(System.Collections.Generic.IReadOnlyDictionary<string, object?> header, Sentry.Protocol.Envelopes.ISerializable payload) { }
public EnvelopeItem(System.Collections.Generic.IReadOnlyDictionary<string, object?> header, Sentry.Protocol.Envelopes.ISerializable payload, string? localFilePath = null) { }
public System.Collections.Generic.IReadOnlyDictionary<string, object?> Header { get; }
public Sentry.Protocol.Envelopes.ISerializable Payload { get; }
public void Dispose() { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Sentry
UnrealContext = 3,
UnrealLogs = 4,
ViewHierarchy = 5,
HeapDump = 6,
}
public class BaggageHeader
{
Expand Down Expand Up @@ -2048,7 +2049,7 @@ namespace Sentry.Protocol.Envelopes
}
public sealed class EnvelopeItem : Sentry.Protocol.Envelopes.ISerializable, System.IDisposable
{
public EnvelopeItem(System.Collections.Generic.IReadOnlyDictionary<string, object?> header, Sentry.Protocol.Envelopes.ISerializable payload) { }
public EnvelopeItem(System.Collections.Generic.IReadOnlyDictionary<string, object?> header, Sentry.Protocol.Envelopes.ISerializable payload, string? localFilePath = null) { }
public System.Collections.Generic.IReadOnlyDictionary<string, object?> Header { get; }
public Sentry.Protocol.Envelopes.ISerializable Payload { get; }
public void Dispose() { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Sentry
UnrealContext = 3,
UnrealLogs = 4,
ViewHierarchy = 5,
HeapDump = 6,
}
public class BaggageHeader
{
Expand Down Expand Up @@ -2048,7 +2049,7 @@ namespace Sentry.Protocol.Envelopes
}
public sealed class EnvelopeItem : Sentry.Protocol.Envelopes.ISerializable, System.IDisposable
{
public EnvelopeItem(System.Collections.Generic.IReadOnlyDictionary<string, object?> header, Sentry.Protocol.Envelopes.ISerializable payload) { }
public EnvelopeItem(System.Collections.Generic.IReadOnlyDictionary<string, object?> header, Sentry.Protocol.Envelopes.ISerializable payload, string? localFilePath = null) { }
public System.Collections.Generic.IReadOnlyDictionary<string, object?> Header { get; }
public Sentry.Protocol.Envelopes.ISerializable Payload { get; }
public void Dispose() { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Sentry
UnrealContext = 3,
UnrealLogs = 4,
ViewHierarchy = 5,
HeapDump = 6,
}
public class BaggageHeader
{
Expand Down Expand Up @@ -2024,7 +2025,7 @@ namespace Sentry.Protocol.Envelopes
}
public sealed class EnvelopeItem : Sentry.Protocol.Envelopes.ISerializable, System.IDisposable
{
public EnvelopeItem(System.Collections.Generic.IReadOnlyDictionary<string, object?> header, Sentry.Protocol.Envelopes.ISerializable payload) { }
public EnvelopeItem(System.Collections.Generic.IReadOnlyDictionary<string, object?> header, Sentry.Protocol.Envelopes.ISerializable payload, string? localFilePath = null) { }
public System.Collections.Generic.IReadOnlyDictionary<string, object?> Header { get; }
public Sentry.Protocol.Envelopes.ISerializable Payload { get; }
public void Dispose() { }
Expand Down
93 changes: 93 additions & 0 deletions test/Sentry.Tests/Internals/Http/HttpTransportTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,99 @@ public async Task SendEnvelopeAsync_AttachmentTooLarge_DropsItem()
actualEnvelopeSerialized.Should().NotContain("test2.txt");
}

[Fact]
public async Task SendEnvelopeAsync_HeapDumpAttachmentTooLarge_DeleteFilesAndLogsDebug()
{
var tempFilePath = Path.GetTempFileName();
File.WriteAllBytes(tempFilePath, new byte[] { 1, 2, 3, 4, 5 });

var logger = new InMemoryDiagnosticLogger();

var httpTransport = new HttpTransport(
new SentryOptions
{
Dsn = ValidDsn,
MaxAttachmentSize = 1,
DiagnosticLogger = logger,
Debug = true
},
new HttpClient(new RecordingHttpMessageHandler(new FakeHttpMessageHandler())));

var HeapDumpAttachment = new SentryAttachment(
AttachmentType.HeapDump,
new FileAttachmentContent(tempFilePath),
Path.GetFileName(tempFilePath),
null);

using var envelope = Envelope.FromEvent(
new SentryEvent(),
null,
new[] { HeapDumpAttachment });

try
{
// Act
await httpTransport.SendEnvelopeAsync(envelope);

// Assert
File.Exists(tempFilePath).Should().BeFalse();

logger.Entries.Should().Contain(e =>
e.Level == SentryLevel.Debug &&
string.Format(e.Message, e.Args).Contains("exceeded the attachment size limit and was deleted"));
}
finally
{
File.Delete(tempFilePath);
}
}

[Fact]
public async Task SendEnvelopeAsync_NonHeapDumpAttachmentTooLarge_DoesNotDeleteFile()
{
var tempFilePath = Path.GetTempFileName();
File.WriteAllBytes(tempFilePath, new byte[] { 1, 2, 3, 4, 5 });

var logger = new InMemoryDiagnosticLogger();

var httpTransport = new HttpTransport(
new SentryOptions
{
Dsn = ValidDsn,
MaxAttachmentSize = 1,
DiagnosticLogger = logger,
Debug = true
},
new HttpClient(new RecordingHttpMessageHandler(new FakeHttpMessageHandler())));

var NonHeapDumpAttachment = new SentryAttachment(
AttachmentType.Default,
new FileAttachmentContent(tempFilePath),
Path.GetFileName(tempFilePath),
null);

using var envelope = Envelope.FromEvent(
new SentryEvent(),
null,
new[] { NonHeapDumpAttachment });

try
{
// Act
await httpTransport.SendEnvelopeAsync(envelope);

// Assert
File.Exists(tempFilePath).Should().BeTrue();

logger.Entries.Should().NotContain(e =>
string.Format(e.Message, e.Args).Contains("exceeded the attachment size limit and was deleted"));
}
finally
{
File.Delete(tempFilePath);
}
}

[Fact]
public async Task SendEnvelopeAsync_ItemRateLimit_PromotesNextSessionWithSameId()
{
Expand Down
Loading