-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Trace Context for GoogleCloudLoggingFormatter #1877
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -24,6 +24,10 @@ | |||||
*/ | ||||||
final class GoogleCloudLoggingFormatter extends JsonFormatter | ||||||
{ | ||||||
const CONTEXT_HEADER_FORMAT = '/([0-9a-fA-F]{32})(?:\/(\d+))?(?:;o=(\d+))?/'; | ||||||
|
||||||
private static ?string $traceID = null; | ||||||
|
||||||
protected function normalizeRecord(LogRecord $record): array | ||||||
{ | ||||||
$normalized = parent::normalizeRecord($record); | ||||||
|
@@ -32,9 +36,49 @@ | |||||
$normalized['severity'] = $normalized['level_name']; | ||||||
$normalized['time'] = $record->datetime->format(DateTimeInterface::RFC3339_EXTENDED); | ||||||
|
||||||
// Tag with Trace ID for request attribution | ||||||
$normalized['logging.googleapis.com/trace'] = $this->getTraceID(); | ||||||
|
||||||
// Remove keys that are not used by GCP | ||||||
unset($normalized['level'], $normalized['level_name'], $normalized['datetime']); | ||||||
|
||||||
return $normalized; | ||||||
} | ||||||
|
||||||
private function getTraceID(): ?string | ||||||
{ | ||||||
if (empty($this->traceID) && !empty($_SERVER['HTTP_X_CLOUD_TRACE_CONTEXT'])) { | ||||||
Check failure on line 50 in src/Monolog/Formatter/GoogleCloudLoggingFormatter.php GitHub Actions / PHPStan (8.1)
|
||||||
$matched = preg_match( | ||||||
self::CONTEXT_HEADER_FORMAT, | ||||||
$_SERVER['HTTP_X_CLOUD_TRACE_CONTEXT'] ?? '', | ||||||
$matches, | ||||||
); | ||||||
|
||||||
if (!$matched) { | ||||||
return null; | ||||||
} | ||||||
|
||||||
$projectID = $this->getProjectID(); | ||||||
if (empty($projectID)) { | ||||||
return null; | ||||||
} | ||||||
|
||||||
$this->traceID = 'projects/'.$projectID.'/traces/'.strtolower($matches[1]); | ||||||
} | ||||||
|
||||||
return $this->traceID; | ||||||
} | ||||||
|
||||||
private function getProjectID(): ?string | ||||||
{ | ||||||
if (isset($_SERVER['GOOGLE_CLOUD_PROJECT'])) { | ||||||
return $_SERVER['GOOGLE_CLOUD_PROJECT']; | ||||||
} | ||||||
|
||||||
if (class_exists('\Google\Cloud\Core\Compute\Metadata')) { | ||||||
Seldaek marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
return (new \Google\Cloud\Core\Compute\Metadata())->getProjectId(); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also, it looks like the implementation of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You'll hopefully see further up, we set the static property The project ID is only retrieved to construct that value the single time it is constructed, so hopefully this is already accounted for. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Caching I am not familiar enough with monolog + octane to confidently say whether Octane reuses the formatter object across different requests, but to be safe I would not cache the trace id but only the project id. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah it's probably safer not to cache, as retrieving the trace id doesn't seem that expensive. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And I would definitely not cache this as a static property. Cache on an instance property then a new instance would at least refetch from a clean state. Static property will definitely cause issues in some envs. |
||||||
} | ||||||
|
||||||
return null; | ||||||
} | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this really be set at all if getTraceID returns null?