User Tools

Site Tools


tracing

Table of Contents

Jaeger UI

PHP

install dependencies with composer:

  • composer require opentracing/opentracing
  • composer require jonahgeorge/jaeger-client-php

the following php extensions have to be installed/enabled:

  • bcmath
  • sockets (for UDP communication)

Code Snippet

<?php

require_once __DIR__ . '/vendor/autoload.php';

use Jaeger\Config;
use OpenTracing\GlobalTracer;

function get_trace_id($span) {

    $span_context = $span->getSpan()->getContext();
    $trace_id = ''
        .dechex($span_context->getTraceId()).':'
        .dechex($span_context->getSpanId()).':'
        .dechex($span_context->getParentId()).':'
        .dechex($span_context->getFlags());
    return $trace_id;
}

$config = new Config(
    [
    'sampler' => [
        'type' => Jaeger\SAMPLER_TYPE_CONST,
        'param' => true,
    ],
    'logging' => true,
    "tags" => [
    ],
    "local_agent" => [
    "reporting_host" => "sg03.picalike.corpex-kunden.de",
//        You can override port by setting local_agent.reporting_port value
        "reporting_port" => 22575
    ],
//     Different ways to send data to Jaeger. Config::ZIPKIN_OVER_COMPACT - default):
    'dispatch_mode' => Config::ZIPKIN_OVER_COMPACT_UDP,    
    ],
    'OSA'
);
$config->initializeTracer();

// extract the span context
$tracer = GlobalTracer::get();

$scope = $tracer->startActiveSpan('JaegerSpan', []);  # $tracer comes from tracer.php
$scope->getSpan()->setTag("tag1", "value1");
$scope->getSpan()->setTag("tag2", "value2");
$scope->getSpan()->setTag("tag3", "value2");
$scope->getSpan()->log([
    "key1" => "value1",
    "key2" => 2,
    "key3" => true
]);

$scope->getSpan()->addBaggageItem("baggage-item1", "baggage-value1");
$scope->getSpan()->addBaggageItem("baggage-item2", "baggage-value2");
$scope->getSpan()->addBaggageItem("baggage-item3", "baggage-value3");

    $nestedSpanScope = $tracer->startActiveSpan("Nested1");
    $nestedSpanScope->getSpan()->setTag("tag1", "value1");
    $nestedSpanScope->getSpan()->setTag("tag2", "value2");
    $nestedSpanScope->getSpan()->setTag("tag3", "value2");
    $nestedSpanScope->getSpan()->log([
        "key1" => "value1",
        "key2" => 2,
        "key3" => true
    ]);

    $nestedSpanScope->getSpan()->addBaggageItem("baggage-item1", "baggage-value1");
    $nestedSpanScope->getSpan()->addBaggageItem("baggage-item2", "baggage-value2");
    $nestedSpanScope->getSpan()->addBaggageItem("baggage-item3", "baggage-value3");

    sleep(1);

    $nestedSpanScope->close();

sleep(1);
$scope->close();
$tracer->flush();

?>
tracing.txt · Last modified: 2024/04/11 14:23 by 127.0.0.1