Skip to content

Commit

Permalink
Add User.Segment property (#1920)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattjohnsonpint authored Sep 15, 2022
1 parent 6fdbe57 commit 168f647
Show file tree
Hide file tree
Showing 16 changed files with 126 additions and 100 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
- `SentryOptions.AttachStackTrace` is now enabled by default. ([#1907](https://github.com/getsentry/sentry-dotnet/pull/1907))
- Update Sentry Android SDK to version 6.4.1 ([#1911](https://github.com/getsentry/sentry-dotnet/pull/1911))
- Update Sentry Cocoa SDK to version 7.24.1 ([#1912](https://github.com/getsentry/sentry-dotnet/pull/1912))
- Add TransactionNameSource annotation ([#1910](https://github.com/getsentry/sentry-dotnet/pull/1910))
- Add `TransactionNameSource` annotation ([#1910](https://github.com/getsentry/sentry-dotnet/pull/1910))
- Use URL path in transaction names instead of "Unknown Route" ([#1919](https://github.com/getsentry/sentry-dotnet/pull/1919))
- Add `User.Segment` property ([#1920](https://github.com/getsentry/sentry-dotnet/pull/1920))

## Fixes

Expand Down
5 changes: 5 additions & 0 deletions src/Sentry.NLog/SentryNLogUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public class SentryNLogUser
/// </summary>
public Layout? IpAddress { get; set; }

/// <summary>
/// A <see cref="Layout"/> used to dynamically specify the segment of a user for a sentry event.
/// </summary>
public Layout? Segment { get; set; }

/// <summary>
/// Additional information about the user
/// </summary>
Expand Down
5 changes: 3 additions & 2 deletions src/Sentry.NLog/SentryTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -449,10 +449,11 @@ private void InnerWrite(LogEventInfo logEvent)

var user = new User
{
Email = User.Email?.Render(logEvent),
Id = User.Id?.Render(logEvent),
IpAddress = User.IpAddress?.Render(logEvent),
Username = User.Username?.Render(logEvent),
Email = User.Email?.Render(logEvent),
IpAddress = User.IpAddress?.Render(logEvent),
Segment = User.Segment?.Render(logEvent)
};

if (User.Other?.Count > 0)
Expand Down
7 changes: 1 addition & 6 deletions src/Sentry/IEventLike.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,7 @@ public static class EventLikeExtensions
/// <summary>
/// Whether a <see cref="User"/> has been set to the object with any of its fields non null.
/// </summary>
public static bool HasUser(this IEventLike eventLike)
=> eventLike.User.Email is not null
|| eventLike.User.Id is not null
|| eventLike.User.Username is not null
|| eventLike.User.InternalOther?.Count > 0
|| eventLike.User.IpAddress is not null;
public static bool HasUser(this IEventLike eventLike) => eventLike.User.HasAnyData();

/// <summary>
/// Sets the fingerprint to the object.
Expand Down
104 changes: 58 additions & 46 deletions src/Sentry/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,50 +15,55 @@ public sealed class User : IJsonSerializable
{
internal Action<User>? PropertyChanged { get; set; }

private string? _id;
private string? _username;
private string? _email;
private string? _ipAddress;
private string? _segment;
private IDictionary<string, string>? _other;

/// <summary>
/// The email address of the user.
/// The unique ID of the user.
/// </summary>
/// <value>
/// The user's email address.
/// </value>
public string? Email
public string? Id
{
get => _email;
get => _id;
set
{
_email = value;
_id = value;
PropertyChanged?.Invoke(this);
}
}

private string? _id;

/// <summary>
/// The unique ID of the user.
/// The username of the user.
/// </summary>
/// <value>
/// The unique identifier.
/// </value>
public string? Id
public string? Username
{
get => _id;
get => _username;
set
{
_id = value;
_username = value;
PropertyChanged?.Invoke(this);
}
}

private string? _ipAddress;
/// <summary>
/// The email address of the user.
/// </summary>
public string? Email
{
get => _email;
set
{
_email = value;
PropertyChanged?.Invoke(this);
}
}

/// <summary>
/// The IP of the user.
/// The IP address of the user.
/// </summary>
/// <value>
/// The user's IP address.
/// </value>
public string? IpAddress
{
get => _ipAddress;
Expand All @@ -69,33 +74,30 @@ public string? IpAddress
}
}

private string? _username;

/// <summary>
/// The username of the user.
/// The segment the user belongs to.
/// </summary>
/// <value>
/// The user's username.
/// </value>
public string? Username
public string? Segment
{
get => _username;
get => _segment;
set
{
_username = value;
_segment = value;
PropertyChanged?.Invoke(this);
}
}

internal IDictionary<string, string>? InternalOther { get; private set; }

/// <summary>
/// Additional information about the user.
/// </summary>
public IDictionary<string, string> Other
{
get => InternalOther ??= new Dictionary<string, string>();
set => InternalOther = value;
get => _other ??= new Dictionary<string, string>();
set
{
_other = value;
PropertyChanged?.Invoke(this);
}
}

/// <summary>
Expand All @@ -105,9 +107,7 @@ public IDictionary<string, string> Other
public User Clone()
{
var user = new User();

CopyTo(user);

return user;
}

Expand All @@ -118,26 +118,36 @@ internal void CopyTo(User? user)
return;
}

user.Email ??= Email;
user.Id ??= Id;
user.Username ??= Username;
user.Email ??= Email;
user.IpAddress ??= IpAddress;
user.Segment ??= Segment;

user.InternalOther ??= InternalOther?.ToDictionary(
user._other ??= _other?.ToDictionary(
entry => entry.Key,
entry => entry.Value);
}

internal bool HasAnyData() =>
Id is not null ||
Username is not null ||
Email is not null ||
IpAddress is not null ||
Segment is not null ||
_other?.Count > 0;

/// <inheritdoc />
public void WriteTo(Utf8JsonWriter writer, IDiagnosticLogger? _)
{
writer.WriteStartObject();

writer.WriteStringIfNotWhiteSpace("email", Email);
writer.WriteStringIfNotWhiteSpace("id", Id);
writer.WriteStringIfNotWhiteSpace("ip_address", IpAddress);
writer.WriteStringIfNotWhiteSpace("username", Username);
writer.WriteStringDictionaryIfNotEmpty("other", InternalOther!);
writer.WriteStringIfNotWhiteSpace("email", Email);
writer.WriteStringIfNotWhiteSpace("ip_address", IpAddress);
writer.WriteStringIfNotWhiteSpace("segment", Segment);
writer.WriteStringDictionaryIfNotEmpty("other", _other!);

writer.WriteEndObject();
}
Expand All @@ -147,19 +157,21 @@ public void WriteTo(Utf8JsonWriter writer, IDiagnosticLogger? _)
/// </summary>
public static User FromJson(JsonElement json)
{
var email = json.GetPropertyOrNull("email")?.GetString();
var id = json.GetPropertyOrNull("id")?.GetString();
var ip = json.GetPropertyOrNull("ip_address")?.GetString();
var username = json.GetPropertyOrNull("username")?.GetString();
var email = json.GetPropertyOrNull("email")?.GetString();
var ip = json.GetPropertyOrNull("ip_address")?.GetString();
var segment = json.GetPropertyOrNull("segment")?.GetString();
var other = json.GetPropertyOrNull("other")?.GetStringDictionaryOrNull();

return new User
{
Email = email,
Id = id,
IpAddress = ip,
Username = username,
InternalOther = other?.WhereNotNullValue().ToDictionary()
Email = email,
IpAddress = ip,
Segment = segment,
_other = other?.WhereNotNullValue().ToDictionary()
};
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[assembly: System.CLSCompliant(true)]
[assembly: System.CLSCompliant(true)]
namespace NLog
{
public static class ConfigurationExtensions
Expand Down Expand Up @@ -49,6 +49,7 @@ namespace Sentry.NLog
public NLog.Layouts.Layout? IpAddress { get; set; }
[NLog.Config.ArrayParameter(typeof(NLog.Targets.TargetPropertyWithContext?), "other")]
public System.Collections.Generic.IList<NLog.Targets.TargetPropertyWithContext>? Other { get; }
public NLog.Layouts.Layout? Segment { get; set; }
public NLog.Layouts.Layout? Username { get; set; }
}
[NLog.Targets.Target("Sentry")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ namespace Sentry.NLog
public NLog.Layouts.Layout? IpAddress { get; set; }
[NLog.Config.ArrayParameter(typeof(NLog.Targets.TargetPropertyWithContext?), "other")]
public System.Collections.Generic.IList<NLog.Targets.TargetPropertyWithContext>? Other { get; }
public NLog.Layouts.Layout? Segment { get; set; }
public NLog.Layouts.Layout? Username { get; set; }
}
[NLog.Targets.Target("Sentry")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ namespace Sentry.NLog
public NLog.Layouts.Layout? IpAddress { get; set; }
[NLog.Config.ArrayParameter(typeof(NLog.Targets.TargetPropertyWithContext?), "other")]
public System.Collections.Generic.IList<NLog.Targets.TargetPropertyWithContext>? Other { get; }
public NLog.Layouts.Layout? Segment { get; set; }
public NLog.Layouts.Layout? Username { get; set; }
}
[NLog.Targets.Target("Sentry")]
Expand Down
5 changes: 3 additions & 2 deletions test/Sentry.NLog.Tests/IntegrationTests.Simple.verified.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@
Request: {},
Contexts: {},
User: {
Email: ,
Id: myId,
IpAddress: ,
Username: ,
Email: ,
IpAddress: ,
Segment: ,
Other: {
mood: joyous
}
Expand Down
8 changes: 2 additions & 6 deletions test/Sentry.NLog.Tests/IntegrationTests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using NLog;
using NLog.Config;
using NLog.Targets;
using Sentry.Testing;

namespace Sentry.NLog.Tests;
namespace Sentry.NLog.Tests;

[UsesVerify]
public class IntegrationTests
Expand Down Expand Up @@ -31,6 +26,7 @@ public Task Simple()
Username = "${scopeproperty:item=username}",
Email = "${scopeproperty:item=email}",
IpAddress = "${scopeproperty:item=ipAddress}",
Segment = "${scopeproperty:item=segment}",
Other =
{
new TargetPropertyWithContext("mood", "joyous")
Expand Down
16 changes: 8 additions & 8 deletions test/Sentry.Serilog.Tests/IntegrationTests.Simple.verified.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
Request: {},
Contexts: {},
User: {
IpAddress: {{auto}},
Username: TheUserName
Username: TheUserName,
IpAddress: {{auto}}
},
Environment: production,
Extra: {
Expand Down Expand Up @@ -64,8 +64,8 @@
Request: {},
Contexts: {},
User: {
IpAddress: {{auto}},
Username: TheUserName
Username: TheUserName,
IpAddress: {{auto}}
},
Environment: production,
Breadcrumbs: [
Expand Down Expand Up @@ -109,8 +109,8 @@
Request: {},
Contexts: {},
User: {
IpAddress: {{auto}},
Username: TheUserName
Username: TheUserName,
IpAddress: {{auto}}
},
Environment: production,
Breadcrumbs: [
Expand Down Expand Up @@ -183,8 +183,8 @@
Request: {},
Contexts: {},
User: {
IpAddress: {{auto}},
Username: TheUserName
Username: TheUserName,
IpAddress: {{auto}}
},
Environment: production,
Breadcrumbs: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,7 @@ namespace Sentry
public string? Id { get; set; }
public string? IpAddress { get; set; }
public System.Collections.Generic.IDictionary<string, string> Other { get; set; }
public string? Segment { get; set; }
public string? Username { get; set; }
public Sentry.User Clone() { }
public void WriteTo(System.Text.Json.Utf8JsonWriter writer, Sentry.Extensibility.IDiagnosticLogger? _) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,7 @@ namespace Sentry
public string? Id { get; set; }
public string? IpAddress { get; set; }
public System.Collections.Generic.IDictionary<string, string> Other { get; set; }
public string? Segment { get; set; }
public string? Username { get; set; }
public Sentry.User Clone() { }
public void WriteTo(System.Text.Json.Utf8JsonWriter writer, Sentry.Extensibility.IDiagnosticLogger? _) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,7 @@ namespace Sentry
public string? Id { get; set; }
public string? IpAddress { get; set; }
public System.Collections.Generic.IDictionary<string, string> Other { get; set; }
public string? Segment { get; set; }
public string? Username { get; set; }
public Sentry.User Clone() { }
public void WriteTo(System.Text.Json.Utf8JsonWriter writer, Sentry.Extensibility.IDiagnosticLogger? _) { }
Expand Down
Loading

0 comments on commit 168f647

Please sign in to comment.