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

Add ReportAssembliesMode in favor of ReportAssemblies #1079

Merged
merged 7 commits into from
Jun 23, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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 @@ -8,6 +8,7 @@

- Implement pause & resume session ([#1069](https://github.com/getsentry/sentry-dotnet/pull/1069))
- Add auto session tracking ([#1068](https://github.com/getsentry/sentry-dotnet/pull/1068))
- Add ReportAssembliesMode in favor of ReportAssemblies ([#1079](https://github.com/getsentry/sentry-dotnet/pull/1079))

## 3.6.0-alpha.2

Expand Down
2 changes: 2 additions & 0 deletions src/Sentry.Serilog/SentrySinkExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,9 @@ public static void ConfigureSentrySerilogOptions(

if (reportAssemblies.HasValue)
{
#pragma warning disable CS0618 // Type or member is obsolete
sentrySerilogOptions.ReportAssemblies = reportAssemblies.Value;
#pragma warning restore CS0618 // Type or member is obsolete
}

if (deduplicateMode.HasValue)
Expand Down
26 changes: 22 additions & 4 deletions src/Sentry/Internal/MainSentryEventProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using System.Threading;
using Sentry.Extensibility;
using Sentry.Reflection;

namespace Sentry.Internal
{
Expand Down Expand Up @@ -96,19 +97,36 @@ public SentryEvent Process(SentryEvent @event)
}
}

if (_options.ReportAssemblies)
if (_options.ReportAssembliesMode != ReportAssembliesMode.None)
{
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
if (assembly.IsDynamic)
if (assembly is null ||
assembly.IsDynamic)
{
continue;
}

var asmName = assembly.GetName();
if (asmName.Name is not null && asmName.Version is not null)
if (asmName.Name is not null)
{
@event.Modules[asmName.Name] = asmName.Version.ToString();
var asmVersion = string.Empty;

switch (_options.ReportAssembliesMode)
{
case ReportAssembliesMode.Version:
asmVersion = asmName.Version?.ToString() ?? string.Empty;
break;

case ReportAssembliesMode.InformationalVersion:
asmVersion = assembly.GetNameAndVersion().Version ?? string.Empty;
break;

default:
throw new ArgumentOutOfRangeException(nameof(_options.ReportAssembliesMode), $"Report assemblies mode '{_options.ReportAssembliesMode}' is not yet supported");
}

@event.Modules[asmName.Name] = asmVersion;
bruno-garcia marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down
25 changes: 21 additions & 4 deletions src/Sentry/Reflection/AssemblyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,28 @@ public static SdkVersion GetNameAndVersion(this Assembly asm)
{
var asmName = asm.GetName();
var name = asmName.Name;
var version =
asm.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion
?? asmName?.Version?.ToString();
string? asmVersion = null;

return new SdkVersion {Name = name, Version = version};
// Note: on full .NET FX, checking the AssemblyInformationalVersionAttribute could throw an exception, therefore
// this method uses a try/catch to make sure this method always returns a value
try
{
asmVersion = asm.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion;
}
catch
{
}

// Note: even though the informational version could be "invalid" (e.g. string.Empty), it should
// be used for versioning and the software should not fallback to the assembly version string.
//
// See https://github.com/getsentry/sentry-dotnet/pull/1079#issuecomment-866992216
if (asmVersion is null)
bruno-garcia marked this conversation as resolved.
Show resolved Hide resolved
{
asmVersion = asmName?.Version?.ToString();
}

return new SdkVersion {Name = name, Version = asmVersion };
}
}
}
24 changes: 24 additions & 0 deletions src/Sentry/ReportAssembliesMode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace Sentry
{
/// <summary>
/// Possible modes for reporting the assemblies.
/// </summary>
public enum ReportAssembliesMode
{
/// <summary>
/// Don't report any assemblies.
/// </summary>
None,

/// <summary>
/// Report assemblies and use the assembly version to determine the version.
/// </summary>
Version,

/// <summary>
/// Report assemblies and prefer the informational assembly version to determine the version. If
/// the informational assembly version is not available, fall back to the assembly version.
/// </summary>
InformationalVersion
}
}
13 changes: 12 additions & 1 deletion src/Sentry/SentryOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,18 @@ public IDiagnosticLogger? DiagnosticLogger
/// <summary>
/// Whether or not to include referenced assemblies in each event sent to sentry. Defaults to <see langword="true"/>.
/// </summary>
public bool ReportAssemblies { get; set; } = true;
[Obsolete("Use ReportAssembliesMode instead", error : false)]
public bool ReportAssemblies
{
// Note: note marking this as error to prevent breaking changes, but this is now a wrapper around ReportAssembliesMode
get { return ReportAssembliesMode != ReportAssembliesMode.None; }
set { ReportAssembliesMode = value ? ReportAssembliesMode.Version : ReportAssembliesMode.None; }
}

/// <summary>
/// What mode to use for reporting referenced assemblies in each event sent to sentry. Defaults to <see cref="ReportAssembliesMode.Version"/>.
/// </summary>
public ReportAssembliesMode ReportAssembliesMode { get; set; } = ReportAssembliesMode.Version;

/// <summary>
/// What modes to use for event automatic deduplication
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ public void ConfigureSentrySerilogOptions_WithMultipleParameters_MakesAppropriat
// Compare individual properties
Assert.Equal(_fixture.SendDefaultPii, sut.SendDefaultPii);
Assert.Equal(_fixture.DecompressionMethods, sut.DecompressionMethods);
#pragma warning disable CS0618 // Type or member is obsolete
Assert.Equal(_fixture.ReportAssemblies, sut.ReportAssemblies);
#pragma warning restore CS0618 // Type or member is obsolete
Assert.Equal(_fixture.SampleRate, sut.SampleRate);
}

Expand Down Expand Up @@ -125,7 +127,9 @@ public void ConfigureSentrySerilogOptions_WithAllParameters_MakesAppropriateChan
Assert.Equal(_fixture.RequestBodyCompressionBuffered, sut.RequestBodyCompressionBuffered);
Assert.Equal(_fixture.Debug, sut.Debug);
Assert.Equal(_fixture.DiagnosticLevel, sut.DiagnosticLevel);
#pragma warning disable CS0618 // Type or member is obsolete
Assert.Equal(_fixture.ReportAssemblies, sut.ReportAssemblies);
#pragma warning restore CS0618 // Type or member is obsolete
Assert.Equal(_fixture.DeduplicateMode, sut.DeduplicateMode);
Assert.Equal(_fixture.InitializeSdk, sut.InitializeSdk);
Assert.Equal(_fixture.MinimumEventLevel, sut.MinimumEventLevel);
Expand Down
39 changes: 39 additions & 0 deletions test/Sentry.Tests/Internals/MainSentryEventProcessorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,14 +312,53 @@ public void Process_Modules_NotEmpty()
[Fact]
public void Process_Modules_IsEmpty_WhenSpecified()
{
// Note: this property is obsolete, test is kept for backwards compatibility check
#pragma warning disable CS0618 // Type or member is obsolete
_fixture.SentryOptions.ReportAssemblies = false;
#pragma warning restore CS0618 // Type or member is obsolete

var sut = _fixture.GetSut();
var evt = new SentryEvent();
_ = sut.Process(evt);

Assert.Empty(evt.Modules);
}

[Fact]
public void Process_Modules_ReportAssembliesMode_None()
{
_fixture.SentryOptions.ReportAssembliesMode = ReportAssembliesMode.None;
var sut = _fixture.GetSut();
var evt = new SentryEvent();
_ = sut.Process(evt);

Assert.Empty(evt.Modules);
}

[Fact]
public void Process_Modules_ReportAssembliesMode_Version()
{
_fixture.SentryOptions.ReportAssembliesMode = ReportAssembliesMode.Version;
var sut = _fixture.GetSut();
var evt = new SentryEvent();
_ = sut.Process(evt);

// Don't allow any assembly with a + (sha commit is added to informational version)
Assert.DoesNotContain(evt.Modules, x => x.Value.Contains("+"));
}

[Fact]
public void Process_Modules_ReportAssembliesMode_InformationalVersion()
{
_fixture.SentryOptions.ReportAssembliesMode = ReportAssembliesMode.InformationalVersion;
var sut = _fixture.GetSut();
var evt = new SentryEvent();
_ = sut.Process(evt);

// Ensure at least 1 assembly with a + (sha commit is added to informational version)
Assert.Contains(evt.Modules, x => x.Value.Contains("+"));
}

[Fact]
public void Process_SdkNameAndVersion_ToDefault()
{
Expand Down