Skip to content

Commit 644bdf5

Browse files
authored
Read DSN, Environment and Release options from environment variables (#1054)
* Add reading DSN from env variable * Update DSN option comment * Add reading environment and release from env vars * Update changelog * Add comments for new functions * Add more logs
1 parent 4b9ec80 commit 644bdf5

File tree

9 files changed

+116
-32
lines changed

9 files changed

+116
-32
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
- Add functionality to give/revoke user consent for crash uploads ([#1053](https://github.com/getsentry/sentry-unreal/pull/1053))
1717
- Add new API for capturing user feedback ([#1051](https://github.com/getsentry/sentry-unreal/pull/1051))
18+
- Read `DSN`, `Environment` and `Release` options from environment variables ([#1054](https://github.com/getsentry/sentry-unreal/pull/1054))
1819

1920
### Dependencies
2021

plugin-dev/Source/Sentry/Private/Android/AndroidSentrySubsystem.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ void FAndroidSentrySubsystem::InitWithSettings(const USentrySettings* settings,
3333
{
3434
TSharedPtr<FJsonObject> SettingsJson = MakeShareable(new FJsonObject);
3535
SettingsJson->SetStringField(TEXT("dsn"), settings->Dsn);
36-
SettingsJson->SetStringField(TEXT("release"), settings->OverrideReleaseName ? settings->Release : settings->GetFormattedReleaseName());
37-
SettingsJson->SetStringField(TEXT("environment"), settings->Environment);
36+
SettingsJson->SetStringField(TEXT("release"), settings->GetEffectiveRelease());
37+
SettingsJson->SetStringField(TEXT("environment"), settings->GetEffectiveEnvironment());
3838
SettingsJson->SetStringField(TEXT("dist"), settings->Dist);
3939
SettingsJson->SetBoolField(TEXT("autoSessionTracking"), settings->EnableAutoSessionTracking);
4040
SettingsJson->SetNumberField(TEXT("sessionTimeout"), settings->SessionTimeout);

plugin-dev/Source/Sentry/Private/Apple/AppleSentrySubsystem.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ void FAppleSentrySubsystem::InitWithSettings(const USentrySettings* settings, US
5151
dispatch_async(dispatch_get_main_queue(), ^{
5252
[SENTRY_APPLE_CLASS(SentrySDK) startWithConfigureOptions:^(SentryOptions* options) {
5353
options.dsn = settings->GetEffectiveDsn().GetNSString();
54-
options.environment = settings->Environment.GetNSString();
54+
options.releaseName = settings->GetEffectiveRelease().GetNSString();
55+
options.environment = settings->GetEffectiveEnvironment().GetNSString();
5556
options.dist = settings->Dist.GetNSString();
5657
options.enableAutoSessionTracking = settings->EnableAutoSessionTracking;
5758
options.sessionTrackingIntervalMillis = settings->SessionTimeout;
58-
options.releaseName = settings->OverrideReleaseName ? settings->Release.GetNSString() : settings->GetFormattedReleaseName().GetNSString();
5959
options.attachStacktrace = settings->AttachStacktrace;
6060
options.debug = settings->Debug;
6161
options.sampleRate = [NSNumber numberWithFloat:settings->SampleRate];

plugin-dev/Source/Sentry/Private/GenericPlatform/GenericPlatformSentrySubsystem.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,10 +298,9 @@ void FGenericPlatformSentrySubsystem::InitWithSettings(const USentrySettings* se
298298
ConfigureCertsPath(options);
299299
ConfigureNetworkConnectFunc(options);
300300

301-
sentry_options_set_release(options, TCHAR_TO_ANSI(settings->OverrideReleaseName ? *settings->Release : *settings->GetFormattedReleaseName()));
302-
303301
sentry_options_set_dsn(options, TCHAR_TO_ANSI(*settings->GetEffectiveDsn()));
304-
sentry_options_set_environment(options, TCHAR_TO_ANSI(*settings->Environment));
302+
sentry_options_set_release(options, TCHAR_TO_ANSI(*settings->GetEffectiveRelease()));
303+
sentry_options_set_environment(options, TCHAR_TO_ANSI(*settings->GetEffectiveEnvironment()));
305304
sentry_options_set_dist(options, TCHAR_TO_ANSI(*settings->Dist));
306305
sentry_options_set_logger(options, PrintVerboseLog, nullptr);
307306
sentry_options_set_debug(options, settings->Debug);

plugin-dev/Source/Sentry/Private/Linux/LinuxSentrySubsystem.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ void FLinuxSentrySubsystem::InitWithSettings(const USentrySettings* Settings, US
1616
{
1717
FGenericPlatformSentrySubsystem::InitWithSettings(Settings, BeforeSendHandler, BeforeBreadcrumbHandler, TraceSampler);
1818

19-
InitCrashReporter(Settings->Release, Settings->Environment);
19+
InitCrashReporter(Settings->GetEffectiveRelease(), Settings->GetEffectiveEnvironment());
2020
}
2121

2222
void FLinuxSentrySubsystem::ConfigureHandlerPath(sentry_options_t* Options)

plugin-dev/Source/Sentry/Private/Microsoft/MicrosoftSentrySubsystem.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ void FMicrosoftSentrySubsystem::InitWithSettings(const USentrySettings* Settings
3131

3232
if (FPlatformMisc::GetCrashHandlingType() == ECrashHandlingType::Default)
3333
{
34-
InitCrashReporter(Settings->Release, Settings->Environment);
34+
InitCrashReporter(Settings->GetEffectiveRelease(), Settings->GetEffectiveEnvironment());
3535
}
3636
#else
37-
InitCrashReporter(Settings->Release, Settings->Environment);
37+
InitCrashReporter(Settings->GetEffectiveRelease(), Settings->GetEffectiveEnvironment());
3838
#endif
3939
}
4040

plugin-dev/Source/Sentry/Private/SentrySettings.cpp

Lines changed: 73 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ USentrySettings::USentrySettings(const FObjectInitializer& ObjectInitializer)
1414
, InitAutomatically(true)
1515
, Dsn()
1616
, Debug(true)
17-
, Environment(GetDefaultEnvironmentName())
1817
, SampleRate(1.0f)
1918
, EnableAutoLogAttachment(false)
2019
, AttachStacktrace(true)
@@ -86,34 +85,47 @@ void USentrySettings::PostEditChangeProperty(FPropertyChangedEvent& PropertyChan
8685

8786
FString USentrySettings::GetEffectiveDsn() const
8887
{
89-
return GIsEditor && !EditorDsn.IsEmpty() ? EditorDsn : Dsn;
90-
}
88+
if (GIsEditor && !EditorDsn.IsEmpty())
89+
{
90+
return EditorDsn;
91+
}
9192

92-
FString USentrySettings::GetFormattedReleaseName()
93-
{
94-
FString FormattedReleaseName = FApp::GetProjectName();
93+
if (!Dsn.IsEmpty())
94+
{
95+
return Dsn;
96+
}
9597

96-
FString Version = TEXT("");
97-
GConfig->GetString(TEXT("/Script/EngineSettings.GeneralProjectSettings"), TEXT("ProjectVersion"), Version, GGameIni);
98-
if (!Version.IsEmpty())
98+
const FString& EnvVarDsn = FPlatformMisc::GetEnvironmentVariable(TEXT("SENTRY_DSN"));
99+
if (!EnvVarDsn.IsEmpty())
99100
{
100-
FormattedReleaseName = FString::Printf(TEXT("%s@%s"), *FormattedReleaseName, *Version);
101+
UE_LOG(LogSentrySdk, Log, TEXT("DSN is not set in plugin settings - using SENTRY_DSN environment variable instead."));
102+
return EnvVarDsn;
101103
}
102104

103-
return FormattedReleaseName;
105+
UE_LOG(LogSentrySdk, Log, TEXT("DSN is not configured."));
106+
return FString();
104107
}
105108

106-
bool USentrySettings::IsDirty() const
109+
FString USentrySettings::GetEffectiveEnvironment() const
107110
{
108-
return bIsDirty;
109-
}
111+
if (!Environment.IsEmpty())
112+
{
113+
UE_LOG(LogSentrySdk, Verbose, TEXT("Using the value from plugin settings as Sentry environment."));
114+
return Environment;
115+
}
110116

111-
void USentrySettings::ClearDirtyFlag()
112-
{
113-
bIsDirty = false;
117+
const FString& EnvVarEnvironment = FPlatformMisc::GetEnvironmentVariable(TEXT("SENTRY_ENVIRONMENT"));
118+
if (!EnvVarEnvironment.IsEmpty())
119+
{
120+
UE_LOG(LogSentrySdk, Log, TEXT("Using SENTRY_ENVIRONMENT variable as Sentry environment."));
121+
return EnvVarEnvironment;
122+
}
123+
124+
UE_LOG(LogSentrySdk, Log, TEXT("Using current build configuration as Sentry environment."));
125+
return GetEnvironmentFromBuildConfig();
114126
}
115127

116-
FString USentrySettings::GetDefaultEnvironmentName()
128+
FString USentrySettings::GetEnvironmentFromBuildConfig() const
117129
{
118130
if (GIsEditor)
119131
{
@@ -129,6 +141,49 @@ FString USentrySettings::GetDefaultEnvironmentName()
129141
return LexToString(FApp::GetBuildConfiguration());
130142
}
131143

144+
FString USentrySettings::GetEffectiveRelease() const
145+
{
146+
if (OverrideReleaseName)
147+
{
148+
UE_LOG(LogSentrySdk, Verbose, TEXT("Using the value from plugin settings as Sentry release."));
149+
return Release;
150+
}
151+
152+
const FString& EnvVarRelease = FPlatformMisc::GetEnvironmentVariable(TEXT("SENTRY_RELEASE"));
153+
if (!EnvVarRelease.IsEmpty())
154+
{
155+
UE_LOG(LogSentrySdk, Log, TEXT("Using SENTRY_RELEASE variable as Sentry release."));
156+
return EnvVarRelease;
157+
}
158+
159+
UE_LOG(LogSentrySdk, Log, TEXT("Using current project name and version as Sentry release."));
160+
return GetReleaseFromProjectSettings();
161+
}
162+
163+
FString USentrySettings::GetReleaseFromProjectSettings() const
164+
{
165+
FString FormattedReleaseName = FApp::GetProjectName();
166+
167+
FString Version = TEXT("");
168+
GConfig->GetString(TEXT("/Script/EngineSettings.GeneralProjectSettings"), TEXT("ProjectVersion"), Version, GGameIni);
169+
if (!Version.IsEmpty())
170+
{
171+
FormattedReleaseName = FString::Printf(TEXT("%s@%s"), *FormattedReleaseName, *Version);
172+
}
173+
174+
return FormattedReleaseName;
175+
}
176+
177+
bool USentrySettings::IsDirty() const
178+
{
179+
return bIsDirty;
180+
}
181+
182+
void USentrySettings::ClearDirtyFlag()
183+
{
184+
bIsDirty = false;
185+
}
186+
132187
void USentrySettings::LoadDebugSymbolsProperties()
133188
{
134189
const FString PropertiesFilePath = FPaths::Combine(FPaths::ProjectDir(), TEXT("sentry.properties"));

plugin-dev/Source/Sentry/Private/SentrySubsystem.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ void USentrySubsystem::Initialize()
7878
const USentrySettings* Settings = FSentryModule::Get().GetSettings();
7979
check(Settings);
8080

81-
if (Settings->Dsn.IsEmpty())
81+
if (Settings->GetEffectiveDsn().IsEmpty())
8282
{
8383
UE_LOG(LogSentrySdk, Warning, TEXT("Sentry requires minimal configuration for its initialization - please provide the DSN in plugin settings."));
8484
return;

plugin-dev/Source/Sentry/Public/SentrySettings.h

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -384,17 +384,46 @@ class SENTRY_API USentrySettings : public UObject
384384
* Gets the effective DSN based on current execution context.
385385
*
386386
* @return Editor DSN when running in the editor and one is set; otherwise, falls back to the default DSN.
387+
* If neither is provided, the SDK will attempt to read it from the SENTRY_DSN environment variable.
387388
*/
388389
FString GetEffectiveDsn() const;
389390

390-
static FString GetFormattedReleaseName();
391+
/**
392+
* Gets the effective environment based on current execution context.
393+
*
394+
* @return By default, the SDK uses the `Environment` value from the plugin settings if set.
395+
* If not, the SDK will attempt to read it from SENTRY_ENVIRONMENT environment variable.
396+
* If that is also not set, the environment is automatically derived from the current build configuration.
397+
*/
398+
FString GetEffectiveEnvironment() const;
399+
400+
/**
401+
* Gets the environment from the application's build configuration.
402+
*
403+
* @return Environment string based on build configuration (`Shipping` maps to `Release`, others map directly).
404+
*/
405+
FString GetEnvironmentFromBuildConfig() const;
406+
407+
/**
408+
* Gets the effective release name based on current execution context.
409+
*
410+
* @return By default, the SDK uses the `Release` value from the plugin settings if `OverrideReleaseName` flag is set.
411+
* If not, the SDK will attempt to read it from SENTRY_RELEASE environment variable.
412+
* If that is also not set, the release name is automatically derived from the current project name and version.
413+
*/
414+
FString GetEffectiveRelease() const;
415+
416+
/**
417+
* Gets the release name from the project settings.
418+
*
419+
* @return Release name derived from the current project name and version to match the format `<ProjectName>@<Version>`.
420+
*/
421+
FString GetReleaseFromProjectSettings() const;
391422

392423
bool IsDirty() const;
393424
void ClearDirtyFlag();
394425

395426
private:
396-
FString GetDefaultEnvironmentName();
397-
398427
void LoadDebugSymbolsProperties();
399428

400429
bool bIsDirty;

0 commit comments

Comments
 (0)