forked from open-telemetry/opentelemetry-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AlwaysOnJaegerExample.php
58 lines (45 loc) · 1.7 KB
/
AlwaysOnJaegerExample.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
declare(strict_types=1);
require __DIR__ . '/../vendor/autoload.php';
use OpenTelemetry\Contrib\Jaeger\Exporter as JaegerExporter;
use OpenTelemetry\SDK\Common\Attribute\Attributes;
use OpenTelemetry\SDK\Trace\Sampler\AlwaysOnSampler;
use OpenTelemetry\SDK\Trace\SpanProcessor\SimpleSpanProcessor;
use OpenTelemetry\SDK\Trace\TracerProvider;
$exporter = JaegerExporter::fromConnectionString('http://jaeger:9412/api/v2/spans', 'AlwaysOnJaegerExample');
$tracerProvider = new TracerProvider(
new SimpleSpanProcessor($exporter),
new AlwaysOnSampler(),
);
echo 'Starting AlwaysOnJaegerExample';
$tracer = $tracerProvider->getTracer();
$rootSpan = $tracer->spanBuilder('root')->startSpan();
$rootSpan->activate();
for ($i = 0; $i < 5; $i++) {
// start a span, register some events
$span = $tracer->spanBuilder('session.generate.span-' . $i)->startSpan();
echo sprintf(
PHP_EOL . 'Exporting Trace: %s, Parent: %s, Span: %s',
$span->getContext()->getTraceId(),
$span->getParentContext()->getSpanId() ?: 'None',
$span->getContext()->getSpanId()
);
$span->setAttribute('remote_ip', '1.2.3.4')
->setAttribute('country', 'USA');
$span->addEvent('found_login' . $i, new Attributes([
'id' => $i,
'username' => 'otuser' . $i,
]));
$span->addEvent('generated_session', new Attributes([
'id' => md5((string) microtime(true)),
]));
try {
throw new Exception('Record exception test event');
} catch (Exception $exception) {
$span->recordException($exception);
}
$span->end();
}
$rootSpan->end();
echo PHP_EOL . 'AlwaysOnJaegerExample complete! See the results at http://localhost:16686/';
echo PHP_EOL;