Skip to content

Commit

Permalink
set correct defaults for events
Browse files Browse the repository at this point in the history
  • Loading branch information
brettmc committed Apr 29, 2024
1 parent 49724d3 commit 829afad
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/API/Logs/EventLoggerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ public function emit(
?int $timestamp = null,
?ContextInterface $context = null,
?Severity $severityNumber = null,
iterable $attributes = [],
array $attributes = [],
): void;
}
11 changes: 7 additions & 4 deletions src/SDK/Logs/EventLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use OpenTelemetry\API\Logs\LoggerInterface;
use OpenTelemetry\API\Logs\LogRecord;
use OpenTelemetry\API\Logs\Severity;
use OpenTelemetry\Context\Context;
use OpenTelemetry\Context\ContextInterface;

class EventLogger implements EventLoggerInterface
Expand All @@ -22,20 +23,22 @@ public function __construct(
) {
}

/**
* @see https://github.com/open-telemetry/opentelemetry-specification/blob/v1.32.0/specification/logs/event-sdk.md#emit-event
*/
public function emit(
string $name,
mixed $payload = null,
?int $timestamp = null,
?ContextInterface $context = null,
?Severity $severityNumber = null,
iterable $attributes = [],
array $attributes = [],
): void {
$logRecord = new LogRecord();
$logRecord->setAttribute('event.name', $name);
$logRecord->setAttributes($attributes);
$logRecord->setAttributes(['event.name' => $name] + $attributes);
$logRecord->setBody($payload);
$logRecord->setTimestamp($timestamp ?? $this->clock->now());
$context && $logRecord->setContext($context);
$logRecord->setContext($context ?? Context::getCurrent());
$logRecord->setSeverityNumber($severityNumber ?? Severity::INFO);

$this->logger->emit($logRecord);
Expand Down
76 changes: 69 additions & 7 deletions tests/Unit/SDK/Logs/EventLoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,50 @@

declare(strict_types=1);

namespace Unit\SDK\Logs;
namespace OpenTelemetry\Tests\Unit\SDK\Logs;

use OpenTelemetry\API\Common\Time\Clock;
use OpenTelemetry\API\Common\Time\TestClock;
use OpenTelemetry\API\Logs\LoggerInterface;
use OpenTelemetry\API\Logs\LoggerProviderInterface;
use OpenTelemetry\API\Logs\LogRecord;
use OpenTelemetry\API\Logs\Severity;
use OpenTelemetry\Context\Context;
use OpenTelemetry\SDK\Logs\EventLoggerProvider;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

/**
* @covers \OpenTelemetry\SDK\Logs\EventLogger
*/
class EventLoggerTest extends TestCase
{
public function test_emit(): void
private LoggerInterface&MockObject $logger;
private EventLoggerProvider $eventLoggerProvider;
private TestClock $clock;

public function setUp(): void
{
$this->clock = new TestClock();
Clock::setDefault($this->clock);
$this->logger = $this->createMock(LoggerInterface::class);
$loggerProvider = $this->createMock(LoggerProviderInterface::class);
$logger = $this->createMock(LoggerInterface::class);
$logger->expects($this->once())->method('emit')->with($this->callback(function (LogRecord $logRecord) {
$loggerProvider->method('getLogger')->willReturn($this->logger);
$this->eventLoggerProvider = new EventLoggerProvider($loggerProvider);
}

public function tearDown(): void
{
Clock::reset();
}

public function test_emit(): void
{
$this->logger->expects($this->once())->method('emit')->with($this->callback(function (LogRecord $logRecord) {
$expected = (new LogRecord('some.payload'))
->setSeverityNumber(Severity::ERROR)
->setTimestamp(123456)
->setContext(Context::getCurrent())
->setAttributes([
'event.name' => 'my.event',
'bar' => 'bar',
Expand All @@ -32,10 +54,50 @@ public function test_emit(): void

return true;
}));
$loggerProvider->expects($this->once())->method('getLogger')->willReturn($logger);
$eventLoggerProvider = new EventLoggerProvider($loggerProvider);
$eventLogger = $eventLoggerProvider->getEventLogger('event.logger', '1.0', 'https://example.org/schema', ['foo' => 'foo']);

$eventLogger = $this->eventLoggerProvider->getEventLogger('event.logger', '1.0', 'https://example.org/schema', ['foo' => 'foo']);
$eventLogger->emit('my.event', 'some.payload', 123456, severityNumber: Severity::ERROR, attributes: ['bar' => 'bar']);
}

public function test_default_values(): void
{
$this->logger->expects($this->once())->method('emit')->with($this->callback(function (LogRecord $logRecord) {
$expected = (new LogRecord())
->setSeverityNumber(Severity::INFO)
->setTimestamp($this->clock->now())
->setContext(Context::getCurrent())
->setAttributes([
'event.name' => 'my.event',
]);
$this->assertEquals($expected, $logRecord);

return true;
}));

$eventLogger = $this->eventLoggerProvider->getEventLogger('event.logger');
$eventLogger->emit('my.event');
}

/**
* "The user provided Attributes MUST not take over the event.name attribute"
* @see https://github.com/open-telemetry/opentelemetry-specification/blob/v1.32.0/specification/logs/event-sdk.md#emit-event
*/
public function test_event_name_attribute_is_ignored(): void
{
$this->logger->expects($this->once())->method('emit')->with($this->callback(function (LogRecord $logRecord) {
$expected = (new LogRecord())
->setSeverityNumber(Severity::INFO)
->setTimestamp($this->clock->now())
->setContext(Context::getCurrent())
->setAttributes([
'event.name' => 'my.event',
]);
$this->assertEquals($expected, $logRecord);

return true;
}));

$eventLogger = $this->eventLoggerProvider->getEventLogger('event.logger');
$eventLogger->emit('my.event', attributes: ['event.name' => 'not.my.event']);
}
}

0 comments on commit 829afad

Please sign in to comment.