Skip to content
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
wants to merge 13 commits into
base: main
Choose a base branch
from
4 changes: 2 additions & 2 deletions docs/tutorials/add-logger-adapter.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Adding a new logger adapter 💾

This document is part of the Utopia contributors' guide. Before you continue reading this document make sure you have read the [Code of Conduct](https://raw.githubusercontent.com/utopia-php/logger/main/CODE_OF_CONDUCT.md) and the [Contributing Guide](https://raw.githubusercontent.com/utopia-php/logger/main/CONTRIBUTING.md).
This document is part of the Utopia contributors' guide. Before you continue reading this document make sure you have read the [Code of Conduct](../../CODE_OF_CONDUCT.md) and the [Contributing Guide](../../CONTRIBUTING.md).

## Getting started

Expand Down Expand Up @@ -139,7 +139,7 @@ In `src/Logger/Logger.php` update variable `const PROVIDERS` to include your pro

## 3. Test your adapter

After you finished adding your new adapter, you should write a proper test for it. To do that, you enter `tests/LoggerTests.php` and take a look at `testAdapters()` method. In there, we already build a whole log object and all you need to do is to push the log using your provider. Take a look at how test for already existign adapter looks or use template below:
After you finished adding your new adapter, you should write a proper test for it. To do that, you enter `tests/LoggerTests.php` and take a look at `testAdapters()` method. In there, we already build a whole log object and all you need to do is to push the log using your provider. Take a look at how test for already existing adapter looks or use the template below:

```php
// Test [ADAPTER_NAME]
Expand Down
161 changes: 161 additions & 0 deletions src/Logger/Adapter/HoneyBadger.php
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()),
Copy link
Contributor

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?

'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
];
}
}
3 changes: 2 additions & 1 deletion src/Logger/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ class Logger
"raygun",
"sentry",
"appSignal",
"logOwl"
"logOwl",
"honeyBadger"
];

/**
Expand Down
22 changes: 14 additions & 8 deletions tests/LoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use PHPUnit\Framework\TestCase;

use Utopia\Logger\Adapter\AppSignal;
use Utopia\Logger\Adapter\HoneyBadger;
use Utopia\Logger\Adapter\LogOwl;
use Utopia\Logger\Adapter\Raygun;
use Utopia\Logger\Adapter\Sentry;
Expand Down Expand Up @@ -75,11 +76,11 @@ public function testLog()
self::assertEquals("aws-001", $log->getServer());

$log->addExtra('isLoggedIn', false);
self::assertEquals([ 'isLoggedIn' => false ], $log->getExtra());
self::assertEquals(['isLoggedIn' => false], $log->getExtra());

$log->addTag('authMethod', 'session');
$log->addTag('authProvider', 'basic');
self::assertEquals([ 'authMethod' => 'session', 'authProvider' => 'basic' ], $log->getTags());
self::assertEquals(['authMethod' => 'session', 'authProvider' => 'basic'], $log->getTags());

$user = new User("myid123");
$log->setUser($user);
Expand All @@ -93,7 +94,6 @@ public function testLog()
self::assertEquals("http", $log->getBreadcrumbs()[0]->getCategory());
self::assertEquals("DELETE /api/v1/database/abcd1234/efgh5678", $log->getBreadcrumbs()[0]->getMessage());
self::assertEquals($timestamp, $log->getBreadcrumbs()[0]->getTimestamp());

}

/**
Expand Down Expand Up @@ -175,10 +175,16 @@ public function testAdapters()
$response = $logger->addLog($log);
$this->assertEquals(202, $response);

// Test LogOwl
$adapter = new LogOwl(\getenv("TEST_LOGOWL_KEY"));
$logger = new Logger($adapter);
$response = $logger->addLog($log);
$this->assertEquals(200, $response);
// Test LogOwl
$adapter = new LogOwl(\getenv("TEST_LOGOWL_KEY"));
$logger = new Logger($adapter);
$response = $logger->addLog($log);
$this->assertEquals(200, $response);

// Test HoneyBadger
$adapter = new HoneyBadger(\getenv("TEST_HONEYBADGER_KEY"));
2002Bishwajeet marked this conversation as resolved.
Show resolved Hide resolved
$logger = new Logger($adapter);
$response = $logger->addLog($log);
2002Bishwajeet marked this conversation as resolved.
Show resolved Hide resolved
$this->assertEquals(201, $response);
}
}