Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .phan/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@
// This is useful for excluding hopelessly unanalyzable
// files that can't be removed for whatever reason.
'exclude_file_list' => [
'src/API/Common/Compatibility/Psr3.php',
'vendor/composer/composer/src/Composer/InstalledVersions.php',
],

Expand Down
3 changes: 2 additions & 1 deletion .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
$finder = PhpCsFixer\Finder::create()
->in('examples/')
->in('tests/')
->in('src/');
->in('src/')
->exclude('src/API/Common/Compatibility');

$config = new PhpCsFixer\Config();

Expand Down
6 changes: 6 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
},
"files": [
"src/Context/fiber/initialize_fiber_handler.php",
"src/API/_compat.php",
"src/API/Trace/functions.php",
"src/Contrib/Otlp/_register.php",
"src/Contrib/Grpc/_register.php",
Expand Down Expand Up @@ -207,5 +208,10 @@
"OpenTelemetry\\Tests\\Integration\\SDK\\Common\\Configuration\\TestEnvSourceProvider"
]
}
},
"scripts": {
"test-psr3": [
"OpenTelemetry\\Tests\\Integration\\Composer\\Psr3Compatibility::run"
]
}
}
2 changes: 2 additions & 0 deletions deptrac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ deptrac:
- ./vendor/symfony/polyfill-php83/Resources/stubs
exclude_files:
- '#.*test.*#'
- '#src/API/_compat\.php#'
- '#src/API/Common/Compatibility/.*#'
layers:
- name: API
collectors:
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ parameters:
- ./tests
# - ./examples TODO: Uncomment this once examples are updated
excludePaths:
- src/API/Common/Compatibility
- tests/TraceContext/W3CTestService
- tests/Unit/Config/SDK/Configuration/ExampleSdk
ignoreErrors:
Expand Down
1 change: 1 addition & 0 deletions psalm.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<directory name="./examples"/>
<ignoreFiles>
<directory name="./examples/traces/demo/"/>
<directory name="src/API/Common/Compatibility/"/>
<directory name="tests/Unit/Config/SDK/Configuration/ExampleSdk"/>
<directory name="tests/TraceContext/W3CTestService"/>
<directory name="vendor"/>
Expand Down
151 changes: 151 additions & 0 deletions src/API/Common/Compatibility/Psr3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?php

/**
* @see https://github.com/php-fig/log/blob/1.0.0/Psr/Log/AbstractLogger.php
* @see https://github.com/php-fig/log/blob/1.0.0/Psr/Log/NullLogger.php
*/

declare(strict_types=1);

namespace Psr\Log;

/**
* This is a simple Logger implementation that other Loggers can inherit from.
*
* It simply delegates all log-level-specific methods to the `log` method to
* reduce boilerplate code that a simple Logger that does the same thing with
* messages regardless of the error level has to implement.
*/
abstract class AbstractLogger implements LoggerInterface
{
/**
* System is unusable.
*
* @param string $message
* @param array $context
* @return null
*/
public function emergency($message, array $context = [])
{
$this->log(LogLevel::EMERGENCY, $message, $context);
}

/**
* Action must be taken immediately.
*
* Example: Entire website down, database unavailable, etc. This should
* trigger the SMS alerts and wake you up.
*
* @param string $message
* @param array $context
* @return null
*/
public function alert($message, array $context = [])
{
$this->log(LogLevel::ALERT, $message, $context);
}

/**
* Critical conditions.
*
* Example: Application component unavailable, unexpected exception.
*
* @param string $message
* @param array $context
* @return null
*/
public function critical($message, array $context = [])
{
$this->log(LogLevel::CRITICAL, $message, $context);
}

/**
* Runtime errors that do not require immediate action but should typically
* be logged and monitored.
*
* @param string $message
* @param array $context
* @return null
*/
public function error($message, array $context = [])
{
$this->log(LogLevel::ERROR, $message, $context);
}

/**
* Exceptional occurrences that are not errors.
*
* Example: Use of deprecated APIs, poor use of an API, undesirable things
* that are not necessarily wrong.
*
* @param string $message
* @param array $context
* @return null
*/
public function warning($message, array $context = [])
{
$this->log(LogLevel::WARNING, $message, $context);
}

/**
* Normal but significant events.
*
* @param string $message
* @param array $context
* @return null
*/
public function notice($message, array $context = [])
{
$this->log(LogLevel::NOTICE, $message, $context);
}

/**
* Interesting events.
*
* Example: User logs in, SQL logs.
*
* @param string $message
* @param array $context
* @return null
*/
public function info($message, array $context = [])
{
$this->log(LogLevel::INFO, $message, $context);
}

/**
* Detailed debug information.
*
* @param string $message
* @param array $context
* @return null
*/
public function debug($message, array $context = [])
{
$this->log(LogLevel::DEBUG, $message, $context);
}
}

/**
* This Logger can be used to avoid conditional log calls
*
* Logging should always be optional, and if no logger is provided to your
* library creating a NullLogger instance to have something to throw logs at
* is a good way to avoid littering your code with `if ($this->logger) { }`
* blocks.
*/
class NullLogger extends AbstractLogger
{
/**
* Logs with an arbitrary level.
*
* @param mixed $level
* @param string $message
* @param array $context
* @return null
*/
public function log($level, $message, array $context = [])
{
// noop
}
}
17 changes: 17 additions & 0 deletions src/API/_compat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace OpenTelemetry\API;

use function class_exists;
use Composer\InstalledVersions;
use Composer\Semver\VersionParser;

if (
// Provide a backwards-compatible Psr3 implementation when running under composer.
class_exists(InstalledVersions::class, false)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is InstalledVersions always loaded during composer execution or do we have to drop the autoload: false argument?

&& InstalledVersions::satisfies(new VersionParser(), 'composer/composer', '~2.0')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Must first check whether we are running as composer command, otherwise VersionParser might not be available / composer/composer might not be InstalledVersions::isInstalled().

Additionally this might break setups with composer/composer ^2 and psr/log ^3 installed as project dependencies.

) {
require_once __DIR__ . '/Common/Compatibility/Psr3.php';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this file always be loaded before the SDK is autoloaded?

Should IMO instead live in the SDK autoload script, using just the API package with composer scripts works fine.

}
1 change: 1 addition & 0 deletions src/API/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"OpenTelemetry\\API\\": "."
},
"files": [
"_compat.php",
"Trace/functions.php"
]
},
Expand Down
15 changes: 15 additions & 0 deletions tests/Integration/Composer/Psr3Compatibility.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace OpenTelemetry\Tests\Integration\Composer;

use Composer\Script\Event;

final class Psr3Compatibility
{
public static function run(Event $event): void
{
require_once $event->getComposer()->getConfig()->get('vendor-dir') . '/autoload.php';
}
}
Loading