Skip to content

Commit 06b4851

Browse files
Refactored logging to use the same sink for tests and tools (#102)
* Fixed ResourceAttributes deprecated * Moved parseBoolValue to PhpPartFacade * Renamed appCodeArgs to appCodeRequestArgs * Initial change to ComponentTestCaseBase * Renamed appCodeArguments to appCodeRequestArgs * Renamed ResourcesClient to ResourcesCleanerClient * Renamed RESOURCES_CLIENT to RESOURCES_CLEANER_CLIENT * Refactored PackagesPhpRequirementTest * Refactored test classes that can use implTestForAppCodeSetsHowFinished * Fixed ProcessUtilTest * Refactored BootstrapStageLoggingClassTrait * Fixed ComponentTestCaseBase::copyProdOptionsToAppCodeHostParams * Synced BootstrapStageLoggingClassTrait::addContextToMessage * Merged Add_feature_to_BootstrapStageLoggingClassTrait to Refactor_tests_tools_logging * Refactored logging to use the same sink for tests and tools * Temporarily disabled ComponentTestsUtilComponentTest::test0WithoutEscalation * Temporarily disabled ComponentTestsUtilComponentTest::test0WithoutEscalation * Re-enable ComponentTestsUtilComponentTest::test0WithoutEscalation * Fixed ComponentTestsUtilComponentTest * Fixed bootstrap_build_tools.php * Fixed test clean-up timing out * Make sure to close spawned process handle to allow it to exit normally * Refactored MySqliAutoInstrumentationTest to use implTestForAppCodeSetsHowFinished * Refactored PgSqlAutoInstrumentationTest * Started removal of BuildToolsLog * Fixed test case when MySQLi/ PostgreSQL instrumentation is disabled * Replaced separate build tools logging by prod logging * Added LoggingClassTraitTest::testWithIsNotEvaluatedIfLevelDisabled * Improved alignment of tests logging with prod and tests * Renamed feature to featureOrCategory where applicable
1 parent 0f503ab commit 06b4851

108 files changed

Lines changed: 2065 additions & 1551 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

packaging/test/smokeTest.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,31 @@
3939
echo CGREEN."OK\n".CDEF;
4040

4141
echo "Trying to log something to stderr: ";
42-
$loggerClass = "{$scopeName}OpenTelemetry\\Distro\\BootstrapStageLogger";
43-
$loggerClass::logDebug("This is just a message to test logger", __FILE__, __LINE__, __CLASS__, __FUNCTION__);
42+
/**
43+
* @var class-string<\OpenTelemetry\Distro\Log\LogBackend> $logBackendClass
44+
* @noinspection PhpFullyQualifiedNameUsageInspection
45+
*/
46+
$logBackendClass = "{$scopeName}OpenTelemetry\\Distro\\Log\\LogBackend";
47+
/**
48+
* @var class-string<\OpenTelemetry\Distro\Log\LogFeature> $logFeatureClass
49+
* @noinspection PhpFullyQualifiedNameUsageInspection
50+
*/
51+
$logFeatureClass = "{$scopeName}OpenTelemetry\\Distro\\Log\\LogFeature";
52+
/**
53+
* @var class-string<\OpenTelemetry\Distro\Log\LogLevel> $logLevelClass
54+
* @noinspection PhpFullyQualifiedNameUsageInspection
55+
*/
56+
$logLevelClass = "{$scopeName}OpenTelemetry\\Distro\\Log\\LogLevel";
57+
$logBackendClass::getSingletonInstance()->write(
58+
file: __FILE__,
59+
line: __LINE__,
60+
func: __FUNCTION__,
61+
featureOrCategory: $logFeatureClass::BOOTSTRAP,
62+
level: $logLevelClass::off,
63+
message: 'This is just a dummy message to test production code logging',
64+
context: ['dummy ctx key' => 'dummy ctx value'],
65+
isForced: true
66+
);
4467
echo CGREEN."OK\n".CDEF;
4568

4669
echo CGREEN."Smoke test passed\n".CDEF;

prod/php/OpenTelemetry/Distro/AutoloaderForClassesInDirectory.php

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
<?php
22

3-
/** @noinspection PhpIllegalPsrClassPathInspection */
4-
53
declare(strict_types=1);
64

75
namespace OpenTelemetry\Distro;
86

7+
use OpenTelemetry\Distro\Log\LoggingClassTrait;
98
use OpenTelemetry\Distro\Log\LogFeature;
9+
use OpenTelemetry\Distro\Util\DistroRuntimeException;
1010

1111
final class AutoloaderForClassesInDirectory
1212
{
13-
use BootstrapStageLoggingClassTrait;
13+
use LoggingClassTrait;
1414

1515
private readonly int $autoloadFqClassNamePrefixLength;
1616

@@ -24,7 +24,9 @@ private function __construct(
2424
public static function register(string $dirRootNamespace, string $dirFullPath): void
2525
{
2626
$autoloader = new self(autoloadFqClassNamePrefix: $dirRootNamespace . '\\', srcFilePathPrefix: $dirFullPath . DIRECTORY_SEPARATOR);
27-
spl_autoload_register(($autoloader)->autoloadCodeForClass(...));
27+
if (!spl_autoload_register(($autoloader)->autoloadCodeForClass(...))) {
28+
throw new DistroRuntimeException('spl_autoload_register() returned false', context: compact('dirRootNamespace', 'dirFullPath'));
29+
}
2830
}
2931

3032
private function shouldAutoloadCodeForClass(string $fqClassName): bool
@@ -37,10 +39,11 @@ public function autoloadCodeForClass(string $fqClassName): void
3739
{
3840
// Example of $fqClassName: OpenTelemetry\Distro\Autoloader
3941

40-
self::logTrace(__LINE__, __FUNCTION__, 'Entered', compact('fqClassName'));
42+
$logTrace = self::logTrace(__FUNCTION__);
43+
$logTrace?->with(__LINE__, 'Entered', compact('fqClassName'));
4144

4245
if (!self::shouldAutoloadCodeForClass($fqClassName)) {
43-
self::logTrace(__LINE__, __FUNCTION__, 'shouldAutoloadCodeForClass returned false', compact('fqClassName'));
46+
$logTrace?->with(__LINE__, 'shouldAutoloadCodeForClass returned false', compact('fqClassName'));
4447
return;
4548
}
4649

@@ -52,34 +55,26 @@ public function autoloadCodeForClass(string $fqClassName): void
5255
$classSrcFileAbsolute = $this->srcFilePathPrefix . $classSrcFileRelative;
5356

5457
if (file_exists($classSrcFileAbsolute)) {
55-
self::logTrace(__LINE__, __FUNCTION__, 'Before require', compact('classSrcFileAbsolute'));
58+
$logTrace?->with(__LINE__, 'Before require', compact('classSrcFileAbsolute'));
5659
require $classSrcFileAbsolute;
57-
self::logTrace(__LINE__, __FUNCTION__, 'After require', compact('classSrcFileAbsolute'));
60+
$logTrace?->with(__LINE__, 'After require', compact('classSrcFileAbsolute'));
5861
} else {
59-
self::logTrace(__LINE__, __FUNCTION__, 'File with the code for class does not exist', compact('fqClassName', 'classSrcFileAbsolute'));
62+
$logTrace?->with(__LINE__, 'File with the code for class does not exist', compact('fqClassName', 'classSrcFileAbsolute'));
6063
}
6164
}
6265

6366
/**
64-
* Must be defined in class using BootstrapStageLoggingClassTrait
67+
* Must be defined in class using LoggingClassTrait
6568
*/
6669
private static function getCurrentSourceCodeFile(): string
6770
{
6871
return __FILE__;
6972
}
7073

7174
/**
72-
* Must be defined in class using BootstrapStageLoggingClassTrait
73-
*/
74-
private static function getCurrentSourceCodeClass(): string
75-
{
76-
return __CLASS__;
77-
}
78-
79-
/**
80-
* Must be defined in class using BootstrapStageLoggingClassTrait
75+
* Must be defined in class using LoggingClassTrait
8176
*/
82-
private static function getCurrentLogFeature(): int
77+
private static function getCurrentOptionalLogProdFeatureIntOrCategoryString(): int
8378
{
8479
return LogFeature::BOOTSTRAP;
8580
}

prod/php/OpenTelemetry/Distro/BootstrapStageLogger.php

Lines changed: 0 additions & 179 deletions
This file was deleted.

0 commit comments

Comments
 (0)