Skip to content

Commit 5f6e7b4

Browse files
chore: Remove DSN from samples (#4268)
* chore: Remove DSN from samples Replaces #4238 To avoid SDK users accidentally using the DSN used internally to maintain the Sentry SDK, the DSN has been removed from all of the sample projects. SDK maintainers can either set this as an environment variable or, as with other SDK users, set it manually in the `samples/SamplesShared.cs` file (although changes to that file should never be checked in). #skip-changelog
1 parent a73789a commit 5f6e7b4

50 files changed

Lines changed: 294 additions & 69 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ test/**/*.apk
2727
/tools/
2828
*.log
2929
.sentry-native
30+
**/EnvironmentVariables.g.cs
3031

3132
# Download cache for Cocoa SDK
3233
modules/sentry-cocoa

Directory.Build.props

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@
6767
<DefineConstants>$(DefineConstants);CI_BUILD</DefineConstants>
6868
</PropertyGroup>
6969

70+
<!-- So we know at build time whether the DSN has been provided in an environment variable or not -->
71+
<PropertyGroup Condition=" '$(SENTRY_DSN)' != '' ">
72+
<DefineConstants>$(DefineConstants);SENTRY_DSN_DEFINED_IN_ENV</DefineConstants>
73+
</PropertyGroup>
74+
7075
<ItemGroup>
7176
<PackageReference Include="UnoptimizedAssemblyDetector" Version="0.1.1" PrivateAssets="All" />
7277
<PackageReference Include="Roslynator.Analyzers" Version="4.9.0" PrivateAssets="All" />

samples/Directory.Build.targets

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,44 @@
77
<Import Project="$(MSBuildThisFileDirectory)..\src\Sentry.Bindings.Cocoa\buildTransitive\Sentry.Bindings.Cocoa.targets"
88
Condition="'$(OutputType)' == 'Exe' And ('$(TargetPlatformIdentifier)' == 'ios' Or '$(TargetPlatformIdentifier)' == 'maccatalyst')" />
99
<Import Project="$(MSBuildThisFileDirectory)..\src\Sentry\Platforms\Native\buildTransitive\Sentry.Native.targets" />
10+
11+
<Target Name="GenerateSharedDsnConstant"
12+
BeforeTargets="BeforeCompile"
13+
Condition=" '$(SENTRY_DSN)' != '' and '$(PlatformIsMobile)' == 'true'">
14+
15+
<Message Text="Generating shared EnvironmentVariables.g.cs with embedded DSN..." Importance="High" />
16+
17+
<WriteLinesToFile
18+
File="$(MSBuildThisFileDirectory)EnvironmentVariables.g.cs"
19+
Lines="
20+
// This file is auto-generated by a custom build target used in the Sentry Samples... in your own projects you should
21+
// specify the DSN either directly in code (in the file where you initialize Sentry) or via SENTRY_DSN environment
22+
// variable (for non-mobile projects - that won't work on Android, iOS, or MacCatalyst).
23+
namespace Sentry.Samples%3B
24+
25+
internal static class EnvironmentVariables
26+
{
27+
/// &lt;summary&gt;
28+
/// To make things easier for the SDK maintainers we have a custom build target that writes the
29+
/// SENTRY_DSN environment variable into an EnvironmentVariables class that is available for mobile
30+
/// targets. This allows us to share one DSN defined in the ENV across desktop and mobile samples.
31+
/// Generally, you won't want to do this in your own mobile projects though - you should set the DSN
32+
/// in code as above
33+
/// &lt;/summary&gt;
34+
internal const string Dsn = &quot;$(SENTRY_DSN)&quot;%3B
35+
}
36+
"
37+
Overwrite="true" />
38+
</Target>
39+
40+
<ItemGroup>
41+
<!-- Make sure all sample projects include the generated file -->
42+
<Compile Include="$(MSBuildThisFileDirectory)EnvironmentVariables.g.cs" Condition="Exists('$(MSBuildThisFileDirectory)EnvironmentVariables.g.cs')" />
43+
<!-- Make sure all sample projects include any other shared files -->
44+
<Compile Include="$(MSBuildThisFileDirectory)SamplesShared.cs" />
45+
46+
<!-- Add shared global usings -->
47+
<Using Include="Sentry.Samples" />
48+
</ItemGroup>
49+
1050
</Project>

samples/SamplesShared.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace Sentry.Samples;
2+
3+
public static class SamplesShared
4+
{
5+
#if !SENTRY_DSN_DEFINED_IN_ENV
6+
/// <summary>
7+
/// <para>
8+
/// You must specify a DSN. See https://docs.sentry.io/product/sentry-basics/dsn-explainer/
9+
/// </para>
10+
/// <para>
11+
/// On mobile platforms (iOS, Android or MacCatalyst), this should be done in code.
12+
/// </para>
13+
/// <para>
14+
/// On other platforms you can set this in code and it is also possible to set this via an environment variable or
15+
/// via configuration bindings (e.g. in an app.config or an appsettings.json file).
16+
/// </para>
17+
/// </summary>
18+
#if !CI_BUILD
19+
#error Sign up for a free Sentry Account and enter your DSN here
20+
#endif
21+
public const string Dsn = "ENTER_YOUR_DSN_HERE";
22+
#endif
23+
}

samples/Sentry.Samples.Android/MainActivity.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,17 @@ protected override void OnCreate(Bundle? savedInstanceState)
99
{
1010
SentrySdk.Init(options =>
1111
{
12-
options.Dsn = "https://eb18e953812b41c3aeb042e666fd3b5c@o447951.ingest.sentry.io/5428537";
12+
#if !SENTRY_DSN_DEFINED_IN_ENV
13+
// You must specify a DSN. On mobile platforms, this should be done in code here.
14+
// See https://docs.sentry.io/product/sentry-basics/dsn-explainer/
15+
options.Dsn = SamplesShared.Dsn;
16+
#else
17+
// To make things easier for the SDK maintainers our samples check for a SENTRY_DSN environment variable
18+
// and write this (as a constant) into an EnvironmentVariables class. Generally, you won't want to do
19+
// this in your own mobile projects though - you should set the DSN in code as above
20+
options.Dsn = EnvironmentVariables.Dsn;
21+
#endif
22+
1323
options.SendDefaultPii = true; // adds the user's IP address automatically
1424

1525
// Android specific .NET features are under the Android properties:

samples/Sentry.Samples.AspNetCore.Basic/Program.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
builder.WebHost.UseSentry(options =>
44
{
5-
// A DSN is required. You can set it here, or in configuration, or in an environment variable.
6-
options.Dsn = "https://eb18e953812b41c3aeb042e666fd3b5c@o447951.ingest.sentry.io/5428537";
5+
#if !SENTRY_DSN_DEFINED_IN_ENV
6+
// A DSN is required. You can set here in code, or you can set it in the SENTRY_DSN environment variable.
7+
// See https://docs.sentry.io/product/sentry-basics/dsn-explainer/
8+
options.Dsn = SamplesShared.Dsn;
9+
#endif
710

811
// Enable Sentry performance monitoring
912
options.TracesSampleRate = 1.0;

samples/Sentry.Samples.AspNetCore.Blazor.Server/Program.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
builder.Services.AddServerSideBlazor();
77
builder.WebHost.UseSentry(options =>
88
{
9-
options.Dsn = "https://eb18e953812b41c3aeb042e666fd3b5c@o447951.ingest.sentry.io/5428537";
9+
#if !SENTRY_DSN_DEFINED_IN_ENV
10+
// A DSN is required. You can set here in code, in the SENTRY_DSN environment variable or in your appsettings.json
11+
// See https://docs.sentry.io/product/sentry-basics/dsn-explainer/
12+
options.Dsn = SamplesShared.Dsn;
13+
#endif
1014
options.TracesSampleRate = 1.0;
1115
options.Debug = true;
1216
});

samples/Sentry.Samples.AspNetCore.Blazor.Wasm/Program.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
var builder = WebAssemblyHostBuilder.CreateDefault(args);
77
builder.UseSentry(options =>
88
{
9-
options.Dsn = "https://eb18e953812b41c3aeb042e666fd3b5c@o447951.ingest.sentry.io/5428537";
9+
#if !SENTRY_DSN_DEFINED_IN_ENV
10+
// A DSN is required. You can set here in code, or you can set it in the SENTRY_DSN environment variable.
11+
// See https://docs.sentry.io/product/sentry-basics/dsn-explainer/
12+
options.Dsn = SamplesShared.Dsn;
13+
#endif
1014
options.Debug = true;
1115
});
1216

samples/Sentry.Samples.AspNetCore.Grpc/Program.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ public static IWebHost BuildWebHost(string[] args) =>
3131
builder.AddGrpc();
3232
builder.AddSentryOptions(options =>
3333
{
34+
#if !SENTRY_DSN_DEFINED_IN_ENV
35+
// A DSN is required. You can set here in code, via the SENTRY_DSN environment variable or in your
36+
// appsettings.json file.
37+
// See https://docs.sentry.io/platforms/dotnet/guides/aspnetcore/#configure
38+
options.Dsn = SamplesShared.Dsn;
39+
#endif
40+
3441
// The parameter 'options' here has values populated through the configuration system.
3542
// That includes 'appsettings.json', environment variables and anything else
3643
// defined on the ConfigurationBuilder.

samples/Sentry.Samples.AspNetCore.Grpc/appsettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// All Sentry settings can also be configured via code or environment variables:
44
"Sentry": {
55
// The DSN can also be set via environment variable
6-
"Dsn": "https://eb18e953812b41c3aeb042e666fd3b5c@o447951.ingest.sentry.io/5428537",
6+
//"Dsn": "TODO: Configure your DSN here and uncomment this line",
77
// Opt-in for payload submission
88
"MaxRequestBodySize": "Always",
99
// Sends Cookies, User Id when one is logged on and user IP address to sentry. It's turned off by default.

0 commit comments

Comments
 (0)