Skip to content

Commit

Permalink
implement events v1.32 (open-telemetry#1291)
Browse files Browse the repository at this point in the history
* implement events v1.32
implement the events api + sdk per spec v1.32:
- event logger is now only retrievable via an event logger provider
- domain attribute for events is removed
- events accept a subset of logrecord params, rather than an entire logrecord

* convert severity to a backed enum

* lint

* remove instead of deprecating logEvent, mark Logger constructor as internal

* make severity an enum only

* event attributes to iterable

* inject ClockInterface, add CachedInstrumentation, update examples

* set correct defaults for events

* test coverage

* Revert "make severity an enum only"

This reverts commit 7108229.
Also, move PSR-3 mapping into the Severity enum.

* event attributes to iterable

* apply review feedback
  • Loading branch information
brettmc authored May 5, 2024
1 parent 8031c78 commit 500f6ee
Show file tree
Hide file tree
Showing 41 changed files with 623 additions and 147 deletions.
7 changes: 1 addition & 6 deletions examples/autoload_sdk.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

namespace OpenTelemetry\Example;

use OpenTelemetry\API\Logs\EventLogger;
use OpenTelemetry\API\Logs\LogRecord;

putenv('OTEL_PHP_AUTOLOAD_ENABLED=true');
putenv('OTEL_TRACES_EXPORTER=otlp');
putenv('OTEL_METRICS_EXPORTER=otlp');
Expand All @@ -24,8 +21,6 @@

$instrumentation->tracer()->spanBuilder('root')->startSpan()->end();
$instrumentation->meter()->createCounter('cnt')->add(1);

$eventLogger = new EventLogger($instrumentation->logger(), 'my-domain');
$eventLogger->logEvent('foo', new LogRecord('hello, otel'));
$instrumentation->eventLogger()->emit('foo', 'hello, otel');

echo 'Finished!' . PHP_EOL;
12 changes: 6 additions & 6 deletions examples/load_config.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

declare(strict_types=1);

use OpenTelemetry\API\Logs\EventLogger;
use OpenTelemetry\API\Logs\LogRecord;
use OpenTelemetry\Config\SDK\Configuration;

require __DIR__ . '/../vendor/autoload.php';
Expand All @@ -18,12 +16,14 @@

$tracer = $sdk->getTracerProvider()->getTracer('demo');
$meter = $sdk->getMeterProvider()->getMeter('demo');
$logger = $sdk->getLoggerProvider()->getLogger('demo');
$eventLogger = $sdk->getEventLoggerProvider()->getEventLogger('demo');

$tracer->spanBuilder('root')->startSpan()->end();
$span = $tracer->spanBuilder('root')->startSpan();
$scope = $span->activate();
$meter->createCounter('cnt')->add(1);

$eventLogger = new EventLogger($logger, 'my-domain');
$eventLogger->logEvent('foo', new LogRecord('hello, otel'));
$eventLogger->emit('foo', 'hello, otel');
$scope->detach();
$span->end();

echo 'Finished!' . PHP_EOL;
8 changes: 2 additions & 6 deletions examples/load_config_env.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

declare(strict_types=1);

use OpenTelemetry\API\Logs\EventLogger;
use OpenTelemetry\API\Logs\LogRecord;
use OpenTelemetry\Config\SDK\Configuration;

require __DIR__ . '/../vendor/autoload.php';
Expand All @@ -23,12 +21,10 @@

$tracer = $sdk->getTracerProvider()->getTracer('demo');
$meter = $sdk->getMeterProvider()->getMeter('demo');
$logger = $sdk->getLoggerProvider()->getLogger('demo');
$eventLogger = $sdk->getEventLoggerProvider()->getEventLogger('demo');

$tracer->spanBuilder('root')->startSpan()->end();
$meter->createCounter('cnt')->add(1);

$eventLogger = new EventLogger($logger, 'my-domain');
$eventLogger->logEvent('foo', new LogRecord('hello, otel'));
$eventLogger->emit('foo', 'hello, otel');

echo 'Finished!' . PHP_EOL;
26 changes: 12 additions & 14 deletions examples/logs/exporters/otlp_grpc.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
namespace OpenTelemetry\Example;

use OpenTelemetry\API\Common\Time\Clock;
use OpenTelemetry\API\Logs\EventLogger;
use OpenTelemetry\API\Logs\LogRecord;
use OpenTelemetry\API\Logs\Severity;
use OpenTelemetry\API\Signals;
use OpenTelemetry\Contrib\Grpc\GrpcTransportFactory;
use OpenTelemetry\Contrib\Otlp\LogsExporter;
use OpenTelemetry\Contrib\Otlp\OtlpUtil;
use Opentelemetry\Proto\Logs\V1\SeverityNumber;
use OpenTelemetry\SDK\Common\Instrumentation\InstrumentationScopeFactory;
use OpenTelemetry\SDK\Logs\EventLoggerProvider;
use OpenTelemetry\SDK\Logs\LoggerProvider;
use OpenTelemetry\SDK\Logs\LogRecordLimitsBuilder;
use OpenTelemetry\SDK\Logs\Processor\BatchLogRecordProcessor;
Expand All @@ -30,19 +29,18 @@
(new LogRecordLimitsBuilder())->build()->getAttributeFactory()
)
);
$logger = $loggerProvider->getLogger('demo', '1.0', 'http://schema.url', ['foo' => 'bar']);
$eventLogger = new EventLogger($logger, 'my-domain');

