PHP code example of whitemerry / phpkin

1. Go to this page and download the library: Download whitemerry/phpkin library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

whitemerry / phpkin example snippets


$tracer = new Tracer($name, $endpoint, $logger, $isSampled, $traceId, $traceSpanId, $parentSpanId);

new SpanIdentifier('garbage');            // throws in 2.0, was a random identifier in 1.x
new SpanIdentifier('');                   // throws in 2.0 too - a header sent empty is a broken caller
new SpanIdentifier();                     // still generates one, and so does null

// Worked in 1.x by accident, throws in 2.0
$tracer->addSpan($myOwnSpanClass);

$metadata = new Metadata();

$metadata->set(Metadata::HTTP_PATH, '/first');
$tracer->addSpan(new Span($firstId, 'First', $firstAnnotations, $metadata));

$metadata->set(Metadata::HTTP_PATH, '/second');
$tracer->addSpan(new Span($secondId, 'Second', $secondAnnotations, $metadata));

$endpoint = new Endpoint(
    'My application', // Application name
    '127.0.0.1', // Current application IP address
    '80' // Current application port (default 80)
);

$logger = new SimpleHttpLogger([
    'host' => 'http://192.168.33.11:9411' // Zipkin's API host with schema (http://) and without trailing slash
]);

$tracer = new Tracer(
    'http://localhost/login', // Trace name
    $endpoint, // Your application meta-information
    $logger // Logger used to store/send traces
);

$traceId = null;
if (!empty($_SERVER['HTTP_X_B3_TRACEID']) && is_zipkin_trace_identifier($_SERVER['HTTP_X_B3_TRACEID'])) {
    $traceId = new TraceIdentifier($_SERVER['HTTP_X_B3_TRACEID']);
}

$traceSpanId = null;
if (!empty($_SERVER['HTTP_X_B3_SPANID']) && is_zipkin_span_identifier($_SERVER['HTTP_X_B3_SPANID'])) {
    $traceSpanId = new SpanIdentifier($_SERVER['HTTP_X_B3_SPANID']);
}

$parentSpanId = null;
if (!empty($_SERVER['HTTP_X_B3_PARENTSPANID']) && is_zipkin_span_identifier($_SERVER['HTTP_X_B3_PARENTSPANID'])) {
    $parentSpanId = new SpanIdentifier($_SERVER['HTTP_X_B3_PARENTSPANID']);
}

$isSampled = null;
if (isset($_SERVER['HTTP_X_B3_SAMPLED'])) {
    if (in_array($_SERVER['HTTP_X_B3_SAMPLED'], array('1', 'true'), true)) {
        $isSampled = true;
    } elseif (in_array($_SERVER['HTTP_X_B3_SAMPLED'], array('0', 'false'), true)) {
        $isSampled = false;
    }
}

$tracer = new Tracer(
    'http://localhost/login',
    $endpoint,
    $logger,
    $isSampled,
    $traceId,
    $traceSpanId,
    $parentSpanId
);

$tracer->trace();

// Before request - read current timestamp in zipkin format
$requestStartTimestamp = zipkin_timestamp();
$spanIdentifier = new SpanIdentifier();

/* 
...
Request logic
Remember, you need to add B3 headers to your request:
X-B3-TraceId = TracerInfo::getTraceId();
X-B3-SpanId = $spanIdentifier;              // The span you just created for this call
X-B3-ParentSpanId = TracerInfo::getTraceSpanId(); // Your own span, the parent of that call
X-B3-Sampled = (int) TracerInfo::isSampled();   // 1 or 0 - a bare false would send an empty header
*/

$endpoint = new Endpoint(
    'Accounts microservice', // Name of service you're connecting with
    '127.0.1.1', // This service Ip
    '8000' // And port
);

$annotationBlock = new AnnotationBlock(
    $endpoint,
    $requestStartTimestamp
);

$span = new Span(
    $spanIdentifier,
    'Authorize user',
    $annotationBlock
);

$tracer->addSpan($span);

foreach ($tracer->getSpans() as $span) {
    $data = $span->toArray();
}

$tracer = new Tracer(...); // Your tracer instance
TracerProxy::init($tracer);

TracerProxy::addSpan($span);
TracerProxy::getSpans();
TracerProxy::trace();

TracerInfo::getTraceId(); // TraceId - X-B3-TraceId
TracerInfo::getTraceSpanId(); // This request's own SpanId - X-B3-SpanId
TracerInfo::isSampled(); // Sampled - X-B3-Sampled