forked from open-telemetry/opentelemetry-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AlwaysOnJaegerExample.php
77 lines (65 loc) · 2.49 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
declare(strict_types=1);
require __DIR__ . '/../vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\HttpFactory;
use OpenTelemetry\API\Trace as API;
use OpenTelemetry\Context\Context;
use OpenTelemetry\Contrib\Jaeger\Exporter as JaegerExporter;
use OpenTelemetry\SDK\Trace\AbstractClock;
use OpenTelemetry\SDK\Trace\Attributes;
use OpenTelemetry\SDK\Trace\Sampler\AlwaysOnSampler;
use OpenTelemetry\SDK\Trace\SamplingResult;
use OpenTelemetry\SDK\Trace\SpanProcessor\BatchSpanProcessor;
use OpenTelemetry\SDK\Trace\TracerProvider;
$sampler = new AlwaysOnSampler();
$samplingResult = $sampler->shouldSample(
Context::getCurrent(),
md5((string) microtime(true)),
'io.opentelemetry.example',
API\SpanKind::KIND_INTERNAL
);
$exporter = new JaegerExporter(
'alwaysOnJaegerExample',
'http://jaeger:9412/api/v2/spans',
new Client(),
new HttpFactory(),
new HttpFactory()
);
if (SamplingResult::RECORD_AND_SAMPLE === $samplingResult->getDecision()) {
echo 'Starting AlwaysOnJaegerExample';
$tracer = (new TracerProvider())
->addSpanProcessor(new BatchSpanProcessor($exporter, AbstractClock::getDefault()))
->getTracer('io.opentelemetry.contrib.php');
for ($i = 0; $i < 5; $i++) {
// start a span, register some events
$timestamp = AbstractClock::getDefault()->timestamp();
$span = $tracer->startAndActivateSpan('session.generate.span' . microtime(true));
$spanParent = $span->getParentContext();
echo sprintf(
PHP_EOL . 'Exporting Trace: %s, Parent: %s, Span: %s',
$span->getContext()->getTraceId(),
$spanParent ? $spanParent->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,
]), $timestamp);
$span->addEvent('generated_session', new Attributes([
'id' => md5((string) microtime(true)),
]), $timestamp);
try {
throw new Exception('Record exception test event');
} catch (Exception $exception) {
$span->recordException($exception);
}
$span->end();
}
echo PHP_EOL . 'AlwaysOnJaegerExample complete! See the results at http://localhost:16686/';
} else {
echo PHP_EOL . 'AlwaysOnJaegerExample tracing is not enabled';
}
echo PHP_EOL;