$eventLogger->logEvent(
'foo',
(new LogRecord(['foo' => 'bar', 'baz' => 'bat', 'msg' => 'hello world']))
->setSeverityText('INFO')
->setSeverityNumber(SeverityNumber::SEVERITY_NUMBER_INFO)
$eventLoggerProvider = new EventLoggerProvider($loggerProvider);
$eventLogger = $eventLoggerProvider->getEventLogger('demo', '1.0', 'http://schema.url', ['foo' => 'bar']);

$eventLogger->emit(
name: 'foo',
payload: ['foo' => 'bar', 'baz' => 'bat', 'msg' => 'hello world'],
severityNumber: Severity::INFO
);

$eventLogger->logEvent(
'foo',
new LogRecord('otel is great')
$eventLogger->emit(
'bar',
'otel is great'
);

$loggerProvider->shutdown();
22 changes: 11 additions & 11 deletions examples/logs/exporters/otlp_http.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace OpenTelemetry\Example;

use OpenTelemetry\API\Logs\EventLogger;
use OpenTelemetry\API\Logs\LogRecord;
use OpenTelemetry\API\Logs\Severity;
use OpenTelemetry\Contrib\Otlp\LogsExporter;
use OpenTelemetry\Contrib\Otlp\OtlpHttpTransportFactory;
use Opentelemetry\Proto\Logs\V1\SeverityNumber;
use OpenTelemetry\SDK\Common\Instrumentation\InstrumentationScopeFactory;
use OpenTelemetry\SDK\Logs\EventLoggerProvider;
use OpenTelemetry\SDK\Logs\LoggerProvider;
use OpenTelemetry\SDK\Logs\LogRecordLimitsBuilder;
use OpenTelemetry\SDK\Logs\Processor\SimpleLogRecordProcessor;
Expand All @@ -27,12 +27,12 @@
(new LogRecordLimitsBuilder())->build()->getAttributeFactory()
)
);
$logger = $loggerProvider->getLogger('demo', '1.0', 'https://opentelemetry.io/schemas/1.7.1', ['foo' => 'bar']);
$eventLogger = new EventLogger($logger, 'my-domain');

$record = (new LogRecord(['foo' => 'bar', 'baz' => 'bat', 'msg' => 'hello world']))
->setSeverityText('INFO')
->setTimestamp((new \DateTime())->getTimestamp() * LogRecord::NANOS_PER_SECOND)
->setSeverityNumber(SeverityNumber::SEVERITY_NUMBER_INFO);

$eventLogger->logEvent('foo', $record);
$eventLoggerProvider = new EventLoggerProvider($loggerProvider);
$eventLogger = $eventLoggerProvider->getEventLogger('demo', '1.0', 'https://opentelemetry.io/schemas/1.7.1', ['foo' => 'bar']);

$eventLogger->emit(
name: 'foo',
payload: ['foo' => 'bar', 'baz' => 'bat', 'msg' => 'hello world'],
timestamp: (new \DateTime())->getTimestamp() * LogRecord::NANOS_PER_SECOND,
severityNumber: Severity::INFO,
);
26 changes: 12 additions & 14 deletions examples/logs/features/batch_exporting.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
namespace OpenTelemetry\Example;

use OpenTelemetry\API\Common\Time\Clock;
use OpenTelemetry\API\Logs\EventLogger;
use OpenTelemetry\API\Logs\LogRecord;
use OpenTelemetry\API\Logs\Severity;
use OpenTelemetry\SDK\Common\Attribute\Attributes;
use OpenTelemetry\SDK\Common\Instrumentation\InstrumentationScopeFactory;
use OpenTelemetry\SDK\Logs\EventLogger;
use OpenTelemetry\SDK\Logs\EventLoggerProvider;
use OpenTelemetry\SDK\Logs\Exporter\ConsoleExporterFactory;
use OpenTelemetry\SDK\Logs\LoggerProvider;
use OpenTelemetry\SDK\Logs\Processor\BatchLogRecordProcessor;
Expand All @@ -22,19 +23,16 @@
),
new InstrumentationScopeFactory(Attributes::factory())
);
$eventLoggerProvider = new EventLoggerProvider($loggerProvider);
//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);

