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

Use SentryId for ISession.Id #1052

Merged
merged 5 commits into from
Jun 14, 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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Changes

- Use SentryId for ISession.Id ([#1052](https://github.com/getsentry/sentry-dotnet/pull/1052))

## 3.6.0-alpha.1

### Features
Expand Down
2 changes: 1 addition & 1 deletion src/Sentry/ISession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public interface ISession
/// <summary>
/// Session auto-generated ID.
/// </summary>
string Id { get; }
SentryId Id { get; }

/// <summary>
/// Session distinct ID.
Expand Down
5 changes: 3 additions & 2 deletions src/Sentry/Internal/Http/HttpTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ internal class HttpTransport : ITransport

// Keep track of last discarded session init so that we can promote the next update.
// We only track one because session updates are ordered.
// Using string instead of SentryId here so that we can use Interlocked.Exchange(...).
private string? _lastDiscardedSessionInitId;

internal const string DefaultErrorMessage = "No message";
Expand Down Expand Up @@ -65,7 +66,7 @@ private Envelope ProcessEnvelope(Envelope envelope, DateTimeOffset instant)
// Check if session update with init=true
if (envelopeItem.Payload is JsonSerializable {Source: SessionUpdate {IsInitial: true} discardedSessionUpdate})
{
_lastDiscardedSessionInitId = discardedSessionUpdate.Id;
_lastDiscardedSessionInitId = discardedSessionUpdate.Id.ToString();

_options.DiagnosticLogger?.LogDebug(
"Discarded envelope item containing initial session update (SID: {0}).",
Expand All @@ -92,7 +93,7 @@ private Envelope ProcessEnvelope(Envelope envelope, DateTimeOffset instant)
// If it's a session update (not discarded) with init=false, check if it continues
// a session with previously dropped init and, if so, promote this update to init=true.
if (envelopeItem.Payload is JsonSerializable {Source: SessionUpdate {IsInitial: false} sessionUpdate} &&
string.Equals(sessionUpdate.Id, Interlocked.Exchange(ref _lastDiscardedSessionInitId, null),
string.Equals(sessionUpdate.Id.ToString(), Interlocked.Exchange(ref _lastDiscardedSessionInitId, null),
StringComparison.Ordinal))
{
var modifiedEnvelopeItem = new EnvelopeItem(
Expand Down
6 changes: 3 additions & 3 deletions src/Sentry/Session.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Sentry
public class Session : ISession
{
/// <inheritdoc />
public string Id { get; }
public SentryId Id { get; }

/// <inheritdoc />
public string? DistinctId { get; }
Expand Down Expand Up @@ -43,7 +43,7 @@ public class Session : ISession
private int _sequenceNumber = -1;

internal Session(
string id,
SentryId id,
string? distinctId,
DateTimeOffset startTimestamp,
string release,
Expand All @@ -65,7 +65,7 @@ internal Session(
/// </summary>
public Session(string? distinctId, string release, string? environment)
: this(
Guid.NewGuid().ToString(),
SentryId.Create(),
distinctId,
DateTimeOffset.Now,
release,
Expand Down
8 changes: 4 additions & 4 deletions src/Sentry/SessionUpdate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Sentry
public class SessionUpdate : ISession, IJsonSerializable
{
/// <inheritdoc />
public string Id { get; }
public SentryId Id { get; }

/// <inheritdoc />
public string? DistinctId { get; }
Expand Down Expand Up @@ -61,7 +61,7 @@ public class SessionUpdate : ISession, IJsonSerializable
/// Initializes a new instance of <see cref="SessionUpdate"/>.
/// </summary>
public SessionUpdate(
string id,
SentryId id,
string? distinctId,
DateTimeOffset startTimestamp,
string release,
Expand Down Expand Up @@ -121,7 +121,7 @@ public void WriteTo(Utf8JsonWriter writer)
{
writer.WriteStartObject();

writer.WriteString("sid", Id);
writer.WriteSerializable("sid", Id);

if (!string.IsNullOrWhiteSpace(DistinctId))
{
Expand Down Expand Up @@ -176,7 +176,7 @@ public void WriteTo(Utf8JsonWriter writer)
/// </summary>
public static SessionUpdate FromJson(JsonElement json)
{
var id = json.GetProperty("sid").GetStringOrThrow();
var id = json.GetProperty("sid").GetStringOrThrow().Pipe(SentryId.Parse);
var distinctId = json.GetPropertyOrNull("did")?.GetString();
var startTimestamp = json.GetProperty("started").GetDateTimeOffset();
var release = json.GetProperty("attrs").GetProperty("release").GetStringOrThrow();
Expand Down
2 changes: 1 addition & 1 deletion test/Sentry.Tests/GlobalSessionManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void StartSession_ReleaseSet_CreatesNewSession()

// Assert
sessionUpdate.Should().NotBeNull();
sessionUpdate?.Id.Should().NotBeNullOrWhiteSpace();
sessionUpdate?.Id.Should().NotBe(SentryId.Empty);
sessionUpdate?.Release.Should().NotBeNullOrWhiteSpace();
}

Expand Down
6 changes: 3 additions & 3 deletions test/Sentry.Tests/SessionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public void Serialization_Session_Success()
{
// Arrange
var session = new Session(
"foo",
SentryId.Parse("75302ac48a024bde9a3b3734a82e36c8"),
"bar",
DateTimeOffset.Parse("2020-01-01T00:00:00+00:00", CultureInfo.InvariantCulture),
"release123",
Expand Down Expand Up @@ -41,7 +41,7 @@ public void Serialization_Session_Success()
// Assert
json.Should().Be(
"{" +
"\"sid\":\"foo\"," +
"\"sid\":\"75302ac48a024bde9a3b3734a82e36c8\"," +
"\"did\":\"bar\"," +
"\"init\":true," +
"\"started\":\"2020-01-01T00:00:00+00:00\"," +
Expand All @@ -65,7 +65,7 @@ public void CreateUpdate_IncrementsSequenceNumber()
{
// Arrange
var session = new Session(
"foo",
SentryId.Parse("75302ac48a024bde9a3b3734a82e36c8"),
"bar",
DateTimeOffset.Parse("2020-01-01T00:00:00+00:00", CultureInfo.InvariantCulture),
"release123",
Expand Down