Skip to content

Commit

Permalink
update sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
cappuc committed Oct 6, 2024
1 parent 02721d3 commit c1a48d1
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 69 deletions.
45 changes: 25 additions & 20 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,30 @@
"php": "^8.1",
"illuminate/contracts": "^10.0 || ^11.0",
"illuminate/support": "^10.0 || ^11.0",
"open-telemetry/context": "^1.0.1",
"open-telemetry/exporter-otlp": "^1.0",
"open-telemetry/sdk": "^1.0.1",
"open-telemetry/sem-conv": "^1.23",
"open-telemetry/context": "^1.1",
"open-telemetry/exporter-otlp": "^1.1",
"open-telemetry/sdk": "^1.1",
"open-telemetry/sem-conv": "^1.27",
"spatie/laravel-package-tools": "^1.16",
"thecodingmachine/safe": "^2.0"
},
"require-dev": {
"guzzlehttp/guzzle": "^7.3",
"guzzlehttp/test-server": "^0.1.0",
"larastan/larastan": "^2.7",
"laravel/pint": "^1.2",
"larastan/larastan": "^2.9",
"laravel/pint": "^1.18",
"nesbot/carbon": "^2.69 || ^3.0",
"nunomaduro/collision": "^7.0 || ^8.0",
"open-telemetry/exporter-zipkin": "^1.0",
"open-telemetry/extension-propagator-b3": "^1.0",
"open-telemetry/transport-grpc": "^1.0",
"open-telemetry/exporter-zipkin": "^1.1",
"open-telemetry/extension-propagator-b3": "^1.1",
"open-telemetry/transport-grpc": "^1.1",
"orchestra/testbench": "^8.22 || ^9.0",
"pestphp/pest": "^2.0",
"pestphp/pest-plugin-laravel": "^2.0",
"pestphp/pest": "^3.2",
"pestphp/pest-plugin-laravel": "^3.0",
"php-http/guzzle7-adapter": "^1.0",
"phpstan/extension-installer": "^1.3",
"phpstan/phpstan": "^1.10.51",
"phpstan/phpstan-deprecation-rules": "^1.1",
"phpstan/extension-installer": "^1.4",
"phpstan/phpstan": "^1.12.5",
"phpstan/phpstan-deprecation-rules": "^1.2",
"predis/predis": "^2.2",
"spatie/invade": "^2.0",
"spatie/test-time": "^1.3",
Expand All @@ -55,9 +55,9 @@
"open-telemetry/transport-grpc": "Required to use OTLP gRPC exporter"
},
"conflict": {
"open-telemetry/extension-propagator-b3": "<1.0.0",
"open-telemetry/exporter-zipkin": "<1.0.0",
"open-telemetry/transport-grpc": "<1.0.0"
"open-telemetry/extension-propagator-b3": "<1.1.0",
"open-telemetry/exporter-zipkin": "<1.1.0",
"open-telemetry/transport-grpc": "<1.1.0"
},
"autoload": {
"psr-4": {
Expand All @@ -70,16 +70,21 @@
}
},
"scripts": {
"test": "vendor/bin/pest --no-coverage",
"test-coverage": "vendor/bin/pest --coverage-html coverage"
"test": "pest --no-coverage",
"test-coverage": "pest --coverage-html coverage",
"lint": [
"pint",
"phpstan"
]
},
"config": {
"sort-packages": true,
"allow-plugins": {
"composer/package-versions-deprecated": true,
"pestphp/pest-plugin": true,
"php-http/discovery": true,
"phpstan/extension-installer": true
"phpstan/extension-installer": true,
"tbachert/spi": true
}
},
"extra": {
Expand Down
7 changes: 3 additions & 4 deletions src/Instrumentation/QueryInstrumentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@ public function recordQuery(QueryExecuted $event): void
->setSpanKind(SpanKind::KIND_CLIENT)
->setStartTimestamp($this->getEventStartTimestampNs($event->time))
->setAttribute(TraceAttributes::DB_SYSTEM, $event->connection->getDriverName())
->setAttribute(TraceAttributes::DB_NAME, $event->connection->getDatabaseName())
->setAttribute(TraceAttributes::DB_OPERATION, $operationName)
->setAttribute(TraceAttributes::DB_STATEMENT, $event->sql)
->setAttribute(TraceAttributes::DB_USER, $event->connection->getConfig('username'))
->setAttribute(TraceAttributes::DB_NAMESPACE, $event->connection->getDatabaseName())
->setAttribute(TraceAttributes::DB_OPERATION_NAME, $operationName)
->setAttribute(TraceAttributes::DB_QUERY_TEXT, $event->sql)
->setAttribute(TraceAttributes::SERVER_ADDRESS, $event->connection->getConfig('host'))
->setAttribute(TraceAttributes::SERVER_PORT, $event->connection->getConfig('port'))
->start();
Expand Down
8 changes: 4 additions & 4 deletions src/Instrumentation/QueueInstrumentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ protected function registerQueueInterceptor(QueueManager $queue): void
$span = Tracer::newSpan(sprintf('%s enqueue', $jobName))
->setSpanKind(SpanKind::KIND_PRODUCER)
->setAttribute(TraceAttributes::MESSAGING_SYSTEM, $this->connectionDriver($connection))
->setAttribute(TraceAttributes::MESSAGING_OPERATION, 'enqueue')
->setAttribute(TraceAttributes::MESSAGE_ID, $uuid)
->setAttribute(TraceAttributes::MESSAGING_OPERATION_TYPE, 'enqueue')
->setAttribute(TraceAttributes::RPC_MESSAGE_ID, $uuid)
->setAttribute(TraceAttributes::MESSAGING_DESTINATION_NAME, $queueName)
->setAttribute(TraceAttributes::MESSAGING_DESTINATION_TEMPLATE, $jobName)
->start();
Expand All @@ -96,8 +96,8 @@ protected function recordJobProcessing(): void
->setSpanKind(SpanKind::KIND_CONSUMER)
->setParent($context)
->setAttribute(TraceAttributes::MESSAGING_SYSTEM, $this->connectionDriver($event->connectionName))
->setAttribute(TraceAttributes::MESSAGING_OPERATION, 'process')
->setAttribute(TraceAttributes::MESSAGE_ID, $event->job->uuid())
->setAttribute(TraceAttributes::MESSAGING_OPERATION_TYPE, 'process')
->setAttribute(TraceAttributes::RPC_MESSAGE_ID, $event->job->uuid())
->setAttribute(TraceAttributes::MESSAGING_DESTINATION_NAME, $event->job->getQueue())
->setAttribute(TraceAttributes::MESSAGING_DESTINATION_TEMPLATE, $event->job->resolveName())
->start();
Expand Down
2 changes: 1 addition & 1 deletion src/Instrumentation/RedisInstrumentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function recordCommand(CommandExecuted $event): void

if ($span->isRecording()) {
$span->setAttribute(TraceAttributes::DB_SYSTEM, 'redis')
->setAttribute(TraceAttributes::DB_STATEMENT, $this->formatCommand($event->command, $event->parameters))
->setAttribute(TraceAttributes::DB_QUERY_TEXT, $this->formatCommand($event->command, $event->parameters))
->setAttribute(TraceAttributes::SERVER_ADDRESS, $this->resolveRedisAddress($event->connection->client()));
}

Expand Down
6 changes: 3 additions & 3 deletions src/Instrumentation/SpanTimeAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

namespace Keepsuit\LaravelOpenTelemetry\Instrumentation;

use OpenTelemetry\SDK\Common\Time\ClockFactory;
use OpenTelemetry\SDK\Common\Time\ClockInterface;
use OpenTelemetry\API\Common\Time\Clock;
use OpenTelemetry\API\Common\Time\ClockInterface;

trait SpanTimeAdapter
{
protected function getEventStartTimestampNs(float $timeMs): int
{
$nowNs = ClockFactory::getDefault()->now();
$nowNs = Clock::getDefault()->now();
$durationNs = (int) ($timeMs * ClockInterface::NANOS_PER_MILLISECOND);

return $nowNs - $durationNs;
Expand Down
14 changes: 10 additions & 4 deletions src/LaravelOpenTelemetryServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Keepsuit\LaravelOpenTelemetry;

use Composer\InstalledVersions;
use Http\Discovery\Psr17FactoryDiscovery;
use Http\Discovery\Psr18ClientDiscovery;
use Illuminate\Config\Repository;
use Illuminate\Foundation\Application;
use Illuminate\Support\Arr;
Expand All @@ -12,6 +14,7 @@
use Keepsuit\LaravelOpenTelemetry\Support\OpenTelemetryMonologHandler;
use Keepsuit\LaravelOpenTelemetry\Support\PropagatorBuilder;
use Keepsuit\LaravelOpenTelemetry\Support\SamplerBuilder;
use OpenTelemetry\API\Common\Time\Clock;
use OpenTelemetry\API\Instrumentation\CachedInstrumentation;
use OpenTelemetry\API\Logs\LoggerInterface;
use OpenTelemetry\API\Signals;
Expand All @@ -28,7 +31,6 @@
use OpenTelemetry\SDK\Common\Configuration\Variables as OTELVariables;
use OpenTelemetry\SDK\Common\Export\Http\PsrTransportFactory;
use OpenTelemetry\SDK\Common\Export\TransportInterface;
use OpenTelemetry\SDK\Common\Time\ClockFactory;
use OpenTelemetry\SDK\Logs\Exporter\ConsoleExporterFactory as LogsConsoleExporterFactory;
use OpenTelemetry\SDK\Logs\Exporter\InMemoryExporterFactory as LogsInMemoryExporterFactory;
use OpenTelemetry\SDK\Logs\LoggerProvider;
Expand Down Expand Up @@ -66,7 +68,7 @@ public function configurePackage(Package $package): void

protected function init(): void
{
ClockFactory::setDefault(new CarbonClock);
Clock::setDefault(new CarbonClock);

$resource = ResourceInfoFactory::defaultResource()->merge(
ResourceInfo::create(Attributes::create([
Expand Down Expand Up @@ -103,7 +105,7 @@ protected function init(): void
$this->app->bind(LogRecordExporterInterface::class, fn () => $logExporter);
$logProcessor = new BatchLogRecordProcessor(
exporter: $logExporter,
clock: ClockFactory::getDefault()
clock: Clock::getDefault()
);

$loggerProvider = LoggerProvider::builder()
Expand Down Expand Up @@ -163,7 +165,11 @@ protected function buildSpanExporter(): SpanExporterInterface

return match ($tracesExporterDriver) {
'zipkin' => new ZipkinExporter(
PsrTransportFactory::discover()->create(
(new PsrTransportFactory(
Psr18ClientDiscovery::find(),
Psr17FactoryDiscovery::findRequestFactory(),
Psr17FactoryDiscovery::findStreamFactory(),
))->create(
Str::of(Arr::get($tracesExporterConfig ?? [], 'endpoint'))->rtrim('/')->append('/api/v2/spans')->toString(),
'application/json'
),
Expand Down
8 changes: 4 additions & 4 deletions src/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace Keepsuit\LaravelOpenTelemetry;

use OpenTelemetry\API\Common\Time\Clock;
use OpenTelemetry\API\Logs\LoggerInterface;
use OpenTelemetry\API\Logs\LogRecord;
use OpenTelemetry\API\Logs\Map\Psr3;
use OpenTelemetry\SDK\Common\Time\ClockFactory;
use OpenTelemetry\API\Logs\Severity;
use Psr\Log\LogLevel;

class Logger
Expand Down Expand Up @@ -60,8 +60,8 @@ public function debug(string $message, array $context = []): void
public function log(string $level, string $message, array $context = []): void
{
$logRecord = (new LogRecord($message))
->setTimestamp(ClockFactory::getDefault()->now())
->setSeverityNumber(Psr3::severityNumber($level))
->setTimestamp(Clock::getDefault()->now())
->setSeverityNumber(Severity::fromPsr3($level))
->setSeverityText($level);

foreach ($context as $key => $value) {
Expand Down
4 changes: 2 additions & 2 deletions src/Support/CarbonClock.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
use Carbon\Carbon;
use Carbon\CarbonImmutable;
use Carbon\CarbonInterface;
use OpenTelemetry\SDK\Common\Time\ClockInterface;
use OpenTelemetry\SDK\Common\Time\SystemClock;
use OpenTelemetry\API\Common\Time\ClockInterface;
use OpenTelemetry\API\Common\Time\SystemClock;

class CarbonClock implements ClockInterface
{
Expand Down
22 changes: 12 additions & 10 deletions tests/Instrumentation/QueryInstrumentationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
expect($span)
->getName()->toBe('sql SELECT')
->getKind()->toBe(SpanKind::KIND_CLIENT)
->getAttributes()->toArray()->toMatchArray([
->getAttributes()->toArray()->toBe([
'db.system' => 'sqlite',
'db.name' => ':memory:',
'db.operation' => 'SELECT',
'db.statement' => 'select * from "users"',
'db.namespace' => ':memory:',
'db.operation.name' => 'SELECT',
'db.query.text' => 'select * from "users"',
])
->hasEnded()->toBeTrue()
->getEndEpochNanos()->toBeLessThan(ClockFactory::getDefault()->now());
Expand All @@ -49,10 +49,11 @@
expect($span)
->getName()->toBe('sql SELECT')
->getKind()->toBe(SpanKind::KIND_CLIENT)
->getAttributes()->toArray()->toMatchArray([
->getAttributes()->toArray()->toBe([
'db.system' => 'sqlite',
'db.name' => ':memory:',
'db.statement' => 'select * from "users" where "id" = ? and "name" like ?',
'db.namespace' => ':memory:',
'db.operation.name' => 'SELECT',
'db.query.text' => 'select * from "users" where "id" = ? and "name" like ?',
]);
});

Expand All @@ -74,9 +75,10 @@
expect($span)
->getName()->toBe('sql UPDATE')
->getKind()->toBe(SpanKind::KIND_CLIENT)
->getAttributes()->toArray()->toMatchArray([
->getAttributes()->toArray()->toBe([
'db.system' => 'sqlite',
'db.name' => ':memory:',
'db.statement' => 'update "users" set "name" = :name where admin = true',
'db.namespace' => ':memory:',
'db.operation.name' => 'UPDATE',
'db.query.text' => 'update "users" set "name" = :name where admin = true',
]);
});
16 changes: 8 additions & 8 deletions tests/Instrumentation/QueueInstrumentationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@
expect($enqueueSpan)
->getAttributes()->toMatchArray([
TraceAttributes::MESSAGING_SYSTEM => 'redis',
TraceAttributes::MESSAGING_OPERATION => 'enqueue',
TraceAttributes::MESSAGE_ID => $this->valuestore->get('uuid'),
TraceAttributes::MESSAGING_OPERATION_TYPE => 'enqueue',
TraceAttributes::RPC_MESSAGE_ID => $this->valuestore->get('uuid'),
TraceAttributes::MESSAGING_DESTINATION_NAME => 'default',
TraceAttributes::MESSAGING_DESTINATION_TEMPLATE => TestJob::class,
]);
Expand All @@ -69,8 +69,8 @@
->not->toBeNull()
->getAttributes()->toMatchArray([
TraceAttributes::MESSAGING_SYSTEM => 'redis',
TraceAttributes::MESSAGING_OPERATION => 'process',
TraceAttributes::MESSAGE_ID => $this->valuestore->get('uuid'),
TraceAttributes::MESSAGING_OPERATION_TYPE => 'process',
TraceAttributes::RPC_MESSAGE_ID => $this->valuestore->get('uuid'),
TraceAttributes::MESSAGING_DESTINATION_NAME => 'default',
TraceAttributes::MESSAGING_DESTINATION_TEMPLATE => TestJob::class,
]);
Expand Down Expand Up @@ -145,8 +145,8 @@
->getStatus()->getCode()->toBe(StatusCode::STATUS_UNSET)
->getAttributes()->toMatchArray([
TraceAttributes::MESSAGING_SYSTEM => 'redis',
TraceAttributes::MESSAGING_OPERATION => 'enqueue',
TraceAttributes::MESSAGE_ID => $this->valuestore->get('uuid'),
TraceAttributes::MESSAGING_OPERATION_TYPE => 'enqueue',
TraceAttributes::RPC_MESSAGE_ID => $this->valuestore->get('uuid'),
TraceAttributes::MESSAGING_DESTINATION_NAME => 'default',
TraceAttributes::MESSAGING_DESTINATION_TEMPLATE => TestJob::class,
]);
Expand All @@ -158,8 +158,8 @@
->getEvents()->{0}->getName()->toBe('exception')
->getAttributes()->toMatchArray([
TraceAttributes::MESSAGING_SYSTEM => 'redis',
TraceAttributes::MESSAGING_OPERATION => 'process',
TraceAttributes::MESSAGE_ID => $this->valuestore->get('uuid'),
TraceAttributes::MESSAGING_OPERATION_TYPE => 'process',
TraceAttributes::RPC_MESSAGE_ID => $this->valuestore->get('uuid'),
TraceAttributes::MESSAGING_DESTINATION_NAME => 'default',
TraceAttributes::MESSAGING_DESTINATION_TEMPLATE => TestJob::class,
]);
Expand Down
6 changes: 3 additions & 3 deletions tests/Instrumentation/RedisInstrumentationTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

use OpenTelemetry\API\Common\Time\Clock;
use OpenTelemetry\API\Trace\SpanKind;
use OpenTelemetry\SDK\Common\Time\ClockFactory;
use OpenTelemetry\SDK\Trace\ImmutableSpan;

it('can watch a redis call', function (string $client) {
Expand All @@ -17,11 +17,11 @@
->getKind()->toBe(SpanKind::KIND_CLIENT)
->getAttributes()->toArray()->toBe([
'db.system' => 'redis',
'db.statement' => 'get test',
'db.query.text' => 'get test',
'server.address' => '127.0.0.1',
])
->hasEnded()->toBeTrue()
->getEndEpochNanos()->toBeLessThan(ClockFactory::getDefault()->now());
->getEndEpochNanos()->toBeLessThan(Clock::getDefault()->now());

expect($span->getEndEpochNanos() - $span->getStartEpochNanos())
->toBeGreaterThan(0);
Expand Down
12 changes: 6 additions & 6 deletions tests/Sdk/LoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

use Illuminate\Support\Facades\Log;
use Keepsuit\LaravelOpenTelemetry\Facades\Logger;
use OpenTelemetry\API\Logs\Map\Psr3;
use OpenTelemetry\SDK\Common\Time\ClockFactory;
use OpenTelemetry\API\Common\Time\Clock;
use OpenTelemetry\API\Logs\Severity;
use OpenTelemetry\SDK\Logs\ReadableLogRecord;
use Psr\Log\LogLevel;
use Spatie\TestTime\TestTime;
Expand All @@ -20,9 +20,9 @@
expect($logs->first())
->toBeInstanceOf(ReadableLogRecord::class)
->getBody()->toBe('test')
->getSeverityNumber()->toBe(Psr3::severityNumber($level))
->getSeverityNumber()->toBe(Severity::fromPsr3($level)->value)
->getSeverityText()->toBe($level)
->getTimestamp()->toBe(ClockFactory::getDefault()->now())
->getTimestamp()->toBe(Clock::getDefault()->now())
->getAttributes()->toArray()->toBe(['foo' => 'bar']);
})->with([
LogLevel::EMERGENCY,
Expand All @@ -47,9 +47,9 @@
expect($logs->first())
->toBeInstanceOf(ReadableLogRecord::class)
->getBody()->toBe('test')
->getSeverityNumber()->toBe(Psr3::severityNumber($level))
->getSeverityNumber()->toBe(Severity::fromPsr3($level)->value)
->getSeverityText()->toBe($level)
->getTimestamp()->toBe(ClockFactory::getDefault()->now())
->getTimestamp()->toBe(Clock::getDefault()->now())
->getAttributes()->toArray()->toBe(['foo' => 'bar']);
})->with([
LogLevel::EMERGENCY,
Expand Down

0 comments on commit c1a48d1

Please sign in to comment.