Skip to content

Commit 23dc79c

Browse files
Support for SENTRY_ENVIRONMENT environment variable (#354)
Co-authored-by: Alex Bouma <[email protected]>
1 parent 2dfcf9e commit 23dc79c

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- Respect the `SENTRY_ENVIRONMENT` environment variable to override the Laravel environment (#354)
6+
57
## 1.8.0
68

79
- Add `send_default_pii` option by default to published config file (#340)

config/sentry.php

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
// capture release as git sha
88
// 'release' => trim(exec('git --git-dir ' . base_path('.git') . ' log --pretty="%h" -n1 HEAD')),
99

10+
// When left empty or `null` the Laravel environment will be used
11+
'environment' => env('SENTRY_ENVIRONMENT'),
12+
1013
'breadcrumbs' => [
1114
// Capture Laravel logs in breadcrumbs
1215
'logs' => true,

src/Sentry/Laravel/ServiceProvider.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,17 @@ protected function configureAndRegisterClient(): void
114114

115115
$options = \array_merge(
116116
[
117-
'environment' => $this->app->environment(),
118117
'prefixes' => [$basePath],
119118
'in_app_exclude' => ["{$basePath}/vendor"],
120119
],
121120
$userConfig
122121
);
123122

123+
// When we get no environment from the (user) configuration we default to the Laravel environment
124+
if (empty($options['environment'])) {
125+
$options['environment'] = $this->app->environment();
126+
}
127+
124128
$clientBuilder = ClientBuilder::create($options);
125129

126130
// Set the Laravel SDK identifier and version
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Sentry;
4+
5+
use Sentry\Laravel\Tests\SentryLaravelTestCase;
6+
7+
class ServiceProviderWithEnvironmentFromConfigTest extends SentryLaravelTestCase
8+
{
9+
public function testSentryEnvironmentDefaultsToLaravelEnvironment()
10+
{
11+
$this->assertEquals('testing', app()->environment());
12+
}
13+
14+
public function testEmptySentryEnvironmentDefaultsToLaravelEnvironment()
15+
{
16+
$this->resetApplicationWithConfig([
17+
'sentry.environment' => '',
18+
]);
19+
20+
$this->assertEquals('testing', $this->getHubFromContainer()->getClient()->getOptions()->getEnvironment());
21+
22+
$this->resetApplicationWithConfig([
23+
'sentry.environment' => null,
24+
]);
25+
26+
$this->assertEquals('testing', $this->getHubFromContainer()->getClient()->getOptions()->getEnvironment());
27+
}
28+
29+
public function testSentryEnvironmentDefaultGetsOverriddenByConfig()
30+
{
31+
$this->resetApplicationWithConfig([
32+
'sentry.environment' => 'not_testing',
33+
]);
34+
35+
$this->assertEquals('not_testing', $this->getHubFromContainer()->getClient()->getOptions()->getEnvironment());
36+
}
37+
}

0 commit comments

Comments
 (0)