-
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
Feat: Honeybadger adaptor #19
Open
2002Bishwajeet
wants to merge
13
commits into
utopia-php:main
Choose a base branch
from
2002Bishwajeet:feat-honeybadger-adaptor
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.
+189
−3
Open
Changes from 4 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
97ac74b
update raw links
2002Bishwajeet f2a7044
minor typo fixes
2002Bishwajeet f35dc53
feat honeyBadger adaptor
2002Bishwajeet 50f4ac7
minor changes
2002Bishwajeet 9323930
Merge branch 'main' into feat-honeybadger-adaptor
2002Bishwajeet d6a9846
wip: change request body
2002Bishwajeet 6f43aba
fix: now able to see in the UI
2002Bishwajeet 0a371b4
fix lint
2002Bishwajeet 3346692
feat: stacktrace
2002Bishwajeet 980d9d9
Merge branch 'main' into feat-honeybadger-adaptor
2002Bishwajeet 3e8f9d1
fix lints
2002Bishwajeet 081ebed
applied suggesstions
2002Bishwajeet 8f8fb48
fix lint errors
2002Bishwajeet 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,161 @@ | ||
<?php | ||
|
||
namespace Utopia\Logger\Adapter; | ||
|
||
use Exception; | ||
use Utopia\Logger\Adapter; | ||
use Utopia\Logger\Log; | ||
use Utopia\Logger\Logger; | ||
|
||
// Reference Material | ||
// https://docs.honeybadger.io/api/reporting-exceptions | ||
|
||
class HoneyBadger extends Adapter | ||
{ | ||
protected string $apiKey; | ||
|
||
/** | ||
* Return unique adapter name | ||
* | ||
* @return string | ||
*/ | ||
public static function getName(): string | ||
{ | ||
return "honeyBadger"; | ||
} | ||
|
||
/** | ||
* Push log to external provider | ||
* | ||
* @param Log $log | ||
* @return int | ||
*/ | ||
public function push(Log $log): int | ||
{ | ||
$params = []; | ||
|
||
foreach ($log->getExtra() as $paramKey => $paramValue) { | ||
$params[$paramKey] = var_export($paramValue, true); | ||
} | ||
|
||
$breadcrumbsObject = $log->getBreadcrumbs(); | ||
$breadcrumbsArray = []; | ||
|
||
foreach ($breadcrumbsObject as $breadcrumb) { | ||
\array_push($breadcrumbsArray, [ | ||
'timestamp' => \intval($breadcrumb->getTimestamp()), | ||
'category' => $breadcrumb->getCategory(), | ||
'action' => $breadcrumb->getMessage(), | ||
'metadata' => [ | ||
'type' => $breadcrumb->getType() | ||
] | ||
]); | ||
} | ||
|
||
$tags = array(); | ||
|
||
foreach ($log->getTags() as $tagKey => $tagValue) { | ||
$tags[$tagKey] = $tagValue; | ||
} | ||
|
||
if (!empty($log->getType())) { | ||
$tags['type'] = $log->getType(); | ||
} | ||
if (!empty($log->getUser()) && !empty($log->getUser()->getId())) { | ||
$tags['userId'] = $log->getUser()->getId(); | ||
} | ||
if (!empty($log->getUser()) && !empty($log->getUser()->getUsername())) { | ||
$tags['userName'] = $log->getUser()->getUsername(); | ||
} | ||
if (!empty($log->getUser()) && !empty($log->getUser()->getEmail())) { | ||
$tags['userEmail'] = $log->getUser()->getEmail(); | ||
} | ||
|
||
$requestBody = [ | ||
'timestamp' => \intval($log->getTimestamp()), | ||
'namespace' => $log->getNamespace(), | ||
'error' => [ | ||
'name' => $log->getMessage(), | ||
'message' => $log->getMessage(), | ||
'backtrace' => [] | ||
stnguyen90 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
], | ||
'environment' => [ | ||
'environment' => $log->getEnvironment(), | ||
'server' => $log->getServer(), | ||
'version' => $log->getVersion(), | ||
], | ||
'revision' => $log->getVersion(), | ||
'action' => $log->getAction(), | ||
'params' => $params, | ||
'tags' => $tags, | ||
'breadcrumbs' => $breadcrumbsArray | ||
]; | ||
|
||
// init curl object | ||
$ch = \curl_init(); | ||
|
||
// define options | ||
$optArray = array( | ||
CURLOPT_URL => 'https://api.honeybadger.io/v1/notices', | ||
CURLOPT_RETURNTRANSFER => true, | ||
CURLOPT_POST => true, | ||
CURLOPT_POSTFIELDS => \json_encode($requestBody), | ||
CURLOPT_HEADEROPT => \CURLHEADER_UNIFIED, | ||
CURLOPT_HTTPHEADER => array('Content-Type: application/json', 'X-API-Key: ' . $this->apiKey, 'Accept: application/json', 'User-Agent: utopia-logger/' . Logger::LIBRARY_VERSION) | ||
); | ||
|
||
// apply those options | ||
\curl_setopt_array($ch, $optArray); | ||
|
||
// execute request and get response | ||
$result = \curl_exec($ch); | ||
$response = \curl_getinfo($ch, \CURLINFO_HTTP_CODE); | ||
|
||
if (!$result && $response >= 400) { | ||
throw new Exception("Log could not be pushed with status code " . $response . ": " . \curl_error($ch)); | ||
} | ||
|
||
\curl_close($ch); | ||
|
||
return $response; | ||
} | ||
/** | ||
* HoneyBadger constructor. | ||
* | ||
* @param string $configKey | ||
*/ | ||
public function __construct(string $configKey) | ||
{ | ||
$this->apiKey = $configKey; | ||
} | ||
|
||
public function getSupportedTypes(): array | ||
{ | ||
return [ | ||
Log::TYPE_INFO, | ||
Log::TYPE_DEBUG, | ||
Log::TYPE_VERBOSE, | ||
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_VERBOSE, | ||
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", | ||
"honeyBadger" | ||
]; | ||
|
||
/** | ||
|
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.
I tested this recently, but got an error in Honeybadger because they expect this to be a string like
2020-01-27T12:37:31.616-08:00
. Would you please update this to be a ISO-8601 formatted timestamp in UTC?