Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
19 changes: 18 additions & 1 deletion src/Sentry/Protocol/Envelopes/Envelope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,10 @@
/// <summary>
/// Creates an envelope that contains a single transaction.
/// </summary>
public static Envelope FromTransaction(SentryTransaction transaction)
public static Envelope FromTransaction(
SentryTransaction transaction,
IDiagnosticLogger? logger = null,
IReadOnlyCollection<SentryAttachment>? attachments = null)

Check warning on line 343 in src/Sentry/Protocol/Envelopes/Envelope.cs

View check run for this annotation

@sentry/warden / warden: code-review

Adding parameters to public FromTransaction is a binary-breaking change

`Envelope.FromTransaction` is a public method whose signature changes from `FromTransaction(SentryTransaction)` to `FromTransaction(SentryTransaction, IDiagnosticLogger?, IReadOnlyCollection<SentryAttachment>?)`. While source-compatible (the new parameters are optional), this is binary-breaking: assemblies compiled against the prior version will throw `MissingMethodException` at runtime when calling the original single-argument overload. Consumers that reference Sentry as a precompiled dependency would need to recompile. Consider preserving the original single-arg overload that delegates to the new method to maintain binary compatibility.
Comment thread
sentry-warden[bot] marked this conversation as resolved.
Outdated
{
var eventId = transaction.EventId;
var header = CreateHeader(eventId, transaction.DynamicSamplingContext);
Expand All @@ -358,6 +361,20 @@
}
}

if (attachments is not null)
{
foreach (var attachment in attachments)
{
if (attachment.IsNull())
{
logger?.LogWarning("Encountered a null attachment. Skipping.");
continue;
}

AddEnvelopeItemFromAttachment(items, attachment, logger);
}
}

return new Envelope(eventId, header, items);
}

Expand Down
20 changes: 20 additions & 0 deletions src/Sentry/SentryAttachment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ public class SentryAttachment
/// </summary>
public string? ContentType { get; }

/// <summary>
/// Whether the attachment should be added to transactions.
/// Defaults to <c>false</c>.
/// </summary>
public bool AddToTransactions { get; }

/// <summary>
/// Initializes an instance of <see cref="SentryAttachment"/>.
/// </summary>
Expand All @@ -73,10 +79,24 @@ public SentryAttachment(
IAttachmentContent content,
string fileName,
string? contentType)
: this(type, content, fileName, contentType, false)
{
}

/// <summary>
/// Initializes an instance of <see cref="SentryAttachment"/>.
/// </summary>
public SentryAttachment(
AttachmentType type,
IAttachmentContent content,
string fileName,
string? contentType,
bool addToTransactions)
{
Type = type;
Content = content;
FileName = fileName;
ContentType = contentType;
AddToTransactions = addToTransactions;
}
}
3 changes: 2 additions & 1 deletion src/Sentry/SentryClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,8 @@ public void CaptureTransaction(SentryTransaction transaction, Scope? scope, Sent
processedTransaction.Redact();
}

CaptureEnvelope(Envelope.FromTransaction(processedTransaction));
var attachments = hint.Attachments.Where(a => a.AddToTransactions).ToList();
CaptureEnvelope(Envelope.FromTransaction(processedTransaction, _options.DiagnosticLogger, attachments));
}

