-
-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from getsentry/client/basic-types
Client contexts and sample
- Loading branch information
Showing
34 changed files
with
1,902 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace Sentry.Samples.Console.Basic | ||
{ | ||
static class Program | ||
{ | ||
static void Main() | ||
{ | ||
var sentry = new HttpSentryClient(); | ||
// This exception is captured and sent to Sentry | ||
throw null; | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
samples/Sentry.Samples.Console.Basic/Sentry.Samples.Console.Basic.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp2.0</TargetFramework> | ||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="../../src/Sentry/Sentry.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace Sentry | ||
{ | ||
internal static class Constants | ||
{ | ||
public const string DsnEnvironmentVariable = "SENTRY_DSN"; | ||
public const string DisableSdkDsnValue = "disabled"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
using System; | ||
using System.Diagnostics; | ||
|
||
namespace Sentry | ||
{ | ||
/// <summary> | ||
/// The Data Source Name of a given project in Sentry. | ||
/// </summary> | ||
/// <remarks> | ||
/// <see href="https://docs.sentry.io/quickstart/#configure-the-dsn"/> | ||
/// </remarks> | ||
public class Dsn | ||
{ | ||
private readonly string _dsn; | ||
|
||
/// <summary> | ||
/// The project ID which the authenticated user is bound to. | ||
/// </summary> | ||
public string ProjectId { get; } | ||
/// <summary> | ||
/// An optional path of which Sentry is hosted | ||
/// </summary> | ||
public string Path { get; } | ||
/// <summary> | ||
/// The optional secret key to authenticate the SDK. | ||
/// </summary> | ||
public string SecretKey { get; } | ||
/// <summary> | ||
/// The required public key to authenticate the SDK. | ||
/// </summary> | ||
public string PublicKey { get; } | ||
/// <summary> | ||
/// The URI used to communicate with Sentry | ||
/// </summary> | ||
public Uri SentryUri { get; } | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="Dsn"/> class. | ||
/// </summary> | ||
/// <param name="dsn">The DSN in the format: {PROTOCOL}://{PUBLIC_KEY}@{HOST}/{PATH}{PROJECT_ID}</param> | ||
/// <remarks> | ||
/// A legacy DSN containing a secret will also be accepted: {PROTOCOL}://{PUBLIC_KEY}:{SECRET_KEY}@{HOST}/{PATH}{PROJECT_ID} | ||
/// </remarks> | ||
public Dsn(string dsn) | ||
{ | ||
var parsed = Parse(dsn, throwOnError: true); | ||
Debug.Assert(parsed != null, "Parse should throw instead of returning null!"); | ||
|
||
var (projectId, path, secretKey, publicKey, sentryUri) = parsed.Value; | ||
|
||
_dsn = dsn; | ||
ProjectId = projectId; | ||
Path = path; | ||
SecretKey = secretKey; | ||
PublicKey = publicKey; | ||
SentryUri = sentryUri; | ||
} | ||
|
||
private Dsn(string dsn, string projectId, string path, string secretKey, string publicKey, Uri sentryUri) | ||
{ | ||
_dsn = dsn; | ||
ProjectId = projectId; | ||
Path = path; | ||
SecretKey = secretKey; | ||
PublicKey = publicKey; | ||
SentryUri = sentryUri; | ||
} | ||
|
||
/// <summary> | ||
/// Tries to parse the string into a <see cref="Dsn"/> | ||
/// </summary> | ||
/// <param name="dsn">The string to attempt parsing.</param> | ||
/// <param name="finalDsn">The <see cref="Dsn"/> when successfully parsed.</param> | ||
/// <returns><c>true</c> if the string is a valid <see cref="Dsn"/> as was successfully parsed. Otherwise, <c>false</c>.</returns> | ||
public static bool TryParse(string dsn, out Dsn finalDsn) | ||
{ | ||
try | ||
{ | ||
var parsed = Parse(dsn, throwOnError: false); | ||
if (!parsed.HasValue) | ||
{ | ||
finalDsn = null; | ||
return false; | ||
} | ||
|
||
var (projectId, path, secretKey, publicKey, sentryUri) = parsed.Value; | ||
|
||
finalDsn = new Dsn( | ||
dsn, | ||
projectId, | ||
path, | ||
secretKey, | ||
publicKey, | ||
sentryUri); | ||
|
||
return true; | ||
} | ||
catch | ||
{ | ||
// Parse should not throw though! | ||
finalDsn = null; | ||
return false; | ||
} | ||
} | ||
|
||
private static (string projectId, string path, string secretKey, string publicKey, Uri sentryUri)? | ||
Parse(string dsn, bool throwOnError) | ||
{ | ||
Uri uri; | ||
if (throwOnError) | ||
{ | ||
uri = new Uri(dsn); // Throws the UriFormatException one would expect | ||
} | ||
else if (Uri.TryCreate(dsn, UriKind.Absolute, out var parsedUri)) | ||
{ | ||
uri = parsedUri; | ||
} | ||
else | ||
{ | ||
return null; | ||
} | ||
|
||
// uri.UserInfo returns empty string instead of null when no user info data is provided | ||
if (string.IsNullOrWhiteSpace(uri.UserInfo)) | ||
{ | ||
if (throwOnError) | ||
{ | ||
throw new ArgumentException("Invalid DSN: No public key provided."); | ||
} | ||
return null; | ||
} | ||
|
||
var keys = uri.UserInfo.Split(':'); | ||
var publicKey = keys[0]; | ||
if (string.IsNullOrWhiteSpace(publicKey)) | ||
{ | ||
if (throwOnError) | ||
{ | ||
throw new ArgumentException("Invalid DSN: No public key provided."); | ||
} | ||
return null; | ||
} | ||
|
||
string secretKey = null; | ||
if (keys.Length > 1) | ||
{ | ||
secretKey = keys[1]; | ||
} | ||
|
||
var path = uri.AbsolutePath.Substring(0, uri.AbsolutePath.LastIndexOf('/')); | ||
var projectId = uri.AbsoluteUri.Substring(uri.AbsoluteUri.LastIndexOf('/') + 1); | ||
|
||
if (string.IsNullOrWhiteSpace(projectId)) | ||
{ | ||
if (throwOnError) | ||
{ | ||
throw new ArgumentException("Invalid DSN: A Project Id is required."); | ||
} | ||
return null; | ||
} | ||
|
||
var builder = new UriBuilder | ||
{ | ||
Scheme = uri.Scheme, | ||
Host = uri.DnsSafeHost, | ||
Port = uri.Port, | ||
Path = $"{path}/api/{projectId}/store/" | ||
}; | ||
|
||
return (projectId, path, secretKey, publicKey, builder.Uri); | ||
} | ||
|
||
/// <summary> | ||
/// The original DSN string used to create this instance | ||
/// </summary> | ||
public override string ToString() => _dsn; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Sentry | ||
{ | ||
/// | ||
public class HttpSentryClient : ISentryClient, IDisposable | ||
{ | ||
/// | ||
public HttpSentryClient() | ||
{ | ||
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; | ||
} | ||
|
||
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) | ||
{ | ||
// TODO: A proper implementation | ||
CaptureEventAsync(new SentryEvent(e.ExceptionObject as Exception)); | ||
} | ||
|
||
/// | ||
public Task<SentryResponse> CaptureEventAsync(SentryEvent @event, CancellationToken cancellationToken = default) | ||
=> Task.FromResult(new SentryResponse(false)); | ||
|
||
/// | ||
public SentryResponse CaptureEvent(SentryEvent @event) => new SentryResponse(false); | ||
|
||
/// | ||
public void Dispose() | ||
{ | ||
AppDomain.CurrentDomain.UnhandledException -= CurrentDomain_UnhandledException; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Sentry | ||
{ | ||
/// <summary> | ||
/// Sentry client | ||
/// </summary> | ||
public interface ISentryClient | ||
{ | ||
/// <summary> | ||
/// Sends the <see cref="SentryEvent" /> to Sentry asynchronously | ||
/// </summary> | ||
/// <param name="event">The event to send to Sentry.</param> | ||
/// <param name="cancellationToken">The cancellation token.</param> | ||
/// <returns><see cref="SentryResponse"/></returns> | ||
Task<SentryResponse> CaptureEventAsync(SentryEvent @event, CancellationToken cancellationToken = default); | ||
|
||
/// <summary> | ||
/// Sends the <see cref="SentryEvent" /> to Sentry | ||
/// </summary> | ||
/// <param name="event">The event to send to Sentry.</param> | ||
/// <returns><see cref="SentryResponse"/></returns> | ||
SentryResponse CaptureEvent(SentryEvent @event); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Converters; | ||
|
||
namespace Sentry | ||
{ | ||
internal static class JsonSerializer | ||
{ | ||
private static readonly StringEnumConverter StringEnumConverter = new StringEnumConverter(); | ||
|
||
public static string SerializeObject<T>(T @object) => JsonConvert.SerializeObject(@object, StringEnumConverter); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
using System; | ||
using System.Runtime.CompilerServices; | ||
|
||
[assembly: InternalsVisibleTo("Sentry.Tests")] | ||
|
||
[assembly: CLSCompliant(true)] |
Oops, something went wrong.