Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
22 changes: 8 additions & 14 deletions src/BuffLog/BuffLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];

Expand All @@ -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"]
Expand Down Expand Up @@ -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
}
Expand Down