-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
send errors/warnings/etc by default through PHP's error_log. This gives more control to administrators, since error_log can be configured to write to any stream (file, stderr, etc). I think it's also less surprising for people trying out otel (particularly with development PHP settings, where trigger_error often breaks an application). Adding a configuration value to control where logs go: OTEL_PHP_LOG_DESTINATION.
- Loading branch information
Showing
41 changed files
with
514 additions
and
253 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,40 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use OpenTelemetry\API\Globals; | ||
use OpenTelemetry\API\Instrumentation\Configurator; | ||
use OpenTelemetry\SDK\Trace\SpanExporter\ConsoleSpanExporterFactory; | ||
use OpenTelemetry\SDK\Trace\SpanProcessor\SimpleSpanProcessor; | ||
use OpenTelemetry\SDK\Trace\TracerProvider; | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
/** | ||
* Example of globally registering <Signal>Provider instances. | ||
* Generally this is hidden inside the SDK builder or SDK autoloading, | ||
* but you can also do it manually. The providers are stored in | ||
* context, and reset to previous values or defaults when the | ||
* scope is detached. | ||
*/ | ||
|
||
//before, a no-op provider is provided by default | ||
echo 'Before: ' . get_class(Globals::tracerProvider()) . PHP_EOL; | ||
|
||
$tracerProvider = TracerProvider::builder()->addSpanProcessor( | ||
new SimpleSpanProcessor( | ||
(new ConsoleSpanExporterFactory())->create() | ||
) | ||
)->build(); | ||
|
||
$configurator = Configurator::create() | ||
->withTracerProvider($tracerProvider); | ||
|
||
$scope = $configurator->activate(); | ||
//activated, now our $tracerProvider is globally available | ||
echo 'During: ' . get_class(Globals::tracerProvider()) . PHP_EOL; | ||
|
||
$scope->detach(); | ||
|
||
//after scope detached, back to default no-op providers: | ||
echo 'After: ' . get_class(Globals::tracerProvider()) . PHP_EOL; |
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
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
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
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
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,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\Example; | ||
|
||
require __DIR__ . '/../../vendor/autoload.php'; | ||
|
||
use Monolog\Logger; | ||
use OpenTelemetry\SDK\Trace\TracerProviderFactory; | ||
|
||
echo 'Starting Logging example' . PHP_EOL; | ||
|
||
/** | ||
* By default errors and warnings from the SDK itself (for example misconfiguration, exporter errors) will be sent to PHP's error_log. | ||
* You can change that by setting OTEL_PHP_LOG_DESTINATION. | ||
* Valid values for OTEL_PHP_LOG_DESTINATION: error_log, stdout, stderr, none, psr3, default | ||
* (default = psr-3 if LoggerHolder::set called, otherwise error_log | ||
* | ||
* Note that PSR-3 logging will only work if a PSR-3 logger is registered in OpenTelemetry\API\LoggerHolder::set() | ||
* If no PSR-3 logger is available, it will fall back to using error_log. | ||
*/ | ||
|
||
putenv('OTEL_PHP_LOG_DESTINATION=stderr'); | ||
putenv('OTEL_EXPORTER_OTLP_ENDPOINT=http://does-not-exist/endpoint'); //invalid endpoint, export will fail | ||
putenv('OTEL_EXPORTER_OTLP_PROTOCOL=grpc'); | ||
|
||
$factory = new TracerProviderFactory(); | ||
$tracerProvider = $factory->create(); | ||
|
||
$tracer = $tracerProvider->getTracer('io.opentelemetry.contrib.php'); | ||
$span = $tracer->spanBuilder('root-span')->startSpan(); | ||
$span->end(); | ||
$tracerProvider->shutdown(); |
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,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\API\Behavior\Internal\LogWriter; | ||
|
||
class ErrorLogWriter implements LogWriterInterface | ||
{ | ||
public function write($level, string $message, array $context): void | ||
{ | ||
error_log(Formatter::format($level, $message, $context)); | ||
} | ||
} |
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,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\API\Behavior\Internal\LogWriter; | ||
|
||
class Formatter | ||
{ | ||
public static function format($level, string $message, array $context): string | ||
{ | ||
$exception = (array_key_exists('exception', $context) && $context['exception'] instanceof \Throwable) | ||
? $context['exception'] | ||
: null; | ||
if ($exception) { | ||
$message = sprintf( | ||
'OpenTelemetry: [%s] %s [exception] %s%s%s', | ||
$level, | ||
$message, | ||
$exception->getMessage(), | ||
PHP_EOL, | ||
$exception->getTraceAsString() | ||
); | ||
} else { | ||
//get calling location, skipping over trait, formatter etc | ||
$caller = debug_backtrace()[3]; | ||
$message = sprintf( | ||
'OpenTelemetry: [%s] %s in %s(%s)', | ||
$level, | ||
$message, | ||
$caller['file'], | ||
$caller['line'], | ||
); | ||
} | ||
|
||
return $message; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/API/Behavior/Internal/LogWriter/LogWriterInterface.php
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,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\API\Behavior\Internal\LogWriter; | ||
|
||
interface LogWriterInterface | ||
{ | ||
public function write($level, string $message, array $context): void; | ||
} |
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,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\API\Behavior\Internal\LogWriter; | ||
|
||
class NoopLogWriter implements LogWriterInterface | ||
{ | ||
public function write($level, string $message, array $context): void | ||
{ | ||
//do nothing | ||
} | ||
} |
Oops, something went wrong.