improve logs console output (#1060)
support multiple scopes in console log output, and update batch example to illustrate
This commit is contained in:
parent
cb45b4a75d
commit
5e827ab7fc
|
|
@ -6,52 +6,35 @@ namespace OpenTelemetry\Example;
|
||||||
|
|
||||||
use OpenTelemetry\API\Logs\EventLogger;
|
use OpenTelemetry\API\Logs\EventLogger;
|
||||||
use OpenTelemetry\API\Logs\LogRecord;
|
use OpenTelemetry\API\Logs\LogRecord;
|
||||||
use OpenTelemetry\SDK\Common\Export\Stream\StreamTransportFactory;
|
use OpenTelemetry\SDK\Common\Attribute\Attributes;
|
||||||
use OpenTelemetry\SDK\Common\Instrumentation\InstrumentationScopeFactory;
|
use OpenTelemetry\SDK\Common\Instrumentation\InstrumentationScopeFactory;
|
||||||
use OpenTelemetry\SDK\Common\Time\ClockFactory;
|
use OpenTelemetry\SDK\Common\Time\ClockFactory;
|
||||||
use OpenTelemetry\SDK\Logs\Exporter\ConsoleExporter;
|
use OpenTelemetry\SDK\Logs\Exporter\ConsoleExporterFactory;
|
||||||
use OpenTelemetry\SDK\Logs\LoggerProvider;
|
use OpenTelemetry\SDK\Logs\LoggerProvider;
|
||||||
use OpenTelemetry\SDK\Logs\LogRecordLimitsBuilder;
|
|
||||||
use OpenTelemetry\SDK\Logs\Processor\BatchLogsProcessor;
|
use OpenTelemetry\SDK\Logs\Processor\BatchLogsProcessor;
|
||||||
use OpenTelemetry\SDK\Trace\TracerProvider;
|
|
||||||
|
|
||||||
require __DIR__ . '/../../../vendor/autoload.php';
|
require __DIR__ . '/../../../vendor/autoload.php';
|
||||||
|
|
||||||
$loggerProvider = new LoggerProvider(
|
$loggerProvider = new LoggerProvider(
|
||||||
new BatchLogsProcessor(
|
new BatchLogsProcessor(
|
||||||
new ConsoleExporter(
|
(new ConsoleExporterFactory())->create(),
|
||||||
(new StreamTransportFactory())->create(STDOUT, '')
|
|
||||||
),
|
|
||||||
ClockFactory::getDefault()
|
ClockFactory::getDefault()
|
||||||
),
|
),
|
||||||
new InstrumentationScopeFactory(
|
new InstrumentationScopeFactory(Attributes::factory())
|
||||||
(new LogRecordLimitsBuilder())->build()->getAttributeFactory()
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
$tracerProvider = new TracerProvider();
|
//get a logger, and emit a log record from an EventLogger.
|
||||||
$tracer = $tracerProvider->getTracer('demo-tracer');
|
$loggerOne = $loggerProvider->getLogger('demo', '1.0');
|
||||||
|
$loggerTwo = $loggerProvider->getLogger('demo', '2.0');
|
||||||
//start and activate a span
|
$eventLoggerOne = new EventLogger($loggerOne, 'my-domain');
|
||||||
$span = $tracer->spanBuilder('root')->startSpan();
|
$eventLoggerTwo = new EventLogger($loggerTwo, 'my-domain');
|
||||||
$scope = $span->activate();
|
|
||||||
echo 'Trace id: ' . $span->getContext()->getTraceId() . PHP_EOL;
|
|
||||||
echo 'Span id: ' . $span->getContext()->getSpanId() . PHP_EOL;
|
|
||||||
|
|
||||||
//get a logger, and emit a log record from an EventLogger. The active context (trace id + span id) will be
|
|
||||||
//attached to the log record
|
|
||||||
$logger = $loggerProvider->getLogger('demo', '1.0', 'http://schema.url', ['foo' => 'bar']);
|
|
||||||
$eventLogger = new EventLogger($logger, 'my-domain');
|
|
||||||
|
|
||||||
$record = (new LogRecord(['foo' => 'bar', 'baz' => 'bat', 'msg' => 'hello world']))
|
$record = (new LogRecord(['foo' => 'bar', 'baz' => 'bat', 'msg' => 'hello world']))
|
||||||
->setSeverityText('INFO')
|
->setSeverityText('INFO')
|
||||||
->setSeverityNumber(9);
|
->setSeverityNumber(9);
|
||||||
|
|
||||||
$eventLogger->logEvent('foo', $record);
|
$eventLoggerOne->logEvent('foo', $record);
|
||||||
$eventLogger->logEvent('bar', (new LogRecord('hello world')));
|
$eventLoggerOne->logEvent('bar', (new LogRecord('hello world')));
|
||||||
|
$eventLoggerTwo->logEvent('foo', $record);
|
||||||
//end span
|
|
||||||
$span->end();
|
|
||||||
$scope->detach();
|
|
||||||
|
|
||||||
//shut down logger provider
|
//shut down logger provider
|
||||||
$loggerProvider->shutdown();
|
$loggerProvider->shutdown();
|
||||||
|
|
|
||||||
|
|
@ -32,20 +32,20 @@ class ConsoleExporter implements LogRecordExporterInterface
|
||||||
public function export(iterable $batch, ?CancellationInterface $cancellation = null): FutureInterface
|
public function export(iterable $batch, ?CancellationInterface $cancellation = null): FutureInterface
|
||||||
{
|
{
|
||||||
$resource = null;
|
$resource = null;
|
||||||
$scope = null;
|
$scopes = [];
|
||||||
foreach ($batch as $record) {
|
foreach ($batch as $record) {
|
||||||
if (!$resource) {
|
if (!$resource) {
|
||||||
$resource = $this->convertResource($record->getResource());
|
$resource = $this->convertResource($record->getResource());
|
||||||
}
|
}
|
||||||
if (!$scope) {
|
$key = $this->scopeKey($record->getInstrumentationScope());
|
||||||
$scope = $this->convertInstrumentationScope($record->getInstrumentationScope());
|
if (!array_key_exists($key, $scopes)) {
|
||||||
$scope['logs'] = [];
|
$scopes[$key] = $this->convertInstrumentationScope($record->getInstrumentationScope());
|
||||||
}
|
}
|
||||||
$scope['logs'][] = $this->convertLogRecord($record);
|
$scopes[$key]['logs'][] = $this->convertLogRecord($record);
|
||||||
}
|
}
|
||||||
$output = [
|
$output = [
|
||||||
'resource' => $resource,
|
'resource' => $resource,
|
||||||
'scope' => $scope,
|
'scopes' => array_values($scopes),
|
||||||
];
|
];
|
||||||
$this->transport->send(json_encode($output, JSON_PRETTY_PRINT));
|
$this->transport->send(json_encode($output, JSON_PRETTY_PRINT));
|
||||||
|
|
||||||
|
|
@ -86,6 +86,12 @@ class ConsoleExporter implements LogRecordExporterInterface
|
||||||
'dropped_attributes_count' => $resource->getAttributes()->getDroppedAttributesCount(),
|
'dropped_attributes_count' => $resource->getAttributes()->getDroppedAttributesCount(),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function scopeKey(InstrumentationScopeInterface $scope): string
|
||||||
|
{
|
||||||
|
return serialize([$scope->getName(), $scope->getVersion(), $scope->getSchemaUrl(), $scope->getAttributes()]);
|
||||||
|
}
|
||||||
|
|
||||||
private function convertInstrumentationScope(InstrumentationScopeInterface $scope): array
|
private function convertInstrumentationScope(InstrumentationScopeInterface $scope): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
|
|
@ -94,6 +100,7 @@ class ConsoleExporter implements LogRecordExporterInterface
|
||||||
'attributes' => $scope->getAttributes()->toArray(),
|
'attributes' => $scope->getAttributes()->toArray(),
|
||||||
'dropped_attributes_count' => $scope->getAttributes()->getDroppedAttributesCount(),
|
'dropped_attributes_count' => $scope->getAttributes()->getDroppedAttributesCount(),
|
||||||
'schema_url' => $scope->getSchemaUrl(),
|
'schema_url' => $scope->getSchemaUrl(),
|
||||||
|
'logs' => [],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue