Skip to content

Commit

Permalink
Merge pull request #8 from lucas-zimerman/staging
Browse files Browse the repository at this point in the history
v3.0.0
[Breaking Changes]
-Merged SessionSdk and TracingSdk into ContribSentrySdk.
-Enhanced the support to .NetCore for tracing.
-Bugfixes.
  • Loading branch information
lucas-zimerman authored Sep 8, 2020
2 parents 472986f + 5c4021a commit e2c593c
Show file tree
Hide file tree
Showing 99 changed files with 2,242 additions and 863 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

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

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

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="2.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore" Version="2.1.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="Sentry.AspNetCore" Version="2.1.4" />
<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="..\ContribSentry.AspNetCore\ContribSentry.AspNetCore.csproj" />
<ProjectReference Include="..\ContribSentry.Testing\ContribSentry.Testing.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using ContribSentry.AspNetCore.Internals;
using ContribSentry.Internals;
using Sentry;
using Xunit;

namespace ContribSentry.AspNetCore.Tests
{
public class ContribSentryNetCoreSdkIntegrationTests
{
[Fact]
public void Http_Tracking_Is_Set_On_Register_To_Main_Tracking_Service()
{
try
{
var integration = new ContribSentryNetCoreSdkIntegration();
integration.Register(null,new SentryOptions());
Assert.True(ContribSentrySdk.IsEnabled);
Assert.True(ContribSentrySdk.IsTracingSdkEnabled);
Assert.Equal(typeof(ContribSentryTracingService), ContribSentrySdk.TracingService.GetType());
var service = (ContribSentryTracingService)ContribSentrySdk.TracingService;
Assert.Equal(typeof(HttpContextTracking), service.Tracker.GetType());
}
finally
{
ContribSentrySdk.Close();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using ContribSentry;
using ContribSentry.AspNetCore.Internals;
using ContribSentry.Testing;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Sentry.AspNetCore;
using Sentry.Extensibility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace ContribSentry.AspNetCore.Tests.Internals
{
public class HttpContextTrackingTests : ContribSentrySdkTestFixture
{
protected Action<SentryAspNetCoreOptions> Configure;

public HttpContextTrackingTests()
{
ConfigureWehHost = builder =>
{
_ = builder.UseSentry(options =>
{
options.Dsn = DsnHelper.ValidDsnWithoutSecret;
});
};
}

private static HttpContextTracking _tracker;

private static List<KeyValuePair<string, int>> _trackerList;

private Action<HttpContext> TrackerIdMiddleware = (context) => {
var id = _tracker.ReserveNewId();
_tracker.AssociateId(id);
_trackerList.Add(_tracker._contextReference.Last(p => p.Value == id));
};

[Fact]
public async Task Tracker_Receives_Unique_KeyPair_ForEach_Request()
{
_tracker = new HttpContextTracking();
_trackerList = new List<KeyValuePair<string, int>>();
Build(TrackerIdMiddleware);
_ = await HttpClient.GetAsync("");
_ = await HttpClient.GetAsync("");
_ = await HttpClient.GetAsync("");
_ = await HttpClient.GetAsync("");
_ = await HttpClient.GetAsync("");
foreach(var valuePair in _trackerList)
{
Assert.Equal(1, _trackerList.Count(p => p.Key == valuePair.Key));
Assert.Equal(1, _trackerList.Count(p => p.Value == valuePair.Value));
}
foreach (var valuePair in _trackerList)
{
Assert.True(_tracker.UnsetId(valuePair.Value));
}
}
}
}
25 changes: 25 additions & 0 deletions ContribSentry.AspNetCore.Tests/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace ContribSentry.AspNetCore.Tests
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

public void ConfigureServices(IServiceCollection services)
{
}

public void Configure(IApplicationBuilder app)
{

}
}
}
14 changes: 14 additions & 0 deletions ContribSentry.AspNetCore.Tests/UnitTest1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using Xunit;

namespace ContribSentry.AspNetCore.Tests
{
public class UnitTest1
{
[Fact]
public void Test1()
{

}
}
}
15 changes: 15 additions & 0 deletions ContribSentry.AspNetCore.Tests/allsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"Sentry": {
"Dsn": "https://[email protected]/1",
"IncludeRequestPayload": true,
"SendDefaultPii": true,
"IncludeActivityData": true,
"MinimumBreadcrumbLevel": "Error",
"MinimumEventLevel": "Critical",
"InitializeSdk": "false",
"MaxBreadcrumbs": "999",
"SampleRate": "1",
"Release": "7f5d9a1",
"Environment": "Staging"
}
}
28 changes: 28 additions & 0 deletions ContribSentry.AspNetCore/ContribSentry.AspNetCore.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<Version>3.0.0</Version>
<Description>.Net Core Addon for ContribSentry</Description>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/lucas-zimerman/sentry-dotnet-performance-addon</PackageProjectUrl>
<PackageIcon>nugget-logo.png</PackageIcon>
<RepositoryUrl>https://github.com/lucas-zimerman/sentry-dotnet-performance-addon</RepositoryUrl>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ContribSentry\ContribSentry.csproj" />
</ItemGroup>

<ItemGroup>
<None Include="..\.assets\nugget-logo.png">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
</ItemGroup>

</Project>
34 changes: 34 additions & 0 deletions ContribSentry.AspNetCore/ContribSentryNetCoreSdkIntegration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using ContribSentry.AspNetCore.Internals;
using ContribSentry.Extensibility;
using Sentry;
using Sentry.Integrations;

namespace ContribSentry.AspNetCore
{
public class ContribSentryNetCoreSdkIntegration : ISdkIntegration
{
internal ContribSentryOptions _options;
public ContribSentryNetCoreSdkIntegration()
{
_options = new ContribSentryOptions(sessionEnable:false);
}

public ContribSentryNetCoreSdkIntegration(ContribSentryOptions options)
{
_options = new ContribSentryOptions(options.TransactionEnabled, false)
{
DistinctId = options.DistinctId,
RegisterTracingBreadcrumb = options.RegisterTracingBreadcrumb,
TracesSampleRate = options.TracesSampleRate,
};
_options.SetTracingService(options.TracingService);
}

public void Register(IHub hub, SentryOptions options)
{
_options.TrackingIdMethod = new HttpContextTracking();
var integration = new ContribSentrySdkIntegration(_options);
integration.Register(hub, options);
}
}
}
71 changes: 71 additions & 0 deletions ContribSentry.AspNetCore/Internals/HttpContextTracking.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using ContribSentry.Interface;
using Sentry;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ContribSentry.AspNetCore.Internals
{
internal class HttpContextTracking : ITracingContextTrackingId
{
private readonly string _requestIdField = "RequestId";

internal List<KeyValuePair<string, int>> _contextReference = new List<KeyValuePair<string, int>>();

private object _lock = new object();
private int _value;
private int InternalNewId()
{
lock (_lock)
{
int i = _value++;
return i;
}
}

private string GetrequestId()
{
string id = null;
SentrySdk.ConfigureScope((scope) => { id = scope.Tags[_requestIdField]; });
return id;
}

public int? GetId()
{
var id = GetrequestId();
if (id == null)
return null;
return _contextReference.FirstOrDefault(p => p.Key == id).Value;
}

public bool IdRegistered(int id)
{
return _contextReference.Any(v => v.Value == id);
}

/// <summary>
/// Link the integer unique id to the Request Id
/// </summary>
/// <param name="id"></param>
public void AssociateId(int id)
{
_contextReference.Add(new KeyValuePair<string, int>(GetrequestId(), id));
}

public int ReserveNewId()
{
return InternalNewId();
}

public bool UnsetId(int id)
{
return _contextReference.Remove(_contextReference.First(p => p.Value == id));
}

public Task WithIsolatedTracing(Func<Task> test, int id)
{
return test.Invoke();
}
}
}
3 changes: 3 additions & 0 deletions ContribSentry.AspNetCore/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("ContribSentry.AspNetCore.Tests")]
21 changes: 21 additions & 0 deletions ContribSentry.SessionTest/ContribSentry.SessionTest.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="..\ContribSentry\ContribSentry.csproj" />
</ItemGroup>

</Project>
13 changes: 13 additions & 0 deletions ContribSentry.SessionTest/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 ContribSentry.SessionTest/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 ContribSentry;
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 (!ContribSentrySdk.IsEnabled)
{
var integration = new ContribSentrySdkIntegration(new ContribSentryOptions() { GlobalSessionMode = true });
integration.Register(null, new SentryOptions() { Release = TestRelease, Environment = TestEnvironment, Dsn = new Dsn(DsnHelper.ValidDsnWithoutSecret) });
}
}
}
}
Loading

0 comments on commit e2c593c

Please sign in to comment.