#if NET6_0_OR_GREATER
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,8 @@ namespace Sentry
public class SentryAttachment
{
public SentryAttachment(Sentry.AttachmentType type, Sentry.IAttachmentContent content, string fileName, string? contentType) { }
public SentryAttachment(Sentry.AttachmentType type, Sentry.IAttachmentContent content, string fileName, string? contentType, bool addToTransactions) { }
public bool AddToTransactions { get; }
public Sentry.IAttachmentContent Content { get; }
public string? ContentType { get; }
public string FileName { get; }
Expand Down Expand Up @@ -2007,7 +2009,7 @@ namespace Sentry.Protocol.Envelopes
public static Sentry.Protocol.Envelopes.Envelope FromEvent(Sentry.SentryEvent @event, Sentry.Extensibility.IDiagnosticLogger? logger = null, System.Collections.Generic.IReadOnlyCollection<Sentry.SentryAttachment>? attachments = null, Sentry.SessionUpdate? sessionUpdate = null) { }
public static Sentry.Protocol.Envelopes.Envelope FromFeedback(Sentry.SentryEvent @event, Sentry.Extensibility.IDiagnosticLogger? logger = null, System.Collections.Generic.IReadOnlyCollection<Sentry.SentryAttachment>? attachments = null, Sentry.SessionUpdate? sessionUpdate = null) { }
public static Sentry.Protocol.Envelopes.Envelope FromSession(Sentry.SessionUpdate sessionUpdate) { }
public static Sentry.Protocol.Envelopes.Envelope FromTransaction(Sentry.SentryTransaction transaction) { }
public static Sentry.Protocol.Envelopes.Envelope FromTransaction(Sentry.SentryTransaction transaction, Sentry.Extensibility.IDiagnosticLogger? logger = null, System.Collections.Generic.IReadOnlyCollection<Sentry.SentryAttachment>? attachments = null) { }
}
public sealed class EnvelopeItem : Sentry.Protocol.Envelopes.ISerializable, System.IDisposable
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,8 @@ namespace Sentry
public class SentryAttachment
{
public SentryAttachment(Sentry.AttachmentType type, Sentry.IAttachmentContent content, string fileName, string? contentType) { }
public SentryAttachment(Sentry.AttachmentType type, Sentry.IAttachmentContent content, string fileName, string? contentType, bool addToTransactions) { }
public bool AddToTransactions { get; }
public Sentry.IAttachmentContent Content { get; }
public string? ContentType { get; }
public string FileName { get; }
Expand Down Expand Up @@ -2007,7 +2009,7 @@ namespace Sentry.Protocol.Envelopes
public static Sentry.Protocol.Envelopes.Envelope FromEvent(Sentry.SentryEvent @event, Sentry.Extensibility.IDiagnosticLogger? logger = null, System.Collections.Generic.IReadOnlyCollection<Sentry.SentryAttachment>? attachments = null, Sentry.SessionUpdate? sessionUpdate = null) { }
public static Sentry.Protocol.Envelopes.Envelope FromFeedback(Sentry.SentryEvent @event, Sentry.Extensibility.IDiagnosticLogger? logger = null, System.Collections.Generic.IReadOnlyCollection<Sentry.SentryAttachment>? attachments = null, Sentry.SessionUpdate? sessionUpdate = null) { }
public static Sentry.Protocol.Envelopes.Envelope FromSession(Sentry.SessionUpdate sessionUpdate) { }
public static Sentry.Protocol.Envelopes.Envelope FromTransaction(Sentry.SentryTransaction transaction) { }
public static Sentry.Protocol.Envelopes.Envelope FromTransaction(Sentry.SentryTransaction transaction, Sentry.Extensibility.IDiagnosticLogger? logger = null, System.Collections.Generic.IReadOnlyCollection<Sentry.SentryAttachment>? attachments = null) { }
}
public sealed class EnvelopeItem : Sentry.Protocol.Envelopes.ISerializable, System.IDisposable
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,8 @@ namespace Sentry
public class SentryAttachment
{
public SentryAttachment(Sentry.AttachmentType type, Sentry.IAttachmentContent content, string fileName, string? contentType) { }
public SentryAttachment(Sentry.AttachmentType type, Sentry.IAttachmentContent content, string fileName, string? contentType, bool addToTransactions) { }
public bool AddToTransactions { get; }
public Sentry.IAttachmentContent Content { get; }
public string? ContentType { get; }
public string FileName { get; }
Expand Down Expand Up @@ -2007,7 +2009,7 @@ namespace Sentry.Protocol.Envelopes
public static Sentry.Protocol.Envelopes.Envelope FromEvent(Sentry.SentryEvent @event, Sentry.Extensibility.IDiagnosticLogger? logger = null, System.Collections.Generic.IReadOnlyCollection<Sentry.SentryAttachment>? attachments = null, Sentry.SessionUpdate? sessionUpdate = null) { }
public static Sentry.Protocol.Envelopes.Envelope FromFeedback(Sentry.SentryEvent @event, Sentry.Extensibility.IDiagnosticLogger? logger = null, System.Collections.Generic.IReadOnlyCollection<Sentry.SentryAttachment>? attachments = null, Sentry.SessionUpdate? sessionUpdate = null) { }
public static Sentry.Protocol.Envelopes.Envelope FromSession(Sentry.SessionUpdate sessionUpdate) { }
public static Sentry.Protocol.Envelopes.Envelope FromTransaction(Sentry.SentryTransaction transaction) { }
public static Sentry.Protocol.Envelopes.Envelope FromTransaction(Sentry.SentryTransaction transaction, Sentry.Extensibility.IDiagnosticLogger? logger = null, System.Collections.Generic.IReadOnlyCollection<Sentry.SentryAttachment>? attachments = null) { }
}
public sealed class EnvelopeItem : Sentry.Protocol.Envelopes.ISerializable, System.IDisposable
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,8 @@ namespace Sentry
public class SentryAttachment
{
public SentryAttachment(Sentry.AttachmentType type, Sentry.IAttachmentContent content, string fileName, string? contentType) { }
public SentryAttachment(Sentry.AttachmentType type, Sentry.IAttachmentContent content, string fileName, string? contentType, bool addToTransactions) { }
public bool AddToTransactions { get; }
public Sentry.IAttachmentContent Content { get; }
public string? ContentType { get; }
public string FileName { get; }
Expand Down Expand Up @@ -1983,7 +1985,7 @@ namespace Sentry.Protocol.Envelopes
public static Sentry.Protocol.Envelopes.Envelope FromEvent(Sentry.SentryEvent @event, Sentry.Extensibility.IDiagnosticLogger? logger = null, System.Collections.Generic.IReadOnlyCollection<Sentry.SentryAttachment>? attachments = null, Sentry.SessionUpdate? sessionUpdate = null) { }
public static Sentry.Protocol.Envelopes.Envelope FromFeedback(Sentry.SentryEvent @event, Sentry.Extensibility.IDiagnosticLogger? logger = null, System.Collections.Generic.IReadOnlyCollection<Sentry.SentryAttachment>? attachments = null, Sentry.SessionUpdate? sessionUpdate = null) { }
public static Sentry.Protocol.Envelopes.Envelope FromSession(Sentry.SessionUpdate sessionUpdate) { }
public static Sentry.Protocol.Envelopes.Envelope FromTransaction(Sentry.SentryTransaction transaction) { }
public static Sentry.Protocol.Envelopes.Envelope FromTransaction(Sentry.SentryTransaction transaction, Sentry.Extensibility.IDiagnosticLogger? logger = null, System.Collections.Generic.IReadOnlyCollection<Sentry.SentryAttachment>? attachments = null) { }
}
public sealed class EnvelopeItem : Sentry.Protocol.Envelopes.ISerializable, System.IDisposable
{
Expand Down
75 changes: 75 additions & 0 deletions test/Sentry.Tests/SentryClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1294,6 +1294,81 @@ public void CaptureTransaction_ScopeContainsAttachments_GetAppliedToHint()
hint.Attachments.Should().Contain(attachments);
}

