Skip to content
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

dont fail for attachment read error #1378

Merged
merged 6 commits into from
Dec 10, 2021
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Fixes

- Dont fail for attachment read error ([#1378](https://github.com/getsentry/sentry-dotnet/pull/1378))
- Fix file locking in attachments ([#1377](https://github.com/getsentry/sentry-dotnet/pull/1377))

## 3.12.1
Expand Down
19 changes: 18 additions & 1 deletion src/Sentry/Envelopes/Envelope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Sentry.Extensibility;
using Sentry.Internal;
using Sentry.Internal.Extensions;

Expand Down Expand Up @@ -110,6 +111,7 @@ public async Task SerializeAsync(Stream stream, CancellationToken cancellationTo
/// </summary>
public static Envelope FromEvent(
SentryEvent @event,
IDiagnosticLogger? logger = null,
IReadOnlyCollection<Attachment>? attachments = null,
SessionUpdate? sessionUpdate = null)
{
Expand All @@ -122,7 +124,22 @@ public static Envelope FromEvent(

if (attachments is not null)
{
items.AddRange(attachments.Select(EnvelopeItem.FromAttachment));
foreach (var attachment in attachments)
{
try
{
items.Add(EnvelopeItem.FromAttachment(attachment));
}
catch (Exception exception)
{
if (logger is null)
Copy link
Member

Choose a reason for hiding this comment

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

Turns out we continue to fail because no logger is passed when Debug=false

{
throw;
}

logger.LogError("Failed to add attachment: {0}.", exception, attachment.FileName);
}
}
}

if (sessionUpdate is not null)
Expand Down
2 changes: 1 addition & 1 deletion src/Sentry/SentryClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ private SentryId DoSendEvent(SentryEvent @event, Scope? scope)
return SentryId.Empty;
}

return CaptureEnvelope(Envelope.FromEvent(processedEvent, scope.Attachments, scope.SessionUpdate))
return CaptureEnvelope(Envelope.FromEvent(processedEvent, _options.DiagnosticLogger, scope.Attachments, scope.SessionUpdate))
? processedEvent.EventId
: SentryId.Empty;
}
Expand Down
55 changes: 51 additions & 4 deletions test/Sentry.Tests/Internals/Http/HttpTransportTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,52 @@ public async Task SendEnvelopeAsync_ItemRateLimit_DropsItem()
actualEnvelopeSerialized.Should().BeEquivalentTo(expectedEnvelopeSerialized);
}

[Fact]
public async Task SendEnvelopeAsync_AttachmentFail_DropsItem()
{
// Arrange
using var httpHandler = new RecordingHttpMessageHandler(
new FakeHttpMessageHandler());

var logger = new InMemoryDiagnosticLogger();

var httpTransport = new HttpTransport(
new SentryOptions
{
Dsn = DsnSamples.ValidDsnWithSecret,
MaxAttachmentSize = 1,
DiagnosticLogger = logger,
Debug = true
},
new HttpClient(httpHandler));

var attachment = new Attachment(
AttachmentType.Default,
new FileAttachmentContent("test1.txt"),
"test1.txt",
null);

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

// Act
await httpTransport.SendEnvelopeAsync(envelope);

var lastRequest = httpHandler.GetRequests().Last();
var actualEnvelopeSerialized = await lastRequest.Content.ReadAsStringAsync();

// Assert
// (the envelope should have only one item)

logger.Entries.Should().Contain(e =>
e.Message == "Failed to add attachment: {0}." &&
(string)e.Args[0] == "test1.txt");

actualEnvelopeSerialized.Should().NotContain("test2.txt");
}

[Fact]
public async Task SendEnvelopeAsync_AttachmentTooLarge_DropsItem()
{
Expand Down Expand Up @@ -358,6 +404,7 @@ public async Task SendEnvelopeAsync_AttachmentTooLarge_DropsItem()

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

// Act
Expand Down Expand Up @@ -399,7 +446,7 @@ public async Task SendEnvelopeAsync_ItemRateLimit_PromotesNextSessionWithSameId(
await httpTransport.SendEnvelopeAsync(Envelope.FromEvent(new SentryEvent()));

// Send session update with init=true
await httpTransport.SendEnvelopeAsync(Envelope.FromEvent(new SentryEvent(), null, session.CreateUpdate(true, DateTimeOffset.Now)));
await httpTransport.SendEnvelopeAsync(Envelope.FromEvent(new SentryEvent(), null, null, session.CreateUpdate(true, DateTimeOffset.Now)));

// Pretend the rate limit has already passed
foreach (var (category, _) in httpTransport.CategoryLimitResets)
Expand All @@ -410,7 +457,7 @@ public async Task SendEnvelopeAsync_ItemRateLimit_PromotesNextSessionWithSameId(
// Act

// Send another update with init=false (should get promoted)
await httpTransport.SendEnvelopeAsync(Envelope.FromEvent(new SentryEvent(), null, session.CreateUpdate(false, DateTimeOffset.Now)));
await httpTransport.SendEnvelopeAsync(Envelope.FromEvent(new SentryEvent(), null, null, session.CreateUpdate(false, DateTimeOffset.Now)));

var lastRequest = httpHandler.GetRequests().Last();
var actualEnvelopeSerialized = await lastRequest.Content.ReadAsStringAsync();
Expand Down Expand Up @@ -441,7 +488,7 @@ public async Task SendEnvelopeAsync_ItemRateLimit_DoesNotAffectNextSessionWithDi
await httpTransport.SendEnvelopeAsync(Envelope.FromEvent(new SentryEvent()));

// Send session update with init=true
await httpTransport.SendEnvelopeAsync(Envelope.FromEvent(new SentryEvent(), null, session.CreateUpdate(true, DateTimeOffset.Now)));
await httpTransport.SendEnvelopeAsync(Envelope.FromEvent(new SentryEvent(), null, null, session.CreateUpdate(true, DateTimeOffset.Now)));

// Pretend the rate limit has already passed
foreach (var (category, _) in httpTransport.CategoryLimitResets)
Expand All @@ -453,7 +500,7 @@ public async Task SendEnvelopeAsync_ItemRateLimit_DoesNotAffectNextSessionWithDi

// Send an update for different session with init=false (should NOT get promoted)
var nextSession = new Session("foo2", "bar2", "baz2");
await httpTransport.SendEnvelopeAsync(Envelope.FromEvent(new SentryEvent(), null, nextSession.CreateUpdate(false, DateTimeOffset.Now)));
await httpTransport.SendEnvelopeAsync(Envelope.FromEvent(new SentryEvent(), null, null, nextSession.CreateUpdate(false, DateTimeOffset.Now)));

var lastRequest = httpHandler.GetRequests().Last();
var actualEnvelopeSerialized = await lastRequest.Content.ReadAsStringAsync();
Expand Down
4 changes: 2 additions & 2 deletions test/Sentry.Tests/Protocol/Envelopes/EnvelopeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ public async Task Roundtrip_WithEvent_WithAttachment_Success()
"file.txt",
null);

using var envelope = Envelope.FromEvent(@event, new[] { attachment });
using var envelope = Envelope.FromEvent(@event, null, new[] { attachment });

using var stream = new MemoryStream();

Expand Down Expand Up @@ -428,7 +428,7 @@ public async Task Roundtrip_WithEvent_WithSession_Success()

var sessionUpdate = new Session("foo", "bar", "baz").CreateUpdate(false, DateTimeOffset.Now);

using var envelope = Envelope.FromEvent(@event, new[] { attachment }, sessionUpdate);
using var envelope = Envelope.FromEvent(@event, null, new[] { attachment }, sessionUpdate);

using var stream = new MemoryStream();

Expand Down