Skip to content

Commit

Permalink
Added Sdk::isDeveloperModeEnabled() helper.
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisLightfootWild committed Nov 17, 2023
1 parent d6c4c89 commit bf21fd4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/SDK/Common/Configuration/Variables.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,5 @@ interface Variables
public const OTEL_PHP_AUTOLOAD_ENABLED = 'OTEL_PHP_AUTOLOAD_ENABLED';
public const OTEL_PHP_INTERNAL_METRICS_ENABLED = 'OTEL_PHP_INTERNAL_METRICS_ENABLED'; //whether the SDK should emit its own metrics
public const OTEL_PHP_DISABLED_INSTRUMENTATIONS = 'OTEL_PHP_DISABLED_INSTRUMENTATIONS';
public const OTEL_PHP_DEVELOPER_MODE_ENABLED = 'OTEL_PHP_DEVELOPER_MODE_ENABLED';
}
18 changes: 18 additions & 0 deletions src/SDK/Sdk.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace OpenTelemetry\SDK;

use Composer\InstalledVersions;
use OpenTelemetry\API\Metrics\MeterProviderInterface;
use OpenTelemetry\API\Trace\TracerProviderInterface;
use OpenTelemetry\Context\Propagation\TextMapPropagatorInterface;
Expand Down Expand Up @@ -43,6 +44,23 @@ public static function isInstrumentationDisabled(string $name): bool
return in_array($name, Configuration::getList(Variables::OTEL_PHP_DISABLED_INSTRUMENTATIONS));
}

/**
* Tests whether developer mode has been enabled by config.
*/
public static function isDeveloperModeEnabled(): bool
{
if (Configuration::has(Variables::OTEL_PHP_DEVELOPER_MODE_ENABLED)) {
return Configuration::getBoolean(Variables::OTEL_PHP_DEVELOPER_MODE_ENABLED);
}

try {
return class_exists(InstalledVersions::class)
&& InstalledVersions::getRootPackage()['dev'];
} catch (\Exception $exception) {
return false;
}
}

public static function builder(): SdkBuilder
{
return new SdkBuilder();
Expand Down
15 changes: 15 additions & 0 deletions tests/Unit/SDK/SdkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,21 @@ public static function instrumentationDisabledProvider(): array
];
}

/**
* @dataProvider disabledProvider
*/
public function test_developer_mode_can_be_enabled(string $value, bool $expected): void
{
$this->setEnvironmentVariable(Variables::OTEL_PHP_DEVELOPER_MODE_ENABLED, $value);

$this->assertEquals($expected, Sdk::isDeveloperModeEnabled());
}

public function test_developer_mode_auto_detected(): void
{
$this->assertTrue(Sdk::isDeveloperModeEnabled());
}

public function test_builder(): void
{
$this->assertInstanceOf(SdkBuilder::class, Sdk::builder());
Expand Down

0 comments on commit bf21fd4

Please sign in to comment.