[Fact]
public void CaptureTransaction_AttachmentWithAddToTransactionsTrue_IncludedInEnvelope()
{
// Arrange
var transaction = new SentryTransaction("name", "operation")
{
IsSampled = true,
EndTimestamp = DateTimeOffset.Now
};
var attachment = new SentryAttachment(
AttachmentType.Default,
new StreamAttachmentContent(new MemoryStream(new byte[] { 1 })),
"include.txt",
null,
addToTransactions: true);
var scope = new Scope(_fixture.SentryOptions);
scope.AddAttachment(attachment);
var sut = _fixture.GetSut();

// Act
sut.CaptureTransaction(transaction, scope, null);

// Assert
sut.Worker.Received(1).EnqueueEnvelope(Arg.Is<Envelope>(envelope =>
envelope.Items.Count(item => item.TryGetType() == "attachment") == 1));
}

[Fact]
public void CaptureTransaction_AttachmentWithAddToTransactionsFalse_ExcludedFromEnvelope()
{
// Arrange
var transaction = new SentryTransaction("name", "operation")
{
IsSampled = true,
EndTimestamp = DateTimeOffset.Now
};
var scope = new Scope(_fixture.SentryOptions);
scope.AddAttachment(AttachmentHelper.FakeAttachment("exclude.txt")); // default: AddToTransactions = false
var sut = _fixture.GetSut();

// Act
sut.CaptureTransaction(transaction, scope, null);

// Assert
sut.Worker.Received(1).EnqueueEnvelope(Arg.Is<Envelope>(envelope =>
envelope.Items.Count(item => item.TryGetType() == "attachment") == 0));
}

[Fact]
public void CaptureTransaction_MixedAttachments_OnlyAddToTransactionsOnesIncluded()
{
// Arrange
var transaction = new SentryTransaction("name", "operation")
{
IsSampled = true,
EndTimestamp = DateTimeOffset.Now
};
var scope = new Scope(_fixture.SentryOptions);
scope.AddAttachment(AttachmentHelper.FakeAttachment("excluded.txt")); // AddToTransactions = false
scope.AddAttachment(new SentryAttachment(
AttachmentType.Default,
new StreamAttachmentContent(new MemoryStream(new byte[] { 1 })),
"included.txt",
null,
addToTransactions: true));
var sut = _fixture.GetSut();

// Act
sut.CaptureTransaction(transaction, scope, null);

// Assert
sut.Worker.Received(1).EnqueueEnvelope(Arg.Is<Envelope>(envelope =>
envelope.Items.Count(item => item.TryGetType() == "attachment") == 1));
}

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