Skip to content

Commit

Permalink
make severity an enum only
Browse files Browse the repository at this point in the history
  • Loading branch information
brettmc committed Apr 28, 2024
1 parent ee1b360 commit 7108229
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 20 deletions.
3 changes: 2 additions & 1 deletion examples/logs/logger_builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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 @@ -24,7 +25,7 @@

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

/**
* 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|int|null $severityNumber = null,
?Severity $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|int $severityNumber): self
public function setSeverityNumber(Severity $severityNumber): self
{
$this->severityNumber = ($severityNumber instanceof Severity) ? $severityNumber->value : $severityNumber;
$this->severityNumber = $severityNumber->value;

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

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 number
* 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 severityNumber(string $level): int
public static function severityNumber(string $level): Severity
{
return match (strtolower($level)) {
LogLevel::DEBUG => 5,
LogLevel::INFO => 9,
LogLevel::NOTICE => 10,
LogLevel::WARNING => 13,
LogLevel::ERROR => 17,
LogLevel::CRITICAL => 18,
LogLevel::ALERT => 19,
LogLevel::EMERGENCY => 21,
default => 0,
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),
};
}
}
4 changes: 2 additions & 2 deletions src/SDK/Logs/EventLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function emit(
mixed $payload = null,
?int $timestamp = null,
?ContextInterface $context = null,
Severity|int|null $severityNumber = null,
?Severity $severityNumber = null,
?array $attributes = [],
): void {
$logRecord = new LogRecord();
Expand All @@ -35,7 +35,7 @@ public function emit(
$payload && $logRecord->setBody($payload);
$logRecord->setTimestamp($timestamp ?? (int) (microtime(true)*LogRecord::NANOS_PER_SECOND));
$context && $logRecord->setContext($context);
$logRecord->setSeverityNumber($severityNumber ?? Severity::INFO);
$severityNumber && $logRecord->setSeverityNumber($severityNumber);

$this->logger->emit($logRecord);
}
Expand Down
1 change: 0 additions & 1 deletion tests/Unit/API/Logs/LogRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ 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
7 changes: 6 additions & 1 deletion tests/Unit/API/Logs/Map/Psr3Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ public static function levelProvider(): array
[LogLevel::NOTICE],
[LogLevel::INFO],
[LogLevel::DEBUG],
['unknown'],
];
}

public function test_unknown_value_error(): void
{
$this->expectException(\InvalidArgumentException::class);
Psr3::severityNumber('unknown');
}
}
3 changes: 2 additions & 1 deletion tests/Unit/SDK/Logs/ReadableLogRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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 @@ -39,7 +40,7 @@ public function setUp(): void
public function test_getters(): void
{
$logRecord = (new LogRecord('body'))
->setSeverityNumber(5)
->setSeverityNumber(Severity::DEBUG)
->setSeverityText('info')
->setTimestamp(11)
->setObservedTimestamp(22)
Expand Down

0 comments on commit 7108229

Please sign in to comment.