$eventLoggerOne->logEvent('foo', $record);
$eventLoggerOne->logEvent('bar', (new LogRecord('hello world')));
$eventLoggerTwo->logEvent('foo', $record);
$eventLoggerOne = $eventLoggerProvider->getEventLogger('demo', '1.0');
$eventLoggerTwo = $eventLoggerProvider->getEventLogger('demo', '2.0');

$payload = ['foo' => 'bar', 'baz' => 'bat', 'msg' => 'hello world'];

$eventLoggerOne->emit(name: 'foo', payload: $payload, severityNumber: Severity::INFO);
$eventLoggerOne->emit('bar', 'hello world');
$eventLoggerTwo->emit(name: 'foo', payload: $payload, severityNumber: Severity::INFO);

//shut down logger provider
$loggerProvider->shutdown();
17 changes: 7 additions & 10 deletions examples/logs/getting_started.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

declare(strict_types=1);

use OpenTelemetry\API\Logs\EventLogger;
use OpenTelemetry\API\Logs\LogRecord;
use OpenTelemetry\API\Logs\Severity;
use OpenTelemetry\SDK\Common\Attribute\Attributes;
use OpenTelemetry\SDK\Common\Instrumentation\InstrumentationScopeFactory;
use OpenTelemetry\SDK\Logs\EventLoggerProvider;
use OpenTelemetry\SDK\Logs\Exporter\ConsoleExporterFactory;
use OpenTelemetry\SDK\Logs\LoggerProvider;
use OpenTelemetry\SDK\Logs\Processor\SimpleLogRecordProcessor;
Expand All @@ -26,6 +26,7 @@
),
new InstrumentationScopeFactory(Attributes::factory())
);
$eventLoggerProvider = new EventLoggerProvider($loggerProvider);
$tracerProvider = new TracerProvider();
$tracer = $tracerProvider->getTracer('demo-tracer');

Expand All @@ -35,16 +36,12 @@
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
//get an event logger, and emit an event. 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');
$eventLogger = $eventLoggerProvider->getEventLogger('demo', '1.0', 'http://schema.url', ['foo' => 'bar']);
$payload = ['foo' => 'bar', 'baz' => 'bat', 'msg' => 'hello world'];

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

$eventLogger->logEvent('foo', $record);
$eventLogger->emit(name: 'foo', payload: $payload, severityNumber: Severity::INFO);

//end span
$span->end();
Expand Down
8 changes: 5 additions & 3 deletions examples/logs/logger_builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

declare(strict_types=1);

use OpenTelemetry\API\Logs\EventLogger;
use OpenTelemetry\API\Logs\LogRecord;
use OpenTelemetry\SDK\Common\Attribute\Attributes;
use OpenTelemetry\SDK\Logs\Exporter\ConsoleExporterFactory;
Expand All @@ -22,11 +21,14 @@
->build();

$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']))
->setSeverityText('INFO')
->setSeverityNumber(9);

$eventLogger->logEvent('foo', $record);
/**
* Note that Loggers should only be used directly by a log appender.
* @see https://github.com/open-telemetry/opentelemetry-specification/blob/v1.32.0/specification/logs/bridge-api.md#logs-bridge-api
*/
$logger->emit($record);
$loggerProvider->shutdown();
12 changes: 10 additions & 2 deletions src/API/Globals.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use const E_USER_WARNING;
use OpenTelemetry\API\Instrumentation\Configurator;
use OpenTelemetry\API\Instrumentation\ContextKeys;
use OpenTelemetry\API\Logs\EventLoggerProviderInterface;
use OpenTelemetry\API\Logs\LoggerProviderInterface;
use OpenTelemetry\API\Metrics\MeterProviderInterface;
use OpenTelemetry\API\Trace\TracerProviderInterface;
Expand All @@ -31,6 +32,7 @@ public function __construct(
private readonly TracerProviderInterface $tracerProvider,
private readonly MeterProviderInterface $meterProvider,
private readonly LoggerProviderInterface $loggerProvider,
private readonly EventLoggerProviderInterface $eventLoggerProvider,
private readonly TextMapPropagatorInterface $propagator,
) {
}
Expand All @@ -55,6 +57,11 @@ public static function loggerProvider(): LoggerProviderInterface
return Context::getCurrent()->get(ContextKeys::loggerProvider()) ?? self::globals()->loggerProvider;
}

public static function eventLoggerProvider(): EventLoggerProviderInterface
{
return Context::getCurrent()->get(ContextKeys::eventLoggerProvider()) ?? self::globals()->eventLoggerProvider;
}

