-
-
Notifications
You must be signed in to change notification settings - Fork 230
Expand file tree
/
Copy pathDsn.cs
More file actions
132 lines (109 loc) · 3.31 KB
/
Dsn.cs
File metadata and controls
132 lines (109 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
namespace Sentry;
/// <summary>
/// The Data Source Name of a given project in Sentry.
/// </summary>
/// <remarks>
/// <see href="https://develop.sentry.dev/sdk/overview/#parsing-the-dsn"/>
/// </remarks>
internal sealed class Dsn
{
/// <summary>
/// Source DSN string.
/// </summary>
public string Source { get; }
/// <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>
/// Sentry API's base URI.
/// </summary>
private Uri ApiBaseUri { get; }
private Dsn(
string source,
string projectId,
string? path,
string? secretKey,
string publicKey,
Uri apiBaseUri)
{
Source = source;
ProjectId = projectId;
Path = path;
SecretKey = secretKey;
PublicKey = publicKey;
ApiBaseUri = apiBaseUri;
}
public Uri GetStoreEndpointUri() => new(ApiBaseUri, "store/");
public Uri GetEnvelopeEndpointUri() => new(ApiBaseUri, "envelope/");
public Uri GetOtlpTracesEndpointUri() => new(ApiBaseUri, "integration/otlp/v1/traces");
public override string ToString() => Source;
public static bool IsDisabled(string? dsn) =>
SentryConstants.DisableSdkDsnValue.Equals(dsn, StringComparison.OrdinalIgnoreCase);
public static Dsn Parse(string dsn)
{
var uri = new Uri(dsn);
// uri.UserInfo returns empty string instead of null when no user info data is provided
if (string.IsNullOrWhiteSpace(uri.UserInfo))
{
throw new ArgumentException("Invalid DSN: No public key provided.");
}
var keys = uri.UserInfo.Split(':');
var publicKey = keys[0];
if (string.IsNullOrWhiteSpace(publicKey))
{
throw new ArgumentException("Invalid DSN: No public key provided.");
}
var secretKey = keys.Length > 1
? keys[1]
: null;
var path = uri.AbsolutePath[..uri.AbsolutePath.LastIndexOf('/')];
var projectId = uri.AbsoluteUri[(uri.AbsoluteUri.LastIndexOf('/') + 1)..];
if (string.IsNullOrWhiteSpace(projectId))
{
throw new ArgumentException("Invalid DSN: A Project Id is required.");
}
var apiBaseUri = new UriBuilder
{
Scheme = uri.Scheme,
Host = uri.DnsSafeHost,
Port = uri.Port,
Path = $"{path}/api/{projectId}/"
}.Uri;
return new Dsn(
dsn,
projectId,
path,
secretKey,
publicKey,
apiBaseUri);
}
public static Dsn? TryParse(string? dsn)
{
if (string.IsNullOrWhiteSpace(dsn))
{
return null;
}
try
{
return Parse(dsn);
}
catch
{
// Parse should not throw though!
return null;
}
}
}