laravel - How to connect my custom trace context to an observability platform? - Stack Overflow

admin2025-04-16  2

I am developing a microservices system in PHP and have implemented a TraceContext class to manage trace information (trace_id, span_id, parent_id). My goal is to send this information to an observability platform every time a log is written.

My questions are:

  1. How can I structure the integration to send the traces generated in my class to an observability platform compatible with standards?

  2. What protocols or export formats should I consider to ensure that the data is compatible with standard monitoring tools?

I am not looking for specific tool recommendations but rather an understanding of the approaches available to connect a custom tracing system to an observability platform.

Here is my TraceContext class:

<?php

namespace Kaira\Shared\Log;

use Random\RandomException;

/**
 * Class TraceContext
 * @package Kaira\Shared\Log
 */
final class TraceContext
{
    private static ?string $traceId = null;
    private static ?string $spanId = null;
    private static ?string $parentId = null;

    /**
     * @param string $traceId
     * @return void
     */
    public static function setTraceId(string $traceId): void
    {
        self::$traceId = $traceId;
    }

    /**
     * @return string|null
     */
    public static function getTraceId(): ?string
    {
        return self::$traceId;
    }

    /**
     * @param string $spanId
     * @return void
     */
    public static function setSpanId(string $spanId): void
    {
        self::$spanId = $spanId;
    }

    /**
     * @return string|null
     */
    public static function getSpanId(): ?string
    {
        return self::$spanId;
    }

    /**
     * @param string $parentId
     * @return void
     */
    public static function setParentId(string $parentId): void
    {
        self::$parentId = $parentId;
    }

    /**
     * @return string|null
     */
    public static function getParentId(): ?string
    {
        return self::$parentId;
    }

    /**
     * @return void
     */
    public static function clear(): void
    {
        self::$traceId = null;
        self::$spanId = null;
        self::$parentId = null;
    }

    /**
     * @return void
     * @throws RandomException
     */
    public static function generateTraceId(): void
    {
        self::setTraceId(bin2hex(random_bytes(16)));
    }

    /**
     * @return void
     * @throws RandomException
     */
    public static function generateSpanId(): void
    {
        self::setSpanId(bin2hex(random_bytes(8)));
    }

    /**
     * @return array
     * @throws RandomException
     */
    public static function getTraceParent(): array
    {
        if(self::getTraceId() === null){
            // 16-byte
            self::setTraceId(bin2hex(random_bytes(16)));
        }
        if(self::getSpanId() === null){
            // 8-byte
            self::setSpanId(bin2hex(random_bytes(8)));
        }
        return [
            'trace_id' => self::$traceId,
            'span_id' => self::$spanId,
            'parent_id' => self::$parentId
        ];
    }
}
转载请注明原文地址:http://www.anycun.com/QandA/1744765540a87309.html