Skip to content

Commit

Permalink
Feat/session addon (#2)
Browse files Browse the repository at this point in the history
Added Session Sdk
Update README.md
  • Loading branch information
lucas-zimerman committed Sep 3, 2020
1 parent 9ca5b65 commit d84656c
Show file tree
Hide file tree
Showing 34 changed files with 1,092 additions and 56 deletions.
Binary file added .assets/nugget-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
57 changes: 3 additions & 54 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,55 +1,4 @@
# sentry dotnet transaction addon V2.0.0
### Unnoficial addon for adding Peformance support to Sentry-dotnet
# SentryContrib
### Unnoficial addons for Sentry.Net SDK

# Status
Currently in Alpha, not all features were implemented and you may experience errors or lose of Performance events.

Official Docs: https://docs.sentry.io/performance-monitoring/getting-started/

# Configuration

To initialize the performance addon you'll need add the SentryTracingSdkIntegration to your SentryOptions integration.
```C#
sentryOptions.AddIntegration(new SentryTracingSdkIntegration());
```
you'll of course need to initialize SentrySdk giving passing the SentryOptions where you added the Integration.

# Usage
You can start/finish a Transaction by creating an transaction Object or by 'using'
```C#
var transaction = SentryTracingSDK.StartTransaction( name );// return a new transaction.
var child = transaction.StartChild( name );// return a new child
... code to be measured
child.Finish();// finishes the child
// You can add as many childs as you wish on a transaction
transaction.Finish();// finishes and sends the transaction
```
```C#
using(var transaction = SentryTracingSDK.StartTransaction( name ))
{
var child = transaction.StartChild( name );// return a new child
... code to be measured
child.Finish();// finishes the child
// You can add as many childs as you wish on a transaction
}
```

You can also start a child anywhere in the code, as long as there's an active Isolated Transaction, else
the child will be discarted
```C#
using(var child = SentryTracingSDK.StartChild( url, Post ))
{
... your http request here
child.Finish(httpstatuscode);// child finished with the current status code
}
```

To isolate a Transaction if you would like to start a child by not referencing the Tracing object you'll need to run the following code
```C#
var transaction = SentryTracingSDK.StartTransaction( name );
await transaction.IsolateTracking(async ()=>{
// your code here
});
transaction.Finish();
```
That way, if the code SentryTracingSDK.StartChild is called and the stack trace is inside of an isolated Transaction block, the Span will be attached to the Isolated Transaction.
For more information check the Wiki
13 changes: 13 additions & 0 deletions SessionSdk.Test/Helpers/DsnHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace SessionSdk.Helpers
{
/// <summary>
/// Based on DsnSamples from Sentry.Net
/// </summary>
public class DsnHelper
{
/// <summary>
/// Sentry has dropped the use of secrets
/// </summary>
public const string ValidDsnWithoutSecret = "https://[email protected]:65535/2147483647";
}
}
23 changes: 23 additions & 0 deletions SessionSdk.Test/Initializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Text;
using Sentry;
using sentry_dotnet_health_addon;
using SessionSdk.Helpers;

namespace SessionSdk.Test
{
public static class Initializer
{
public const string TestRelease = "--1";
public const string TestEnvironment = "test";
public static void Init()
{
if (!SentrySessionSdk.IsEnabled)
{
var integration = new SentrySessionSdkIntegration(new SentrySessionOptions() { GlobalHubMode = true });
integration.Register(null, new SentryOptions() { Release = TestRelease, Environment = TestEnvironment, Dsn = new Dsn(DsnHelper.ValidDsnWithoutSecret) });
}
}
}
}
21 changes: 21 additions & 0 deletions SessionSdk.Test/SessionSdk.Test.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="Moq" Version="4.14.5" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="coverlet.collector" Version="1.2.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\sentry-dotnet-health-addon\SentryContrib.SessionSdk.csproj" />
</ItemGroup>

</Project>
80 changes: 80 additions & 0 deletions SessionSdk.Test/SessionTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using Moq;
using Sentry.Protocol;
using sentry_dotnet_health_addon;
using sentry_dotnet_health_addon.Enums;
using sentry_dotnet_health_addon.Internals;
using System;
using System.Threading.Tasks;
using Xunit;

namespace SessionSdk.Test
{
public class SessionTest
{
[Fact]
public void Session_Start_Should_Set_Default_Parameters()
{
var user = new User()
{
Id = "123",
IpAddress = "127.0.0.1"
};
var session = CreateSession(user);
Assert.Equal(user.IpAddress,session.Attributes.IpAddress);
Assert.Equal(Initializer.TestRelease, session.Attributes.Release);
Assert.Equal(Initializer.TestEnvironment, session.Attributes.Environment);
Assert.Null(session.DistinctId);
Assert.NotNull(session.Init);
Assert.Equal(SessionState.Ok, session.Status);
Assert.NotNull(session.SessionId);
}

[Fact]
public async Task Session_End_Status_Is_Exited_And_Timestamp_Higher_Than_Start()
{
var user = new User()
{
Id = "123",
IpAddress = "127.0.0.1"
};
var session = CreateSession(user);
await Task.Delay(10);
session.End();
Assert.Equal(SessionState.Exited, session.Status);
Assert.True(session.Timestamp > session.Started);
}

[Fact]
private void Session_Crashed_When_Ended_Has_Status_Crashed()
{
var user = new User()
{
Id = "123",
IpAddress = "127.0.0.1"
};
var session = CreateSession(user);
session.Status = SessionState.Crashed;
session.End(null);
Assert.Equal(SessionState.Crashed, session.Status);
}

[Fact]
private void Session_End_With_TimeStamp_Has_Timestamp()
{
var user = new User()
{
Id = "123",
IpAddress = "127.0.0.1"
};
var session = CreateSession(user);
var date = DateTime.Now.AddSeconds(5);
session.End(date);
Assert.Equal(date, session.Timestamp);
}

private Session CreateSession(User user)
{
return new Session(null, user, Initializer.TestEnvironment, Initializer.TestRelease);
}
}
}
11 changes: 11 additions & 0 deletions sentry-dotnet-health-addon/Enums/ESentryType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace sentry_dotnet_health_addon.Enums
{
public enum ESentryType
{
Session,
Event,
Attachment,
Transaction,
Unknown
}
}
10 changes: 10 additions & 0 deletions sentry-dotnet-health-addon/Enums/SessionState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace sentry_dotnet_health_addon.Enums
{
public enum SessionState
{
Ok,
Exited,
Crashed,
Abnormal // not currently used in this SDK.
}
}
36 changes: 36 additions & 0 deletions sentry-dotnet-health-addon/Extensibility/DisabledSession.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using sentry_dotnet_health_addon.Enums;
using System;

namespace sentry_dotnet_health_addon.Extensibility
{
public class DisabledSession : ISession
{
public static DisabledSession Instance = new DisabledSession();
public DateTime? Started { get; private set; }

public DateTime? Timestamp { get; private set; }

public int? ErrorCount { get; private set; }

public string DistinctId { get; private set; }

public Guid SessionId { get; private set; }

public bool? Init { get; private set; }

public SessionState Status { get; set; }

public long? Sequence { get; private set; }

public long? Duration { get; private set; }

public SessionAttributes Attributes { get; private set; }

public void End(DateTime? timestamp) {}

public void RegisterError() {}

public DisabledSession() {}

}
}
21 changes: 21 additions & 0 deletions sentry-dotnet-health-addon/Extensions/DateTimeExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace sentry_dotnet_health_addon.Extensions
{
internal static class DateTimeExtensions
{
private static readonly DateTime _epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

internal static double TotalUtcSeconds(this DateTime date)
{
return (date - _epoch).TotalSeconds;
}

internal static double TotalUtcMiliseconds(this DateTime date)
{
return (date - _epoch).TotalMilliseconds;
}
}
}
12 changes: 12 additions & 0 deletions sentry-dotnet-health-addon/Extensions/DsnExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Sentry;

namespace sentry_dotnet_health_addon.Extensions
{
internal static class DsnExtensions
{
internal static string GetTracingUrl(this Dsn dsn)
{
return $"{dsn.SentryUri.Scheme}://{dsn.SentryUri.Host}/api/{dsn.ProjectId}/envelope/?sentry_key={dsn.PublicKey}&sentry_version=7";
}
}
}
23 changes: 23 additions & 0 deletions sentry-dotnet-health-addon/Extensions/ESentryTypeExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using sentry_dotnet_health_addon.Enums;
using System;
using System.Collections.Generic;
using System.Text;

namespace sentry_dotnet_health_addon.Extensions
{
internal static class ESentryTypeExtensions
{
public static string ConvertString(this ESentryType type)
{
if (type == ESentryType.Attachment)
return "attachment";
if (type == ESentryType.Event)
return "event";
if (type == ESentryType.Session)
return "session";
if (type == ESentryType.Transaction)
return "transaction";
return "unknown";
}
}
}
20 changes: 20 additions & 0 deletions sentry-dotnet-health-addon/Extensions/SessionStateExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using sentry_dotnet_health_addon.Enums;

namespace sentry_dotnet_health_addon.Extensions
{
public static class SessionStateExtensions
{
public static string ConvertString(this SessionState state)
{
if (state == SessionState.Ok)
return "ok";
if (state == SessionState.Exited)
return "exited";
if (state == SessionState.Crashed)
return "crashed";
if( state == SessionState.Abnormal)
return "abnormal";
return null;
}
}
}
21 changes: 21 additions & 0 deletions sentry-dotnet-health-addon/ISession.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using sentry_dotnet_health_addon.Enums;
using System;

namespace sentry_dotnet_health_addon
{
public interface ISession
{
DateTime? Started { get; }
DateTime? Timestamp { get; }
int? ErrorCount { get; }
string DistinctId { get; }
Guid SessionId { get; }
bool? Init { get; }
SessionState Status { get; set; }
long? Sequence { get; }
long? Duration { get; }
SessionAttributes Attributes { get; }
void End(DateTime? timestamp);
void RegisterError();
}
}
Loading

0 comments on commit d84656c

Please sign in to comment.