Skip to content

Commit

Permalink
Revert "make severity an enum only"
Browse files Browse the repository at this point in the history
This reverts commit 7108229.
Also, move PSR-3 mapping into the Severity enum.
  • Loading branch information
brettmc committed Apr 30, 2024
1 parent 9b20ddb commit fdff961
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 29 deletions.
3 changes: 1 addition & 2 deletions examples/logs/logger_builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
declare(strict_types=1);

use OpenTelemetry\API\Logs\LogRecord;
use OpenTelemetry\API\Logs\Severity;
use OpenTelemetry\SDK\Common\Attribute\Attributes;
use OpenTelemetry\SDK\Logs\Exporter\ConsoleExporterFactory;
use OpenTelemetry\SDK\Logs\LoggerProvider;
Expand All @@ -25,7 +24,7 @@

$record = (new LogRecord(['foo' => 'bar', 'baz' => 'bat', 'msg' => 'hello world']))
->setSeverityText('INFO')
->setSeverityNumber(Severity::INFO);
->setSeverityNumber(9);

/**
* Note that Loggers should only be used directly by a log appender.
Expand Down
2 changes: 1 addition & 1 deletion src/API/Logs/EventLoggerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function emit(
mixed $payload = null,
?int $timestamp = null,
?ContextInterface $context = null,
?Severity $severityNumber = null,
Severity|int|null $severityNumber = null,
array $attributes = [],
): void;
}
4 changes: 2 additions & 2 deletions src/API/Logs/LogRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ public function setContext(?ContextInterface $context = null): self
/**
* @see https://opentelemetry.io/docs/reference/specification/logs/data-model/#field-severitynumber
*/
public function setSeverityNumber(Severity $severityNumber): self
public function setSeverityNumber(Severity|int $severityNumber): self
{
$this->severityNumber = $severityNumber->value;
$this->severityNumber = ($severityNumber instanceof Severity) ? $severityNumber->value : $severityNumber;

return $this;
}
Expand Down
17 changes: 2 additions & 15 deletions src/API/Logs/Map/Psr3.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,17 @@

namespace OpenTelemetry\API\Logs\Map;

use InvalidArgumentException;
use OpenTelemetry\API\Logs\Severity;
use Psr\Log\LogLevel;

class Psr3
{
/**
* Maps PSR-3 severity level (string) to the appropriate opentelemetry severity
*
* @see https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/logs/data-model-appendix.md#appendix-b-severitynumber-example-mappings
* @see https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/logs/data-model.md#field-severitynumber
* @deprecated Use Severity::fromPsr3
*/
public static function severityNumber(string $level): Severity
{
return match (strtolower($level)) {
LogLevel::DEBUG => Severity::DEBUG,
LogLevel::INFO => Severity::INFO,
LogLevel::NOTICE => Severity::INFO2,
LogLevel::WARNING => Severity::WARN,
LogLevel::ERROR => Severity::ERROR,
LogLevel::CRITICAL => Severity::ERROR2,
LogLevel::ALERT => Severity::ERROR3,
LogLevel::EMERGENCY => Severity::FATAL,
default => throw new InvalidArgumentException('Unknown severity: ' . $level),
};
return Severity::fromPsr3($level);
}
}
24 changes: 24 additions & 0 deletions src/API/Logs/Severity.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace OpenTelemetry\API\Logs;

use Psr\Log\LogLevel;
use ValueError;

enum Severity: int
{
case TRACE = 1;
Expand All @@ -30,4 +33,25 @@ enum Severity: int
case FATAL2 = 22;
case FATAL3 = 23;
case FATAL4 = 24;

/**
* Maps PSR-3 severity level (string) to the appropriate opentelemetry severity
*
* @see https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/logs/data-model-appendix.md#appendix-b-severitynumber-example-mappings
* @see https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/logs/data-model.md#field-severitynumber
*/
public static function fromPsr3(string $level): self
{
return match (strtolower($level)) {
LogLevel::DEBUG => Severity::DEBUG,
LogLevel::INFO => Severity::INFO,
LogLevel::NOTICE => Severity::INFO2,
LogLevel::WARNING => Severity::WARN,
LogLevel::ERROR => Severity::ERROR,
LogLevel::CRITICAL => Severity::ERROR2,
LogLevel::ALERT => Severity::ERROR3,
LogLevel::EMERGENCY => Severity::FATAL,
default => throw new ValueError('Unknown severity: ' . $level),
};
}
}
2 changes: 1 addition & 1 deletion src/SDK/Logs/EventLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function emit(
mixed $payload = null,
?int $timestamp = null,
?ContextInterface $context = null,
?Severity $severityNumber = null,
Severity|int|null $severityNumber = null,
array $attributes = [],
): void {
$logRecord = new LogRecord();
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/API/Logs/LogRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public static function settersProvider(): array
return [
['setBody', 'body', 'foo'],
['setAttributes', 'attributes', ['foo' => 'bar']],
['setSeverityNumber', 'severityNumber', 5],
['setSeverityNumber', 'severityNumber', Severity::ERROR, Severity::ERROR->value],
['setSeverityText', 'severityText', 'info'],
['setObservedTimestamp', 'observedTimestamp', 999],
Expand Down
6 changes: 0 additions & 6 deletions tests/Unit/API/Logs/Map/Psr3Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,4 @@ public static function levelProvider(): array
[LogLevel::DEBUG],
];
}

public function test_unknown_value_error(): void
{
$this->expectException(\InvalidArgumentException::class);
Psr3::severityNumber('unknown');
}
}
44 changes: 44 additions & 0 deletions tests/Unit/API/Logs/SeverityTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace OpenTelemetry\Tests\Unit\API\Logs;

use OpenTelemetry\API\Logs\Severity;
use PHPUnit\Framework\TestCase;
use Psr\Log\LogLevel;
use ValueError;

/**
* @covers \OpenTelemetry\API\Logs\Severity
*/
class SeverityTest extends TestCase
{
public function test_value_error(): void
{
$this->expectException(ValueError::class);
Severity::fromPsr3('unknown');
}

/**
* @dataProvider levelProvider
*/
public function test_severity_number(string $level): void
{
$this->assertNotNull(Severity::fromPsr3($level));
}

public static function levelProvider(): array
{
return [
[LogLevel::EMERGENCY],
[LogLevel::ALERT],
[LogLevel::CRITICAL],
[LogLevel::ERROR],
[LogLevel::WARNING],
[LogLevel::NOTICE],
[LogLevel::INFO],
[LogLevel::DEBUG],
];
}
}
3 changes: 1 addition & 2 deletions tests/Unit/SDK/Logs/ReadableLogRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace OpenTelemetry\Tests\Unit\SDK\Logs;

use OpenTelemetry\API\Logs\LogRecord;
use OpenTelemetry\API\Logs\Severity;
use OpenTelemetry\Context\ContextInterface;
use OpenTelemetry\SDK\Common\Attribute\AttributesFactory;
use OpenTelemetry\SDK\Common\Instrumentation\InstrumentationScopeInterface;
Expand Down Expand Up @@ -40,7 +39,7 @@ public function setUp(): void
public function test_getters(): void
{
$logRecord = (new LogRecord('body'))
->setSeverityNumber(Severity::DEBUG)
->setSeverityNumber(5)
->setSeverityText('info')
->setTimestamp(11)
->setObservedTimestamp(22)
Expand Down

0 comments on commit fdff961

Please sign in to comment.