improve logs console output (#1060)

support multiple scopes in console log output, and update batch example to illustrate
This commit is contained in:
Brett McBride 2023-07-04 14:46:57 +10:00 committed by GitHub
parent cb45b4a75d
commit 5e827ab7fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 35 deletions

View File

@ -6,52 +6,35 @@ namespace OpenTelemetry\Example;
use OpenTelemetry\API\Logs\EventLogger;
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\Time\ClockFactory;
use OpenTelemetry\SDK\Logs\Exporter\ConsoleExporter;
use OpenTelemetry\SDK\Logs\Exporter\ConsoleExporterFactory;
use OpenTelemetry\SDK\Logs\LoggerProvider;
use OpenTelemetry\SDK\Logs\LogRecordLimitsBuilder;
use OpenTelemetry\SDK\Logs\Processor\BatchLogsProcessor;
use OpenTelemetry\SDK\Trace\TracerProvider;
require __DIR__ . '/../../../vendor/autoload.php';
$loggerProvider = new LoggerProvider(
new BatchLogsProcessor(
new ConsoleExporter(
(new StreamTransportFactory())->create(STDOUT, '')
),
(new ConsoleExporterFactory())->create(),
ClockFactory::getDefault()
),
new InstrumentationScopeFactory(
(new LogRecordLimitsBuilder())->build()->getAttributeFactory()
)
new InstrumentationScopeFactory(Attributes::factory())
);
$tracerProvider = new TracerProvider();
$tracer = $tracerProvider->getTracer('demo-tracer');
//start and activate a span
$span = $tracer->spanBuilder('root')->startSpan();
$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');
//get a logger, and emit a log record from an EventLogger.
$loggerOne = $loggerProvider->getLogger('demo', '1.0');
$loggerTwo = $loggerProvider->getLogger('demo', '2.0');
$eventLoggerOne = new EventLogger($loggerOne, 'my-domain');
$eventLoggerTwo = new EventLogger($loggerTwo, 'my-domain');
$record = (new LogRecord(['foo' => 'bar', 'baz' => 'bat', 'msg' => 'hello world']))
->setSeverityText('INFO')
->setSeverityNumber(9);
$eventLogger->logEvent('foo', $record);
$eventLogger->logEvent('bar', (new LogRecord('hello world')));
//end span
$span->end();
$scope->detach();
$eventLoggerOne->logEvent('foo', $record);
$eventLoggerOne->logEvent('bar', (new LogRecord('hello world')));
$eventLoggerTwo->logEvent('foo', $record);
//shut down logger provider
$loggerProvider->shutdown();

View File

@ -32,20 +32,20 @@ class ConsoleExporter implements LogRecordExporterInterface
public function export(iterable $batch, ?CancellationInterface $cancellation = null): FutureInterface
{
$resource = null;
$scope = null;
$scopes = [];
foreach ($batch as $record) {
if (!$resource) {
$resource = $this->convertResource($record->getResource());
}
if (!$scope) {
$scope = $this->convertInstrumentationScope($record->getInstrumentationScope());
$scope['logs'] = [];
$key = $this->scopeKey($record->getInstrumentationScope());
if (!array_key_exists($key, $scopes)) {
$scopes[$key] = $this->convertInstrumentationScope($record->getInstrumentationScope());
}
$scope['logs'][] = $this->convertLogRecord($record);
$scopes[$key]['logs'][] = $this->convertLogRecord($record);
}
$output = [
'resource' => $resource,
'scope' => $scope,
'scopes' => array_values($scopes),
];
$this->transport->send(json_encode($output, JSON_PRETTY_PRINT));
@ -86,6 +86,12 @@ class ConsoleExporter implements LogRecordExporterInterface
'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
{
return [
@ -94,6 +100,7 @@ class ConsoleExporter implements LogRecordExporterInterface
'attributes' => $scope->getAttributes()->toArray(),
'dropped_attributes_count' => $scope->getAttributes()->getDroppedAttributesCount(),
'schema_url' => $scope->getSchemaUrl(),
'logs' => [],
];
}
}