/**
* @param Closure(Configurator): Configurator $initializer
*
Expand Down Expand Up @@ -95,10 +102,11 @@ private static function globals(): self
$meterProvider = $context->get(ContextKeys::meterProvider());
$propagator = $context->get(ContextKeys::propagator());
$loggerProvider = $context->get(ContextKeys::loggerProvider());
$eventLoggerProvider = $context->get(ContextKeys::eventLoggerProvider());

assert(isset($tracerProvider, $meterProvider, $loggerProvider, $propagator));
assert(isset($tracerProvider, $meterProvider, $loggerProvider, $eventLoggerProvider, $propagator));

return self::$globals = new self($tracerProvider, $meterProvider, $loggerProvider, $propagator);
return self::$globals = new self($tracerProvider, $meterProvider, $loggerProvider, $eventLoggerProvider, $propagator);
}

/**
Expand Down
11 changes: 11 additions & 0 deletions src/API/Instrumentation/CachedInstrumentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace OpenTelemetry\API\Instrumentation;

use OpenTelemetry\API\Globals;
use OpenTelemetry\API\Logs\EventLoggerInterface;
use OpenTelemetry\API\Logs\EventLoggerProviderInterface;
use OpenTelemetry\API\Logs\LoggerInterface;
use OpenTelemetry\API\Logs\LoggerProviderInterface;
use OpenTelemetry\API\Metrics\MeterInterface;
Expand All @@ -29,6 +31,8 @@ final class CachedInstrumentation
private WeakMap $meters;
/** @var WeakMap<LoggerProviderInterface, LoggerInterface> */
private WeakMap $loggers;
/** @var WeakMap<EventLoggerProviderInterface, EventLoggerInterface> */
private WeakMap $eventLoggers;

/**
* @psalm-suppress PropertyTypeCoercion
Expand All @@ -42,6 +46,7 @@ public function __construct(
$this->tracers = new \WeakMap();
$this->meters = new \WeakMap();
$this->loggers = new \WeakMap();
$this->eventLoggers = new \WeakMap();
}

public function tracer(): TracerInterface
Expand All @@ -63,4 +68,10 @@ public function logger(): LoggerInterface

return $this->loggers[$loggerProvider] ??= $loggerProvider->getLogger($this->name, $this->version, $this->schemaUrl, $this->attributes);
}
public function eventLogger(): EventLoggerInterface
{
$eventLoggerProvider = Globals::eventLoggerProvider();

return $this->eventLoggers[$eventLoggerProvider] ??= $eventLoggerProvider->getEventLogger($this->name, $this->version, $this->schemaUrl, $this->attributes);
}
}
17 changes: 16 additions & 1 deletion src/API/Instrumentation/Configurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace OpenTelemetry\API\Instrumentation;

use OpenTelemetry\API\Logs\EventLoggerProviderInterface;
use OpenTelemetry\API\Logs\LoggerProviderInterface;
use OpenTelemetry\API\Logs\NoopEventLoggerProvider;
use OpenTelemetry\API\Logs\NoopLoggerProvider;
use OpenTelemetry\API\Metrics\MeterProviderInterface;
use OpenTelemetry\API\Metrics\Noop\NoopMeterProvider;
Expand All @@ -28,6 +30,7 @@ final class Configurator implements ImplicitContextKeyedInterface
private ?MeterProviderInterface $meterProvider = null;
private ?TextMapPropagatorInterface $propagator = null;
private ?LoggerProviderInterface $loggerProvider = null;
private ?EventLoggerProviderInterface $eventLoggerProvider = null;

private function __construct()
{
Expand All @@ -50,7 +53,8 @@ public static function createNoop(): Configurator
->withTracerProvider(new NoopTracerProvider())
->withMeterProvider(new NoopMeterProvider())
->withPropagator(new NoopTextMapPropagator())
->withLoggerProvider(new NoopLoggerProvider())
->withLoggerProvider(NoopLoggerProvider::getInstance())
->withEventLoggerProvider(new NoopEventLoggerProvider())
;
}

Expand All @@ -75,6 +79,9 @@ public function storeInContext(?ContextInterface $context = null): ContextInterf
if ($this->loggerProvider !== null) {
$context = $context->with(ContextKeys::loggerProvider(), $this->loggerProvider);
}
if ($this->eventLoggerProvider !== null) {
$context = $context->with(ContextKeys::eventLoggerProvider(), $this->eventLoggerProvider);
}

return $context;
}
Expand Down Expand Up @@ -110,4 +117,12 @@ public function withLoggerProvider(?LoggerProviderInterface $loggerProvider): Co

return $self;
}

public function withEventLoggerProvider(?EventLoggerProviderInterface $eventLoggerProvider): Configurator
{
$self = clone $this;
$self->eventLoggerProvider = $eventLoggerProvider;

return $self;
}
}
Loading

0 comments on commit 500f6ee

Please sign in to comment.