-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Environment Variable Handling (#561)
* Move EnvironmentVariablesTrait * Add env var constants * Move EnvironmentVariablesTrait * Set default to empty string * Add ClassConstantAccessor * Use Env Var constants * Fix naming * Add Env Var parsers * Fix boolean values * Add Resolver * Fix covers annotation * Add Accessor * Allow unknown env vars * Add test for empty var * Filter correct types * Allow empty lists and maps * Add 'none' to known compressions * Use inline constants * Use Env Accessor * Fix CS * Make TraceIdRatioBasedSampler construction safe * Supress wrong Psalm error * Remove redundant checks * Remove redundant checks * Add additional getters to trait * Remove redundant metadadata handling * Fix CS * Fix insecure gRPC credential creation * Test for cert file
- Loading branch information
Showing
33 changed files
with
1,901 additions
and
288 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\Contrib\Otlp; | ||
|
||
use OpenTelemetry\SDK\Behavior\LogsMessagesTrait; | ||
use OpenTelemetry\SDK\Common\Environment\EnvironmentVariablesTrait; | ||
use OpenTelemetry\SDK\Trace\Behavior\UsesSpanConverterTrait; | ||
|
||
trait ExporterTrait | ||
{ | ||
use EnvironmentVariablesTrait; | ||
use LogsMessagesTrait; | ||
use UsesSpanConverterTrait; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\SDK\Common\Environment; | ||
|
||
use OpenTelemetry\SDK\Common\Environment\Parser\BooleanParser; | ||
use OpenTelemetry\SDK\Common\Environment\Parser\ListParser; | ||
use OpenTelemetry\SDK\Common\Environment\Parser\MapParser; | ||
use OpenTelemetry\SDK\Common\Environment\Parser\RatioParser; | ||
use UnexpectedValueException; | ||
|
||
class Accessor | ||
{ | ||
public static function getString(string $variableName, string $default = null): string | ||
{ | ||
return (string) self::validateVariableValue( | ||
Resolver::resolveValue( | ||
self::validateVariableType($variableName, VariableTypes::STRING), | ||
$default | ||
) | ||
); | ||
} | ||
|
||
public static function getBool(string $variableName, string $default = null): bool | ||
{ | ||
return BooleanParser::parse( | ||
self::validateVariableValue( | ||
Resolver::resolveValue( | ||
self::validateVariableType($variableName, VariableTypes::BOOL), | ||
$default | ||
) | ||
) | ||
); | ||
} | ||
|
||
public static function getInt(string $variableName, string $default = null): int | ||
{ | ||
return (int) self::validateVariableValue( | ||
Resolver::resolveValue( | ||
self::validateVariableType($variableName, VariableTypes::INTEGER), | ||
$default | ||
), | ||
FILTER_VALIDATE_INT | ||
); | ||
} | ||
|
||
public static function getRatio(string $variableName, string $default = null): float | ||
{ | ||
return RatioParser::parse( | ||
self::validateVariableValue( | ||
Resolver::resolveValue( | ||
self::validateVariableType($variableName, VariableTypes::RATIO), | ||
$default | ||
) | ||
) | ||
); | ||
} | ||
|
||
public static function getEnum(string $variableName, string $default = null): string | ||
{ | ||
return (string) self::validateVariableValue( | ||
Resolver::resolveValue( | ||
self::validateVariableType($variableName, VariableTypes::ENUM), | ||
$default | ||
) | ||
); | ||
} | ||
|
||
public static function getList(string $variableName, string $default = null): array | ||
{ | ||
return ListParser::parse( | ||
Resolver::resolveValue( | ||
self::validateVariableType($variableName, VariableTypes::LIST), | ||
$default | ||
) | ||
); | ||
} | ||
|
||
public static function getMap(string $variableName, string $default = null): array | ||
{ | ||
return MapParser::parse( | ||
Resolver::resolveValue( | ||
self::validateVariableType($variableName, VariableTypes::MAP), | ||
$default | ||
) | ||
); | ||
} | ||
|
||
public static function getMixed(string $variableName, string $default = null, ?string $type = null) | ||
{ | ||
return self::validateVariableValue( | ||
Resolver::resolveValue( | ||
$variableName, | ||
$default | ||
) | ||
); | ||
} | ||
|
||
private static function validateVariableType(string $variableName, string $type): string | ||
{ | ||
$variableType = Resolver::getType($variableName); | ||
|
||
if ($variableType !== null && $variableType !== $type && $variableType !== VariableTypes::MIXED) { | ||
throw new UnexpectedValueException( | ||
sprintf('Variable "%s" is not supposed to be of type "%s" but type "%s"', $variableName, $type, $variableType) | ||
); | ||
} | ||
|
||
return $variableName; | ||
} | ||
|
||
private static function validateVariableValue($value, ?int $filterType = null) | ||
{ | ||
if ($filterType !== null && filter_var($value, $filterType) === false) { | ||
throw new UnexpectedValueException(sprintf('Value has invalid type "%s"', gettype($value))); | ||
} | ||
|
||
if ($value === null || $value === '') { | ||
throw new UnexpectedValueException( | ||
'Variable must not be null or empty' | ||
); | ||
} | ||
|
||
return $value; | ||
} | ||
} |
Oops, something went wrong.