Skip to content
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

split config into own package #1248

Closed
wants to merge 14 commits into from
2 changes: 2 additions & 0 deletions .gitsplit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ splits:
target: "https://${GH_TOKEN}@github.com/opentelemetry-php/gen-otlp-protobuf.git"
- prefix: "src/Context"
target: "https://${GH_TOKEN}@github.com/opentelemetry-php/context.git"
- prefix: "src/Config"
target: "https://${GH_TOKEN}@github.com/opentelemetry-php/config.git"
- prefix: "src/SemConv"
target: "https://${GH_TOKEN}@github.com/opentelemetry-php/sem-conv.git"
- prefix: "src/API"
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
],
"replace": {
"open-telemetry/api": "self.version",
"open-telemetry/config": "self.version",
"open-telemetry/context": "self.version",
"open-telemetry/gen-otlp-protobuf": "self.version",
"open-telemetry/sdk": "self.version",
Expand Down
8 changes: 8 additions & 0 deletions deptrac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ deptrac:
collectors:
- type: directory
value: src/SDK/.*
- name: Config
collectors:
- type: directory
value: src/Config/.*
- name: Context
collectors:
- type: directory
Expand Down Expand Up @@ -81,8 +85,10 @@ deptrac:
ruleset:
Context:
- FFI
- Config
SemConv: ~
API:
- Config
- Context
- PsrLog
SDK:
Expand All @@ -91,6 +97,8 @@ deptrac:
- PsrHttp
- HttpPlug
- Composer
Config:
- PsrLog
Contrib:
- +SDK
- +OtelProto
Expand Down
4 changes: 2 additions & 2 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ parameters:
-
message: "#Call to an undefined method .*#"
paths:
- tests/Unit/SDK/Common/Configuration/Resolver/PhpIniResolverTest.php
- tests/Unit/SDK/Common/Configuration/Resolver/CompositeResolverTest.php
- tests/Unit/Config/Resolver/PhpIniResolverTest.php
- tests/Unit/Config/Resolver/CompositeResolverTest.php
-
message: "#Call to an undefined method .*:allows.*#"
paths:
Expand Down
18 changes: 9 additions & 9 deletions src/API/Behavior/Internal/LogWriterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,34 @@
use OpenTelemetry\API\Behavior\Internal\LogWriter\NoopLogWriter;
use OpenTelemetry\API\Behavior\Internal\LogWriter\Psr3LogWriter;
use OpenTelemetry\API\Behavior\Internal\LogWriter\StreamLogWriter;
use OpenTelemetry\API\Instrumentation\ConfigurationResolver;
use OpenTelemetry\API\LoggerHolder;
use OpenTelemetry\Config\Configuration;
use OpenTelemetry\Config\KnownValues;
use OpenTelemetry\Config\Variables;

class LogWriterFactory
{
private const OTEL_PHP_LOG_DESTINATION = 'OTEL_PHP_LOG_DESTINATION';

public function create(): LogWriterInterface
{
$dest = (new ConfigurationResolver())->getString(self::OTEL_PHP_LOG_DESTINATION);
$dest = (new Configuration())->getEnum(Variables::OTEL_PHP_LOG_DESTINATION);
$logger = LoggerHolder::get();

switch ($dest) {
case 'none':
case KnownValues::VALUE_NONE:
return new NoopLogWriter();
case 'stderr':
case KnownValues::VALUE_STDERR:
return new StreamLogWriter('php://stderr');
case 'stdout':
case KnownValues::VALUE_STDOUT:
return new StreamLogWriter('php://stdout');
case 'psr3':
case KnownValues::VALUE_PSR3:
if ($logger) {
return new Psr3LogWriter($logger);
}
error_log('OpenTelemetry: cannot use OTEL_PHP_LOG_DESTINATION=psr3 without providing a PSR-3 logger');

//default to error log
return new ErrorLogWriter();
case 'error_log':
case KnownValues::VALUE_ERROR_LOG:
return new ErrorLogWriter();
default:
if ($logger) {
Expand Down
12 changes: 3 additions & 9 deletions src/API/Behavior/Internal/Logging.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace OpenTelemetry\API\Behavior\Internal;

use OpenTelemetry\API\Behavior\Internal\LogWriter\LogWriterInterface;
use OpenTelemetry\Config\Configuration;
use OpenTelemetry\Config\Variables;
use Psr\Log\LogLevel;

/**
Expand Down Expand Up @@ -69,15 +71,7 @@ public static function logLevel(): int

private static function getLogLevel(): int
{
$level = array_key_exists(self::OTEL_LOG_LEVEL, $_SERVER)
? $_SERVER[self::OTEL_LOG_LEVEL]
: getenv(self::OTEL_LOG_LEVEL);
if (!$level) {
$level = ini_get(self::OTEL_LOG_LEVEL);
}
if (!$level) {
$level = self::DEFAULT_LEVEL;
}
$level = Configuration::getEnum(Variables::OTEL_LOG_LEVEL, self::DEFAULT_LEVEL);

return self::level($level);
}
Expand Down
77 changes: 0 additions & 77 deletions src/API/Instrumentation/ConfigurationResolver.php

This file was deleted.

14 changes: 0 additions & 14 deletions src/API/Instrumentation/ConfigurationResolverInterface.php

This file was deleted.

1 change: 1 addition & 0 deletions src/API/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
],
"require": {
"php": "^8.0",
"open-telemetry/config": "*",
"open-telemetry/context": "^1.0",
"psr/log": "^1.1|^2.0|^3.0",
"symfony/polyfill-php81": "^1.26",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

declare(strict_types=1);

namespace OpenTelemetry\SDK\Common\Util;
namespace OpenTelemetry\Config\Accessor;

use LogicException;

/**
* @internal
*/
class ClassConstantAccessor
{
public static function requireValue(string $className, string $constantName)
public static function requireValue(string $className, string $constantName): mixed
{
$constant = self::getFullName($className, $constantName);

Expand All @@ -21,7 +24,7 @@ public static function requireValue(string $className, string $constantName)
return constant($constant);
}

public static function getValue(string $className, string $constantName)
public static function getValue(string $className, string $constantName): mixed
{
$constant = self::getFullName($className, $constantName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

declare(strict_types=1);

namespace OpenTelemetry\SDK\Common\Configuration\Resolver;
namespace OpenTelemetry\Config\Accessor;

/**
* @internal
*/
class PhpIniAccessor
{
/**
* Mockable accessor for php.ini values
* @internal
* @return array|false|string
*/
public function get(string $variableName)
public function get(string $variableName): array|false|string
{
return get_cfg_var($variableName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,24 @@

declare(strict_types=1);

namespace OpenTelemetry\SDK\Common\Configuration;

use InvalidArgumentException;
use OpenTelemetry\API\Behavior\LogsMessagesTrait;
use OpenTelemetry\SDK\Common\Configuration\Parser\BooleanParser;
use OpenTelemetry\SDK\Common\Configuration\Parser\ListParser;
use OpenTelemetry\SDK\Common\Configuration\Parser\MapParser;
use OpenTelemetry\SDK\Common\Configuration\Parser\RatioParser;
use OpenTelemetry\SDK\Common\Configuration\Resolver\CompositeResolver;
use OpenTelemetry\SDK\Common\Util\ClassConstantAccessor;
namespace OpenTelemetry\Config;

use OpenTelemetry\Config\Accessor\ClassConstantAccessor;
use OpenTelemetry\Config\Parser\BooleanParser;
use OpenTelemetry\Config\Parser\ListParser;
use OpenTelemetry\Config\Parser\MapParser;
use OpenTelemetry\Config\Parser\RatioParser;
use OpenTelemetry\Config\Resolver\CompositeResolver;
use UnexpectedValueException;

/**
* Configuration can come from one or more of the following sources (from highest to lowest priority):
* - environment variable (getenv, $_SERVER)
* - values defined in php.ini
* - environment variable ($_SERVER)
* - configuration file (todo)
*/
class Configuration
{
use LogsMessagesTrait;

public static function has(string $name): bool
{
return CompositeResolver::instance()->hasVariable($name);
Expand Down Expand Up @@ -59,16 +55,10 @@ public static function getBoolean(string $key, bool $default = null): bool
)
);

try {
return BooleanParser::parse($resolved);
} catch (InvalidArgumentException) {
self::logWarning(sprintf('Invalid boolean value "%s" interpreted as "false" for %s', $resolved, $key));

return false;
}
return BooleanParser::parse($resolved);
}

public static function getMixed(string $key, $default = null)
public static function getMixed(string $key, $default = null): mixed
{
return self::validateVariableValue(
CompositeResolver::instance()->resolve(
Expand Down Expand Up @@ -165,7 +155,7 @@ private static function validateVariableType(string $variableName, string $type)
return $variableName;
}

private static function validateVariableValue($value, ?int $filterType = null)
private static function validateVariableValue(mixed $value, ?int $filterType = null): mixed
{
if ($filterType !== null && filter_var($value, $filterType) === false) {
throw new UnexpectedValueException(sprintf('Value has invalid type "%s"', gettype($value)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace OpenTelemetry\SDK\Common\Configuration;
namespace OpenTelemetry\Config;

/**
* Default values for environment variables defined by the OpenTelemetry specification and language specific variables for the PHP SDK.
Expand Down Expand Up @@ -119,4 +119,6 @@ interface Defaults
public const OTEL_PHP_DISABLED_INSTRUMENTATIONS = [];
public const OTEL_PHP_LOGS_PROCESSOR = 'batch';
public const OTEL_PHP_LOG_DESTINATION = 'default';
public const OTEL_PHP_DEBUG_SCOPES_DISABLED = 'false';
public const OTEL_PHP_FIBERS_ENABLED = 'false';
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace OpenTelemetry\SDK\Common\Configuration;
namespace OpenTelemetry\Config;

use Psr\Log\LogLevel;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

declare(strict_types=1);

namespace OpenTelemetry\SDK\Common\Configuration\Parser;
namespace OpenTelemetry\Config\Parser;

use InvalidArgumentException;

/**
* @internal
*/
class BooleanParser
{
private const TRUE_VALUE = 'true';
private const FALSE_VALUE = 'false';

/**
* @param string|bool $value
*/
public static function parse($value): bool
public static function parse(string|bool $value): bool
{
if (is_bool($value)) {
return $value;
Expand Down
Loading
Loading