-
Notifications
You must be signed in to change notification settings - Fork 17
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
Added Airbrake Adapter #18
Open
devblin
wants to merge
3
commits into
utopia-php:main
Choose a base branch
from
devblin:feat/airbrake-adapter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
<?php | ||
|
||
namespace Utopia\Logger\Adapter; | ||
|
||
use Exception; | ||
use Utopia\Logger\Adapter; | ||
use Utopia\Logger\Log; | ||
|
||
|
||
/** | ||
* Reference Docs: | ||
* https://docs.airbrake.io/docs/devops-tools/api/#create-notice-v3 | ||
* | ||
* https://docs.airbrake.io/docs/devops-tools/api/#post-data-fields-v3 | ||
*/ | ||
class Airbrake extends Adapter | ||
{ | ||
/** | ||
* Base URL for airbrake api endpoint | ||
*/ | ||
private string $airbrakeAPIHost; | ||
|
||
/** | ||
* API endpoint for pushing error data into airbrake (creating notice) | ||
*/ | ||
private string $airbrakeCreateNoticeURL; | ||
|
||
/** | ||
* Required to access airbrake API | ||
* | ||
* Ref: https://docs.airbrake.io/docs/devops-tools/api/ | ||
*/ | ||
protected string $projectKey; | ||
|
||
/** | ||
* Project unique ID | ||
* | ||
* Ref: https://docs.airbrake.io/docs/devops-tools/api/ | ||
*/ | ||
protected string $projectId; | ||
|
||
/** | ||
* Airbrake constructor | ||
* | ||
* @param string $configKey Contains projectId and projectKey | ||
*/ | ||
public function __construct(string $configKey) | ||
{ | ||
$this->airbrakeAPIHost = 'https://api.airbrake.io'; | ||
|
||
$configChunks = \explode(';', $configKey); | ||
$this->projectId = $configChunks[0]; | ||
$this->projectKey = $configChunks[1]; | ||
|
||
$this->airbrakeCreateNoticeURL = $this->airbrakeAPIHost . "/api/v3/projects/" . $this->projectId . "/notices?key=" . $this->projectKey; | ||
} | ||
|
||
/** | ||
* Unique adapter name | ||
* | ||
* @return string | ||
*/ | ||
public static function getName(): string | ||
{ | ||
return "airbrake"; | ||
} | ||
|
||
/** | ||
* Push log to airbrake | ||
* | ||
* @param Log $log | ||
* @return int Response status code | ||
* @throws Exception When status code is >= 400 | ||
*/ | ||
public function push(Log $log): int | ||
{ | ||
$breadcrumbObjects = $log->getBreadcrumbs(); | ||
$userLog = $log->getUser(); | ||
$user = [ | ||
"id" => $userLog->getId(), | ||
"name" => $userLog->getUsername(), | ||
"email" => $userLog->getEmail() | ||
]; | ||
$context = [ | ||
"hostname" => $log->getServer(), | ||
"environment" => $log->getEnvironment(), | ||
"severity" => $log->getType(), | ||
"version" => $log->getVersion(), | ||
"user" => $user, | ||
"action" => $log->getAction() | ||
]; | ||
$errors = []; | ||
|
||
foreach ($breadcrumbObjects as $breadcrumbObject) { | ||
$error = [ | ||
"type" => $breadcrumbObject->getType(), | ||
"message" => $breadcrumbObject->getMessage() | ||
]; | ||
|
||
\array_push($errors, $error); | ||
} | ||
|
||
$requestBody = [ | ||
"errors" => $errors, | ||
"context" => $context, | ||
'lastNoticeAt' => $log->getTimestamp() | ||
]; | ||
|
||
$curlHandler = \curl_init(); | ||
|
||
$options = array( | ||
CURLOPT_URL => $this->airbrakeCreateNoticeURL, | ||
CURLOPT_RETURNTRANSFER => true, | ||
CURLOPT_POST => true, | ||
CURLOPT_POSTFIELDS => \json_encode($requestBody), | ||
CURLOPT_HEADEROPT => \CURLHEADER_UNIFIED, | ||
CURLOPT_HTTPHEADER => array('Content-Type: application/json') | ||
); | ||
|
||
\curl_setopt_array($curlHandler, $options); | ||
|
||
$result = curl_exec($curlHandler); | ||
|
||
$response = curl_getinfo($curlHandler, \CURLINFO_HTTP_CODE); | ||
|
||
if ($result && $response >= 400) { | ||
throw new Exception("Log could not be pushed with status code " . $response . ": " . \curl_error($curlHandler)); | ||
} | ||
|
||
\curl_close($curlHandler); | ||
|
||
return $response; | ||
} | ||
|
||
public function getSupportedTypes(): array | ||
{ | ||
return [ | ||
Log::TYPE_DEBUG, | ||
Log::TYPE_INFO, | ||
Log::TYPE_WARNING, | ||
Log::TYPE_ERROR, | ||
]; | ||
} | ||
|
||
public function getSupportedEnvironments(): array | ||
{ | ||
return [ | ||
Log::ENVIRONMENT_STAGING, | ||
Log::ENVIRONMENT_PRODUCTION, | ||
]; | ||
} | ||
|
||
public function getSupportedBreadcrumbTypes(): array | ||
{ | ||
return [ | ||
Log::TYPE_INFO, | ||
Log::TYPE_DEBUG, | ||
Log::TYPE_WARNING, | ||
Log::TYPE_ERROR | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,8 @@ class Logger | |
"raygun", | ||
"sentry", | ||
"appSignal", | ||
"logOwl" | ||
"logOwl", | ||
"airbrake" | ||
]; | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Instead of adding the breadcrumbs to errors, I think it should come from
$log->getExtra()['detailedTrace']
. Seelogger/src/Logger/Adapter/Sentry.php
Lines 65 to 73 in e6a8ff4
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.
Hey @stnguyen90 , sorry for being inactive here and responding so late.
Looking at your comment and the airbrake docs, I think the
detailedTrace
should go inerror/{i}/backtrace
as per the docThere 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.
I am suggesting this change: