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 Distribution properties to go along with Release info #1851

Merged
merged 15 commits into from
Aug 17, 2022
Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- Enable Scope Sync for iOS ([#1834](https://github.com/getsentry/sentry-dotnet/pull/1834))
- Add API for deliberately crashing an app ([#1842](https://github.com/getsentry/sentry-dotnet/pull/1842))
- Add Mac Catalyst target ([#1848](https://github.com/getsentry/sentry-dotnet/pull/1848))
- Add `Distribution` properties ([#1851](https://github.com/getsentry/sentry-dotnet/pull/1851))
- Add and configure options for the iOS SDK ([#1849](https://github.com/getsentry/sentry-dotnet/pull/1849))

### Fixes
Expand Down
3 changes: 3 additions & 0 deletions src/Sentry/Internal/Enricher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public void Apply(IEventLike eventLike)
// Release
eventLike.Release ??= _options.SettingLocator.GetRelease();

// Distribution
eventLike.WithDistribution(_ => _.Distribution ??= _options.Distribution);

// Environment
eventLike.Environment ??= _options.SettingLocator.GetEnvironment();

Expand Down
30 changes: 30 additions & 0 deletions src/Sentry/Internal/IHasDistribution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;

namespace Sentry.Internal
{
// NOTE: We only need this interface because IEventLike is public and thus we can't
// add more properties without introducing a potentially breaking change.
// TODO: Move the Distribution property to IEventLike in the next major release.

internal interface IHasDistribution
{
/// <summary>
/// The release distribution of the application.
/// </summary>
public string? Distribution { get; set; }
}

internal static class HasDistributionExtensions
{
internal static string? GetDistribution(this IEventLike obj) =>
(obj as IHasDistribution)?.Distribution;

internal static void WithDistribution(this IEventLike obj, Action<IHasDistribution> action)
{
if (obj is IHasDistribution hasDistribution)
{
action.Invoke(hasDistribution);
}
}
}
}
14 changes: 5 additions & 9 deletions src/Sentry/Internal/MainSentryEventProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ internal class MainSentryEventProcessor : ISentryEventProcessor

internal string? Release => _options.SettingLocator.GetRelease();

internal string? Distribution => _options.Distribution;

public MainSentryEventProcessor(
SentryOptions options,
Func<ISentryStackTraceFactory> sentryStackTraceFactoryAccessor)
Expand Down Expand Up @@ -83,15 +85,9 @@ public SentryEvent Process(SentryEvent @event)
}
}

if (@event.Level == null)
{
@event.Level = SentryLevel.Error;
}

if (@event.Release == null)
{
@event.Release = Release;
}
@event.Level ??= SentryLevel.Error;
@event.Release ??= Release;
@event.Distribution ??= Distribution;

if (@event.Exception == null)
{
Expand Down
22 changes: 14 additions & 8 deletions src/Sentry/Platforms/Android/SentryOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@ public partial class SentryOptions
/// <summary>
/// Exposes additional options for the Android platform.
/// </summary>
public AndroidOptions Android { get; } = new();
public AndroidOptions Android { get; }

/// <summary>
/// Provides additional options for the Android platform.
/// </summary>
public class AndroidOptions
{
internal AndroidOptions() { }
private readonly SentryOptions _options;

internal AndroidOptions(SentryOptions options)
{
_options = options;
}

// ---------- From SentryAndroidOptions.java ----------

Expand Down Expand Up @@ -153,13 +158,14 @@ public class AndroidOptions
public TimeSpan ConnectionTimeout { get; set; } = TimeSpan.FromSeconds(5);

/// <summary>
/// Gets or sets the distribution of the application.
/// The distribution of the application, associated with the release set in <see cref="Release"/>.
/// </summary>
/// <remarks>
/// See "dist" in https://develop.sentry.dev/sdk/event-payloads/#optional-attributes
/// </remarks>
// TODO: Should we have this property on the main SentryOptions (with Release and Environment)?
public string? Distribution { get; set; } = null;
[Obsolete("Use SentryOptions.Distribution instead. This property will be removed in a future version.")]
public string? Distribution
{
get => _options.Distribution;
set => _options.Distribution = value;
}

/// <summary>
/// Gets or sets a value that indicates if the NDK (Android Native Development Kit) is enabled.
Expand Down
2 changes: 1 addition & 1 deletion src/Sentry/Platforms/Android/SentrySdk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ private static void InitSentryAndroidSdk(SentryOptions options)
o.AttachStacktrace = options.AttachStacktrace;
o.Debug = options.Debug;
o.DiagnosticLevel = options.DiagnosticLevel.ToJavaSentryLevel();
o.Dist = options.Distribution;
o.Dsn = options.Dsn;
o.EnableAutoSessionTracking = options.AutoSessionTracking;
o.Environment = options.Environment;
Expand Down Expand Up @@ -148,7 +149,6 @@ private static void InitSentryAndroidSdk(SentryOptions options)
// These options are in Java.SentryOptions but not ours
o.AttachThreads = options.Android.AttachThreads;
o.ConnectionTimeoutMillis = (int)options.Android.ConnectionTimeout.TotalMilliseconds;
o.Dist = options.Android.Distribution;
o.EnableNdk = options.Android.EnableNdk;
o.EnableShutdownHook = options.Android.EnableShutdownHook;
o.EnableUncaughtExceptionHandler = options.Android.EnableUncaughtExceptionHandler;
Expand Down
22 changes: 14 additions & 8 deletions src/Sentry/Platforms/iOS/SentryOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@ public partial class SentryOptions
/// Exposes additional options for the iOS platform.
/// </summary>
// ReSharper disable once InconsistentNaming
public IosOptions iOS { get; } = new();
public IosOptions iOS { get; }

/// <summary>
/// Provides additional options for the iOS platform.
/// </summary>
public class IosOptions
{
internal IosOptions() { }
private readonly SentryOptions _options;

internal IosOptions(SentryOptions options)
{
_options = options;
}

// ---------- From Cocoa's SentryOptions.h ----------

Expand Down Expand Up @@ -52,13 +57,14 @@ public class IosOptions
public TimeSpan IdleTimeout { get; set; } = TimeSpan.FromSeconds(3);

/// <summary>
/// Gets or sets the distribution of the application.
/// The distribution of the application, associated with the release set in <see cref="Release"/>.
/// </summary>
/// <remarks>
/// See "dist" in https://develop.sentry.dev/sdk/event-payloads/#optional-attributes
/// </remarks>
// TODO: Should we have this property on the main SentryOptions (with Release and Environment)?
public string? Distribution { get; set; } = null;
[Obsolete("Use SentryOptions.Distribution instead. This property will be removed in a future version.")]
public string? Distribution
{
get => _options.Distribution;
set => _options.Distribution = value;
}

/// <summary>
/// When enabled, the SDK tracks when the application stops responding for a specific amount of
Expand Down
2 changes: 1 addition & 1 deletion src/Sentry/Platforms/iOS/SentrySdk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ private static void InitSentryCocoaSdk(SentryOptions options)
cocoaOptions.AttachScreenshot = options.iOS.AttachScreenshot;
cocoaOptions.AppHangTimeoutInterval = options.iOS.AppHangTimeoutInterval.TotalSeconds;
cocoaOptions.IdleTimeout = options.iOS.IdleTimeout.TotalSeconds;
cocoaOptions.Dist = options.iOS.Distribution;
cocoaOptions.Dist = options.Distribution;
cocoaOptions.EnableAppHangTracking = options.iOS.EnableAppHangTracking;
cocoaOptions.EnableAutoBreadcrumbTracking = options.iOS.EnableAutoBreadcrumbTracking;
cocoaOptions.EnableAutoPerformanceTracking = options.iOS.EnableAutoPerformanceTracking;
Expand Down
7 changes: 6 additions & 1 deletion src/Sentry/Scope.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.Internal;
using Sentry.Internal.Extensions;

namespace Sentry
Expand All @@ -15,7 +16,7 @@ namespace Sentry
/// Scope data is sent together with any event captured
/// during the lifetime of the scope.
/// </remarks>
public class Scope : IEventLike
public class Scope : IEventLike, IHasDistribution
{
internal SentryOptions Options { get; }

Expand Down Expand Up @@ -133,6 +134,9 @@ public User User
/// <inheritdoc />
public string? Release { get; set; }

/// <inheritdoc />
public string? Distribution { get; set; }

/// <inheritdoc />
public string? Environment { get; set; }

Expand Down Expand Up @@ -363,6 +367,7 @@ public void Apply(IEventLike other)

other.Platform ??= Platform;
other.Release ??= Release;
other.WithDistribution(_ => _.Distribution ??= Distribution);
other.Environment ??= Environment;
other.TransactionName ??= TransactionName;
other.Level ??= Level;
Expand Down
9 changes: 8 additions & 1 deletion src/Sentry/SentryEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using System.Text.Json;
using Sentry.Extensibility;
using Sentry.Internal;
using Sentry.Internal.Extensions;
using Sentry.Protocol;

Expand All @@ -15,7 +16,7 @@ namespace Sentry
/// </summary>
/// <seealso href="https://develop.sentry.dev/sdk/event-payloads/" />
[DebuggerDisplay("{GetType().Name,nq}: {" + nameof(EventId) + ",nq}")]
public sealed class SentryEvent : IEventLike, IJsonSerializable
public sealed class SentryEvent : IEventLike, IJsonSerializable, IHasDistribution
{
private IDictionary<string, string>? _modules;

Expand Down Expand Up @@ -73,6 +74,9 @@ public sealed class SentryEvent : IEventLike, IJsonSerializable
/// <inheritdoc />
public string? Release { get; set; }

/// <inheritdoc />
public string? Distribution { get; set; }

internal SentryValues<SentryException>? SentryExceptionValues { get; set; }

/// <summary>
Expand Down Expand Up @@ -234,6 +238,7 @@ public void WriteTo(Utf8JsonWriter writer, IDiagnosticLogger? logger)
writer.WriteStringIfNotWhiteSpace("platform", Platform);
writer.WriteStringIfNotWhiteSpace("server_name", ServerName);
writer.WriteStringIfNotWhiteSpace("release", Release);
writer.WriteStringIfNotWhiteSpace("dist", Distribution);
writer.WriteSerializableIfNotNull("exception", SentryExceptionValues, logger);
writer.WriteSerializableIfNotNull("threads", SentryThreadValues, logger);
writer.WriteStringIfNotWhiteSpace("level", Level?.ToString().ToLowerInvariant());
Expand Down Expand Up @@ -276,6 +281,7 @@ internal static SentryEvent FromJson(JsonElement json, Exception? exception)
var platform = json.GetPropertyOrNull("platform")?.GetString();
var serverName = json.GetPropertyOrNull("server_name")?.GetString();
var release = json.GetPropertyOrNull("release")?.GetString();
var distribution = json.GetPropertyOrNull("dist")?.GetString();
var exceptionValues = json.GetPropertyOrNull("exception")?.GetPropertyOrNull("values")?.EnumerateArray().Select(SentryException.FromJson).ToList().Pipe(v => new SentryValues<SentryException>(v));
var threadValues = json.GetPropertyOrNull("threads")?.GetPropertyOrNull("values")?.EnumerateArray().Select(SentryThread.FromJson).ToList().Pipe(v => new SentryValues<SentryThread>(v));
var level = json.GetPropertyOrNull("level")?.GetString()?.ParseEnum<SentryLevel>();
Expand All @@ -301,6 +307,7 @@ internal static SentryEvent FromJson(JsonElement json, Exception? exception)
Platform = platform,
ServerName = serverName,
Release = release,
Distribution = distribution,
SentryExceptionValues = exceptionValues,
SentryThreadValues = threadValues,
DebugImages = images,
Expand Down
24 changes: 23 additions & 1 deletion src/Sentry/SentryOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,23 @@ public bool IsGlobalModeEnabled
/// <seealso href="https://docs.sentry.io/platforms/dotnet/configuration/releases/"/>
public string? Release { get; set; }

/// <summary>
/// The distribution of the application, associated with the release set in <see cref="Release"/>.
/// </summary>
/// <example>
/// 22
/// 14G60
/// </example>
/// <remarks>
/// Distributions are used to disambiguate build or deployment variants of the same release of
/// an application. For example, it can be the build number of an XCode (iOS) build, or the version
/// code of an Android build.
/// A distribution can be set under any circumstances, and is passed along to Sentry if provided.
/// However, they are generally relevant only for mobile application scenarios.
/// </remarks>
/// <seealso href="https://develop.sentry.dev/sdk/event-payloads/#optional-attributes"/>
public string? Distribution { get; set; }

/// <summary>
/// The environment the application is running
/// </summary>
Expand Down Expand Up @@ -672,6 +689,12 @@ public SentryOptions()
#endif
};

#if ANDROID
Android = new AndroidOptions(this);
#elif IOS || MACCATALYST
iOS = new IosOptions(this);
#endif

InAppExclude = new[] {
"System.",
"Mono.",
Expand Down Expand Up @@ -720,7 +743,6 @@ public SentryOptions()
#else
InAppInclude = Array.Empty<string>();
#endif

}
}
}
10 changes: 9 additions & 1 deletion src/Sentry/Transaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Text.Json;
using Sentry.Extensibility;
using Sentry.Internal;
using Sentry.Internal.Extensions;

namespace Sentry
Expand All @@ -11,7 +12,7 @@ namespace Sentry
/// <summary>
/// Sentry performance transaction.
/// </summary>
public class Transaction : ITransactionData, IJsonSerializable
public class Transaction : ITransactionData, IJsonSerializable, IHasDistribution
{
/// <summary>
/// Transaction's event ID.
Expand Down Expand Up @@ -58,6 +59,9 @@ public SentryId TraceId
/// <inheritdoc />
public string? Release { get; set; }

/// <inheritdoc />
public string? Distribution { get; set; }

/// <inheritdoc />
public DateTimeOffset StartTimestamp { get; private set; } = DateTimeOffset.UtcNow;

Expand Down Expand Up @@ -212,6 +216,7 @@ public Transaction(ITransaction tracer)
Operation = tracer.Operation;
Platform = tracer.Platform;
Release = tracer.Release;
Distribution = tracer.GetDistribution();
StartTimestamp = tracer.StartTimestamp;
EndTimestamp = tracer.EndTimestamp;
Description = tracer.Description;
Expand Down Expand Up @@ -261,6 +266,7 @@ public void WriteTo(Utf8JsonWriter writer, IDiagnosticLogger? logger)
writer.WriteStringIfNotWhiteSpace("level", Level?.ToString().ToLowerInvariant());
writer.WriteStringIfNotWhiteSpace("platform", Platform);
writer.WriteStringIfNotWhiteSpace("release", Release);
writer.WriteStringIfNotWhiteSpace("dist", Distribution);
writer.WriteStringIfNotWhiteSpace("transaction", Name);
writer.WriteString("start_timestamp", StartTimestamp);
writer.WriteStringIfNotNull("timestamp", EndTimestamp);
Expand Down Expand Up @@ -290,6 +296,7 @@ public static Transaction FromJson(JsonElement json)
var level = json.GetPropertyOrNull("level")?.GetString()?.ParseEnum<SentryLevel>();
var platform = json.GetPropertyOrNull("platform")?.GetString();
var release = json.GetPropertyOrNull("release")?.GetString();
var distribution = json.GetPropertyOrNull("dist")?.GetString();
var request = json.GetPropertyOrNull("request")?.Pipe(Request.FromJson);
var contexts = json.GetPropertyOrNull("contexts")?.Pipe(Contexts.FromJson);
var user = json.GetPropertyOrNull("user")?.Pipe(User.FromJson);
Expand All @@ -312,6 +319,7 @@ public static Transaction FromJson(JsonElement json)
Level = level,
Platform = platform,
Release = release,
Distribution = distribution,
_request = request,
Contexts = contexts ?? new(),
_user = user,
Expand Down
5 changes: 4 additions & 1 deletion src/Sentry/TransactionTracer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Sentry
/// <summary>
/// Transaction tracer.
/// </summary>
public class TransactionTracer : ITransaction
public class TransactionTracer : ITransaction, IHasDistribution
{
private readonly IHub _hub;
private readonly SentryStopwatch _stopwatch = SentryStopwatch.StartNew();
Expand Down Expand Up @@ -50,6 +50,9 @@ public SentryId TraceId
/// <inheritdoc />
public string? Release { get; set; }

/// <inheritdoc />
public string? Distribution { get; set; }

/// <inheritdoc />
public DateTimeOffset StartTimestamp => _stopwatch.StartDateTimeOffset;

Expand Down
Loading