From e72a778661612315542b8d4127613a236e984226 Mon Sep 17 00:00:00 2001 From: Eric Khun Date: Wed, 5 Nov 2025 16:23:59 +0800 Subject: [PATCH] upgrade dd-trace --- composer.json | 4 ++-- src/BuffLog/BuffLog.php | 22 ++++++++-------------- 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/composer.json b/composer.json index ebd514d..ea0ae32 100644 --- a/composer.json +++ b/composer.json @@ -1,14 +1,14 @@ { "name": "bufferapp/php-bufflog", "description": "PHP log libraries for Buffer services", - "version": "0.3.3", + "version": "0.4.0", "require": { "php": ">=8.1", "monolog/monolog": "^3.2", "psr/log": "^2.0 || ^3.0" }, "require-dev": { - "datadog/dd-trace": "0.82.*" + "datadog/dd-trace": "^1.13.0" }, "autoload": { "psr-4": { diff --git a/src/BuffLog/BuffLog.php b/src/BuffLog/BuffLog.php index 08a2ea2..029197d 100644 --- a/src/BuffLog/BuffLog.php +++ b/src/BuffLog/BuffLog.php @@ -16,9 +16,6 @@ class BuffLog { // verbosity can be changed with setting this env var public static $logLevelEnvVar = "LOG_LEVEL"; - // Global Tracer comes with the datadog tracing extension - private static $hasGlobalTracer = false; - // we can use strtolower(Logger::getLevels()) instead private static $logOutputMethods = ['debug', 'info', 'notice', 'warning', 'error', 'critical']; @@ -41,13 +38,12 @@ static public function getLogger() protected static function configureInstance() { - if (class_exists("\DDTrace\GlobalTracer")) { - self::$hasGlobalTracer = true; - } else { + // Check if DDTrace functions are available and warn if not + if (!function_exists("\DDTrace\logs_correlation_trace_id")) { // local envs don't need tracing if (getenv("ENVIRONMENT") !== "local") { echo json_encode([ - "message" => "Can't find \DDTrace\GlobalTracer class. Did you install the Datadog APM tracer extension? It will allow you to have logs enriched with traces making troubleshooting easier. If you run a cli mode service (such as a worker), did you set the DD_TRACE_CLI_ENABLED env variable?", + "message" => "Can't find DDTrace functions. Did you install the Datadog APM tracer extension? It will allow you to have logs enriched with traces making troubleshooting easier. If you run a cli mode service (such as a worker), did you set the DD_TRACE_CLI_ENABLED env variable?", "level" => 300, "level_name" => "WARNING", "context" => ["bufflog_error" => "no_tracer"] @@ -87,15 +83,13 @@ protected static function configureInstance() // 'profileID' => $user->getProfileID() // ); - if (self::$hasGlobalTracer) { + // Add traces information to correlate logs with APM + if (function_exists('\DDTrace\logs_correlation_trace_id') && function_exists('\dd_trace_peek_span_id')) { try { - // Add traces information to be able to correlate logs with APM - $ddTraceSpan = \DDTrace\GlobalTracer::get()->getActiveSpan(); - $record['context']['dd'] = [ - "trace_id" => $ddTraceSpan->getTraceId(), - "span_id" => $ddTraceSpan->getSpanId() + $record->extra['dd'] = [ + 'trace_id' => \DDTrace\logs_correlation_trace_id(), + 'span_id' => \dd_trace_peek_span_id() ]; - } catch (\Exception $e) { // no-op }