Skip to content
This repository was archived by the owner on Dec 5, 2025. It is now read-only.

Commit f3be78d

Browse files
committed
feat: test command
1 parent c18bd50 commit f3be78d

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,15 @@ $scope = $span->activate();
106106
$span->end();
107107
$scope->detach();
108108
```
109+
### test events
110+
111+
You can test the events by using the command:
112+
113+
```bash
114+
php artisan otle:test
115+
```
116+
117+
it will create a span with the name `test-event` and the attributes `key1` and `key2`.
109118

110119
## Contributing
111120

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace Overtrue\LaravelOpenTelemetry\Console\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use OpenTelemetry\API\Trace\StatusCode;
7+
use Overtrue\LaravelOpenTelemetry\Facades\Measure;
8+
9+
class TestCommand extends Command
10+
{
11+
/**
12+
* Command name and signature
13+
*/
14+
protected $signature = 'otel:test';
15+
16+
/**
17+
* Command description
18+
*/
19+
protected $description = 'Create a test span and output the result';
20+
21+
/**
22+
* Execute the command
23+
*/
24+
public function handle(): int
25+
{
26+
$this->info('Creating test span...');
27+
28+
// Create root span
29+
$rootSpan = Measure::start('Test Span');
30+
$rootSpan->span->setAttribute('test.attribute', 'test_value');
31+
$rootSpan->span->setAttribute('timestamp', time());
32+
33+
// Simulate delay
34+
sleep(1);
35+
36+
// Add child span
37+
$childSpan = Measure::start('Child Operation');
38+
$childSpan->span->setAttribute('child.attribute', 'child_value');
39+
40+
sleep(1);
41+
42+
// End child span
43+
Measure::end('Child Operation');
44+
45+
// Record event
46+
$rootSpan->span->addEvent('Test Event', [
47+
'detail' => 'This is a test event',
48+
]);
49+
50+
// Set status
51+
$rootSpan->span->setStatus(StatusCode::STATUS_OK);
52+
53+
// Get trace ID before ending the root span
54+
$traceId = $rootSpan->span->getContext()->getTraceId();
55+
56+
// End root span
57+
Measure::end('Test Span');
58+
59+
// Output result
60+
$this->info('Test completed!');
61+
$this->info('Trace ID: ' . $traceId);
62+
63+
// Display information table
64+
$this->table(
65+
['Span Name', 'Status', 'Attributes'],
66+
[
67+
['Test Span', 'OK', 'test.attribute=test_value, timestamp=' . $rootSpan->span->getAttribute('timestamp')],
68+
['Child Operation', 'OK', 'child.attribute=child_value'],
69+
]
70+
);
71+
72+
return Command::SUCCESS;
73+
}
74+
}

src/OpenTelemetryServiceProvider.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ public function boot(): void
4040
$this->app->make($watcher)->register($this->app);
4141
Log::debug(sprintf('[laravel-open-telemetry] watcher `%s` registered', $watcher));
4242
}
43+
44+
$this->registerCommands();
4345
}
4446

4547
public function register(): void
@@ -72,4 +74,13 @@ protected function injectHttpMiddleware(Kernel $kernel): void
7274
Log::debug(sprintf('[laravel-open-telemetry] %s middleware injected', MeasureRequest::class));
7375
}
7476
}
77+
78+
protected function registerCommands()
79+
{
80+
if ($this->app->runningInConsole()) {
81+
$this->commands([
82+
\Overtrue\LaravelOpenTelemetry\Console\Commands\TestCommand::class,
83+
]);
84+
}
85+
}
7586
}

0 commit comments

Comments
 (0)