forked from open-telemetry/opentelemetry-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlwaysOnZipkinExample.php
72 lines (60 loc) · 2.34 KB
/
AlwaysOnZipkinExample.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
<?php
declare(strict_types=1);
require __DIR__ . '/../vendor/autoload.php';
use OpenTelemetry\Contrib\Zipkin\Exporter as ZipkinExporter;
use OpenTelemetry\Sdk\Trace\Attributes;
use OpenTelemetry\Sdk\Trace\Clock;
use OpenTelemetry\Sdk\Trace\Sampler\AlwaysOnSampler;
use OpenTelemetry\Sdk\Trace\SamplingResult;
use OpenTelemetry\Sdk\Trace\SpanProcessor\SimpleSpanProcessor;
use OpenTelemetry\Sdk\Trace\TracerProvider;
use OpenTelemetry\Trace as API;
$sampler = new AlwaysOnSampler();
$samplingResult = $sampler->shouldSample(
null,
md5((string) microtime(true)),
substr(md5((string) microtime(true)), 16),
'io.opentelemetry.example',
API\SpanKind::KIND_INTERNAL
);
$zipkinExporter = new ZipkinExporter(
'alwaysOnZipkinExample',
'http://zipkin:9411/api/v2/spans'
);
if (SamplingResult::RECORD_AND_SAMPLED === $samplingResult->getDecision()) {
echo 'Starting AlwaysOnZipkinExample';
$tracer = (new TracerProvider())
->addSpanProcessor(new SimpleSpanProcessor($zipkinExporter))
->getTracer('io.opentelemetry.contrib.php');
for ($i = 0; $i < 5; $i++) {
// start a span, register some events
$timestamp = Clock::get()->timestamp();
$span = $tracer->startAndActivateSpan('session.generate.span.' . microtime(true));
$spanParent = $span->getParent();
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, $timestamp, new Attributes([
'id' => $i,
'username' => 'otuser' . $i,
]));
$span->addEvent('generated_session', $timestamp, new Attributes([
'id' => md5((string) microtime(true)),
]));
try {
throw new Exception('Record exception test event');
} catch (Exception $exception) {
$span->recordException($exception);
}
$tracer->endActiveSpan();
}
echo PHP_EOL . 'AlwaysOnZipkinExample complete! See the results at http://localhost:9411/';
} else {
echo PHP_EOL . 'AlwaysOnZipkinExample tracing is not enabled';
}
echo PHP_EOL;