-
-
Notifications
You must be signed in to change notification settings - Fork 267
Fix: Read all environment variables in sentry #375
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
6dd3619
95a4d8a
61fbabc
3de7b95
9ecebf3
ccb6c74
ecf2750
f37fc9c
b9169f3
7294bde
adcb9ac
13d5e8f
ccb0a69
7fea45b
52af9c7
9aa6633
f2b320b
a3c31eb
3b83d90
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import 'package:meta/meta.dart'; | ||
|
||
import 'sentry_options.dart'; | ||
|
||
/// This method reads available environment variables and uses them | ||
/// accordingly. | ||
/// To see which environment variables are available, see [EnvironmentVariables] | ||
/// | ||
/// The precendence of these options are tricky, | ||
/// see https://docs.sentry.io/platforms/dart/configuration/options/ | ||
/// and https://github.com/getsentry/sentry-dart/issues/306 | ||
@internal | ||
void setEnvironmentVariables(SentryOptions options, EnvironmentVariables vars) { | ||
ueman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// options has precendence over vars | ||
options.dsn = options.dsn ?? vars.dsn; | ||
|
||
var environment = options.platformChecker.environment; | ||
options.environment = options.environment ?? environment; | ||
|
||
// vars has precedence over options | ||
options.environment = vars.environment ?? options.environment; | ||
|
||
// vars has precedence over options | ||
options.release = vars.release ?? options.release; | ||
options.dist = vars.dist ?? options.dist; | ||
ueman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/// Reads environment variables from the system. | ||
/// In an Flutter environment these can be set via | ||
/// `flutter build --dart-define=VARIABLE_NAME=VARIABLE_VALUE`. | ||
class EnvironmentVariables { | ||
static const _sentryEnvironment = 'SENTRY_ENVIRONMENT'; | ||
static const _sentryDsn = 'SENTRY_DSN'; | ||
static const _sentryRelease = 'SENTRY_RELEASE'; | ||
static const _sentryDist = 'SENTRY_DSN'; | ||
|
||
/// `SENTRY_ENVIRONMENT` | ||
/// See [SentryOptions.environment] | ||
String? get environment => const bool.hasEnvironment(_sentryEnvironment) | ||
marandaneto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
? const String.fromEnvironment(_sentryEnvironment) | ||
: null; | ||
|
||
/// `SENTRY_DSN` | ||
/// See [SentryOptions.dsn] | ||
String? get dsn => const bool.hasEnvironment(_sentryDsn) | ||
? const String.fromEnvironment(_sentryDsn) | ||
: null; | ||
|
||
// `SENTRY_RELEASE` | ||
/// See [SentryOptions.release] | ||
String? get release => const bool.hasEnvironment(_sentryRelease) | ||
? const String.fromEnvironment(_sentryRelease) | ||
: null; | ||
|
||
/// `SENTRY_DIST` | ||
/// See [SentryOptions.dist] | ||
String? get dist => const bool.hasEnvironment(_sentryDist) | ||
? const String.fromEnvironment(_sentryDist) | ||
: null; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import 'sentry_options.dart'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe we move |
||
|
||
/// Helper to check in which enviroment the library is running. | ||
/// The envirment checks (release/debug/profile) are mutually exclusive. | ||
class PlatformChecker { | ||
|
@@ -17,4 +19,18 @@ class PlatformChecker { | |
bool isProfileMode() { | ||
return const bool.fromEnvironment('dart.vm.profile', defaultValue: false); | ||
} | ||
|
||
/// This can be set as [SentryOptions.environment] | ||
String get environment { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this be a method instead? quite a few conditions to be just a property, maybe we move hardcoded values to |
||
// We infer the enviroment based on the release/non-release and profile | ||
// constants. | ||
|
||
if (isReleaseMode()) { | ||
return defaultEnvironment; | ||
} | ||
if (isProfileMode()) { | ||
return 'profile'; | ||
} | ||
return 'debug'; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import 'package:sentry/sentry.dart'; | ||
import 'package:sentry/src/environment_variables.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
import 'mocks.dart'; | ||
import 'mocks/mock_environment_variables.dart'; | ||
|
||
void main() { | ||
group('Environment Variables', () { | ||
marandaneto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// See https://docs.sentry.io/platforms/dart/configuration/options/ | ||
// and https://github.com/getsentry/sentry-dart/issues/306 | ||
test('SentryOptions are correctly overriden by environment', () { | ||
final options = SentryOptions(dsn: fakeDsn); | ||
options.release = 'release-1.2.3'; | ||
options.dist = 'foo'; | ||
options.environment = 'prod'; | ||
|
||
setEnvironmentVariables( | ||
options, | ||
MockEnvironmentVariables( | ||
dsn: 'foo-bar', | ||
environment: 'staging', | ||
release: 'release-9.8.7', | ||
dist: 'bar', | ||
), | ||
); | ||
|
||
expect(options.dsn, fakeDsn); | ||
expect(options.environment, 'staging'); | ||
expect(options.release, 'release-9.8.7'); | ||
expect(options.dist, 'bar'); | ||
}); | ||
|
||
test('No environment variables are set', () { | ||
final options = SentryOptions(dsn: fakeDsn); | ||
options.environment = 'prod'; | ||
options.release = 'release-1.2.3'; | ||
options.dist = 'foo'; | ||
|
||
setEnvironmentVariables( | ||
options, | ||
MockEnvironmentVariables(), | ||
); | ||
|
||
expect(options.dsn, fakeDsn); | ||
expect(options.environment, 'prod'); | ||
expect(options.release, 'release-1.2.3'); | ||
expect(options.dist, 'foo'); | ||
}); | ||
}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import 'package:sentry/src/environment_variables.dart'; | ||
|
||
class MockEnvironmentVariables implements EnvironmentVariables { | ||
MockEnvironmentVariables({ | ||
String? dist, | ||
String? dsn, | ||
String? environment, | ||
String? release, | ||
}) : _dist = dist, | ||
_dsn = dsn, | ||
_environment = environment, | ||
_release = release; | ||
|
||
final String? _dist; | ||
final String? _dsn; | ||
final String? _environment; | ||
final String? _release; | ||
|
||
@override | ||
String? get dist => _dist; | ||
|
||
@override | ||
String? get dsn => _dsn; | ||
|
||
@override | ||
String? get environment => _environment; | ||
|
||
@override | ||
String? get release => _release; | ||
} |
Uh oh!
There was an error while loading. Please reload this page.