Skip to content

Commit 998dfc7

Browse files
authored
Merge pull request #16 from bufferapp/only-one-pushprocessor
We only need 1 processor
2 parents 115a8ab + 3963964 commit 998dfc7

File tree

1 file changed

+39
-45
lines changed

1 file changed

+39
-45
lines changed

src/BuffLog/BuffLog.php

Lines changed: 39 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ class BuffLog {
1515
// verbosity can be changed with setting this env var
1616
public static $logLevelEnvVar = "LOG_LEVEL";
1717

18+
// Global Tracer comes with the datadog tracing extension
19+
private static $hasGlobalTracer = false;
20+
1821
// we can use strtolower(Logger::getLevels()) instead
1922
private static $logOutputMethods = ['debug', 'info', 'notice', 'warning', 'error', 'critical'];
2023

@@ -37,8 +40,10 @@ static public function getLogger()
3740
protected static function configureInstance()
3841
{
3942

40-
if (!class_exists("\DDTrace\GlobalTracer")) {
41-
error_log("Tip #1: 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! :)");
43+
if (class_exists("\DDTrace\GlobalTracer")) {
44+
self::$hasGlobalTracer = true;
45+
} else {
46+
error_log("Tip #1: 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.");
4247
error_log("Tip #2: If you run a cli mode service (such as a worker), did you set the DD_TRACE_CLI_ENABLED env variable?");
4348
}
4449

@@ -61,6 +66,36 @@ protected static function configureInstance()
6166
$handler = new StreamHandler('php://stdout', self::$verbosityLevel);
6267
$handler->setFormatter( new \Monolog\Formatter\JsonFormatter() );
6368
$logger->pushHandler($handler);
69+
70+
// We should probably implement this as a Monolog Processor
71+
// https://github.com/Seldaek/monolog/tree/master/src/Monolog/Processor
72+
$logger->pushProcessor(function ($record) {
73+
// We should grab any Buffer information useful when available
74+
// Need to check with the Core team: accountID / userID / profileID
75+
// $user = Buffer/Core::getCurrentUser();
76+
// That should look like:
77+
// $record['context']['user'] = array(
78+
// 'accountID' => $user->getAccountID(),
79+
// 'userID' => $user->getUserID(),
80+
// 'profileID' => $user->getProfileID()
81+
// );
82+
83+
if (self::$hasGlobalTracer) {
84+
try {
85+
// Add traces information to be able to correlate logs with APM
86+
$ddTraceSpan = \DDTrace\GlobalTracer::get()->getActiveSpan();
87+
$record['context']['dd'] = [
88+
"trace_id" => $ddTraceSpan->getTraceId(),
89+
"span_id" => $ddTraceSpan->getSpanId()
90+
];
91+
92+
} catch (\Exception $e) {
93+
// no-op
94+
}
95+
}
96+
return $record;
97+
});
98+
6499
self::$instance = $logger;
65100
}
66101

@@ -72,12 +107,9 @@ public static function __callStatic($methodName, $args)
72107
if (in_array($methodName, array_merge(self::$logOutputMethods, self::$extraAllowedMethods))) {
73108

74109
if (in_array($methodName, self::$logOutputMethods)) {
75-
76-
self::enrichLog();
110+
// Where the magic happen. We "proxy" functions name with arguments to the Monolog instance
111+
return call_user_func_array(array(self::getLogger(), $methodName), $args);
77112
}
78-
// Where the magic happen. We "proxy" functions name with arguments to the Monolog instance
79-
return call_user_func_array(array(self::getLogger(), $methodName), $args);
80-
81113
} else {
82114
error_log("BuffLog::$methodName() is not supported yet. Add it to the BuffLog whitelist to allow it");
83115
}
@@ -86,42 +118,4 @@ public static function __callStatic($methodName, $args)
86118
}
87119
}
88120

89-
private static function enrichLog()
90-
{
91-
// We should probably implement this as a Monolog Processor
92-
// https://github.com/Seldaek/monolog/tree/master/src/Monolog/Processor
93-
self::getLogger()->pushProcessor(function ($record) {
94-
95-
// We should grab any Buffer information useful when available
96-
// Need to check with the Core team: accountID / userID / profileID
97-
// $user = Buffer/Core::getCurrentUser();
98-
// That should look like:
99-
// $record['context']['user'] = array(
100-
// 'accountID' => $user->getAccountID(),
101-
// 'userID' => $user->getUserID(),
102-
// 'profileID' => $user->getProfileID()
103-
// );
104-
105-
try {
106-
107-
if (class_exists("\DDTrace\GlobalTracer", false) === false) {
108-
throw new \Exception('DDTrace\GlobalTracer can\'t be found. Have you setup the Datadog Tracer extension? If you run cli worker, have you added the DD_TRACE_CLI_ENABLED env variable?');
109-
}
110-
111-
// Add traces information to be able to correlate logs with APM
112-
$ddTraceSpan = \DDTrace\GlobalTracer::get()->getActiveSpan();
113-
$record['context']['dd'] = [
114-
"trace_id" => $ddTraceSpan->getTraceId(),
115-
"span_id" => $ddTraceSpan->getSpanId()
116-
];
117-
118-
} catch (\Exception $e) {
119-
// we probably will want to make an no-op or it will be too verbose
120-
error_log($e->getMessage() . " Traces will not be added in the logs");
121-
}
122-
123-
return $record;
124-
});
125-
}
126-
127121
}

0 commit comments

Comments
 (0)