Skip to content

Commit

Permalink
implement events v1.32
Browse files Browse the repository at this point in the history
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
  • Loading branch information
brettmc committed Apr 26, 2024
1 parent 5a395e0 commit c46b009
Show file tree
Hide file tree
Showing 33 changed files with 481 additions and 107 deletions.
4 changes: 2 additions & 2 deletions examples/autoload_sdk.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace OpenTelemetry\Example;

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

putenv('OTEL_PHP_AUTOLOAD_ENABLED=true');
putenv('OTEL_TRACES_EXPORTER=otlp');
Expand All @@ -25,7 +25,7 @@
$instrumentation->tracer()->spanBuilder('root')->startSpan()->end();
$instrumentation->meter()->createCounter('cnt')->add(1);

$eventLogger = new EventLogger($instrumentation->logger(), 'my-domain');
$eventLogger = new EventLogger($instrumentation->logger());
$eventLogger->logEvent('foo', new LogRecord('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;
20 changes: 9 additions & 11 deletions examples/logs/exporters/otlp_grpc.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@

namespace OpenTelemetry\Example;

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\Common\Time\ClockFactory;
use OpenTelemetry\SDK\Logs\EventLogger;
use OpenTelemetry\SDK\Logs\LoggerProvider;
use OpenTelemetry\SDK\Logs\LogRecordLimitsBuilder;
use OpenTelemetry\SDK\Logs\Processor\BatchLogRecordProcessor;
Expand All @@ -31,18 +30,17 @@
)
);
$logger = $loggerProvider->getLogger('demo', '1.0', 'http://schema.url', ['foo' => 'bar']);
$eventLogger = new EventLogger($logger, 'my-domain');
$eventLogger = new EventLogger($logger);

$eventLogger->logEvent(
'foo',
(new LogRecord(['foo' => 'bar', 'baz' => 'bat', 'msg' => 'hello world']))
->setSeverityText('INFO')
->setSeverityNumber(SeverityNumber::SEVERITY_NUMBER_INFO)
$eventLogger->emit(
name: 'foo',
payload: ['foo' => 'bar', 'baz' => 'bat', 'msg' => 'hello world'],
severityNumber: Severity::INFO
);

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

$loggerProvider->shutdown();
18 changes: 9 additions & 9 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\EventLogger;
use OpenTelemetry\SDK\Logs\LoggerProvider;
use OpenTelemetry\SDK\Logs\LogRecordLimitsBuilder;
use OpenTelemetry\SDK\Logs\Processor\SimpleLogRecordProcessor;
Expand All @@ -28,11 +28,11 @@
)
);
$logger = $loggerProvider->getLogger('demo', '1.0', 'https://opentelemetry.io/schemas/1.7.1', ['foo' => 'bar']);
$eventLogger = new EventLogger($logger, 'my-domain');
$eventLogger = new EventLogger($logger);

$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);
$eventLogger->emit(
name: 'foo',
payload: ['foo' => 'bar', 'baz' => 'bat', 'msg' => 'hello world'],
timestamp: (new \DateTime())->getTimestamp() * LogRecord::NANOS_PER_SECOND,
severityNumber: Severity::INFO,
);
18 changes: 8 additions & 10 deletions examples/logs/features/batch_exporting.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

namespace OpenTelemetry\Example;

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\Common\Time\ClockFactory;
use OpenTelemetry\SDK\Logs\EventLogger;
use OpenTelemetry\SDK\Logs\Exporter\ConsoleExporterFactory;
use OpenTelemetry\SDK\Logs\LoggerProvider;
use OpenTelemetry\SDK\Logs\Processor\BatchLogRecordProcessor;
Expand All @@ -25,16 +25,14 @@
//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');
$eventLoggerOne = new EventLogger($loggerOne);
$eventLoggerTwo = new EventLogger($loggerTwo);

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

$eventLoggerOne->logEvent('foo', $record);
$eventLoggerOne->logEvent('bar', (new LogRecord('hello world')));
$eventLoggerTwo->logEvent('foo', $record);
$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();
20 changes: 20 additions & 0 deletions examples/logs/features/event-logger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace OpenTelemetry\Example;

use OpenTelemetry\API\Logs\Severity;
use OpenTelemetry\SDK\Common\Configuration\Variables;
use OpenTelemetry\SDK\Logs\EventLoggerProviderFactory;
use OpenTelemetry\SDK\Logs\LoggerProviderFactory;

require __DIR__ . '/../../../vendor/autoload.php';
putenv(sprintf('%s=console', Variables::OTEL_LOGS_EXPORTER));
putenv(sprintf('%s=simple', Variables::OTEL_PHP_LOGS_PROCESSOR));

$provider = (new EventLoggerProviderFactory())->create((new LoggerProviderFactory())->create());
$eventLogger = $provider->getEventLogger('my-event-logger', '1.0', 'https://example.com/events');

$payload = ['foo' => 'bar', 'baz' => 'bat', 'msg' => 'hello world'];
$eventLogger->emit(name: 'my-event', payload: $payload, severityNumber: Severity::DEBUG);
12 changes: 7 additions & 5 deletions examples/logs/getting_started.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

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\EventLogger;
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 +28,7 @@
),
new InstrumentationScopeFactory(Attributes::factory())
);
$eventLoggerProvider = new EventLoggerProvider($loggerProvider);
$tracerProvider = new TracerProvider();
$tracer = $tracerProvider->getTracer('demo-tracer');

Expand All @@ -37,14 +40,13 @@

//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');

$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: $record, 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
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(NoopLoggerProvider::getInstance()))
;
}

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;
}
}
11 changes: 11 additions & 0 deletions src/API/Instrumentation/ContextKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace OpenTelemetry\API\Instrumentation;

use OpenTelemetry\API\Logs\EventLoggerProviderInterface;
use OpenTelemetry\API\Logs\LoggerProviderInterface;
use OpenTelemetry\API\Metrics\MeterProviderInterface;
use OpenTelemetry\API\Trace\TracerProviderInterface;
Expand Down Expand Up @@ -55,4 +56,14 @@ public static function loggerProvider(): ContextKeyInterface

return $instance ??= Context::createKey(LoggerProviderInterface::class);
}

/**
* @return ContextKeyInterface<EventLoggerProviderInterface>
*/
public static function eventLoggerProvider(): ContextKeyInterface
{
static $instance;

return $instance ??= Context::createKey(EventLoggerProviderInterface::class);
}
}
Loading

0 comments on commit c46b009

Please sign in to comment.