Skip to content

Commit 464290e

Browse files
authored
Merge pull request #13 from bufferapp/task/check-dd-extensions
Check datadog if class exists
2 parents 01dcc01 + 691bd73 commit 464290e

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "bufferapp/php-bufflog",
33
"description": "PHP log libraries for Buffer services",
4-
"version": "0.1.2",
4+
"version": "0.1.3",
55
"require": {
66
"php": "^7.1",
77
"monolog/monolog": "^1.20",

src/BuffLog/BuffLog.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ static public function getLogger()
3636

3737
protected static function configureInstance()
3838
{
39+
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! :)");
42+
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?");
43+
}
44+
3945
// @TODO: We could potentially use the Kubernetes downward API to
4046
// define the logger name. This will make it easier for developers
4147
// to read and friendlier to identify where come the logs at a glance
@@ -82,7 +88,7 @@ public static function __callStatic($methodName, $args)
8288

8389
private static function enrichLog()
8490
{
85-
// This should probably implemented as a Monolog Processor
91+
// We should probably implement this as a Monolog Processor
8692
// https://github.com/Seldaek/monolog/tree/master/src/Monolog/Processor
8793
self::getLogger()->pushProcessor(function ($record) {
8894

@@ -96,12 +102,18 @@ private static function enrichLog()
96102
// 'profileID' => $user->getProfileID()
97103
// );
98104

99-
// Add traces information to be able to correlate logs with APM
100-
$ddTraceSpan = \DDTrace\GlobalTracer::get()->getActiveSpan();
101-
$record['context']['dd'] = [
102-
"trace_id" => $ddTraceSpan->getTraceId(),
103-
"span_id" => $ddTraceSpan->getSpanId()
104-
];
105+
try {
106+
// Add traces information to be able to correlate logs with APM
107+
$ddTraceSpan = \DDTrace\GlobalTracer::get()->getActiveSpan();
108+
$record['context']['dd'] = [
109+
"trace_id" => $ddTraceSpan->getTraceId(),
110+
"span_id" => $ddTraceSpan->getSpanId()
111+
];
112+
113+
} catch (Exception $e) {
114+
error_log($e->getMessage() . " Can't add trace to logs. Have you setup the Datadog Tracer extension? If you run a worker have your added the DD_TRACE_CLI_ENABLED env variable?");
115+
}
116+
105117
return $record;
106118
});
107119
}

0 commit comments

Comments
 (0)