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 datadog adapter #31

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions src/Logger/Adapter/Datadog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace Utopia\Logger\Adapter;

use Utopia\Logger\Adapter;
use Utopia\Logger\Log;
use Utopia\Logger\Logger;

class Datadog extends Adapter
{
private $apiKey;
private $apiEndpoint;

public static function getName(): string
{
return "datadog";
}

public function __construct(string $apiKey)
{
$this->apiKey = $apiKey;
$this->apiEndpoint = "https://http-intake.logs.datadoghq.com/v1/input/{$this->apiKey}";
}

public function push(Log $log): int
{
$data = [
"message" => $log->getMessage(),
"level" => $log->getLevel(),
"timestamp" => $log->getTimestamp(),
"tags" => $log->getTags(),
// Add any additional fields specific to Datadog logging
];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->apiEndpoint);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
$statusCode = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);

if (curl_errno($ch)) {
throw new \Exception('Error pushing log to Datadog: ' . curl_error($ch));
}

curl_close($ch);

return $statusCode;
}

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_WARNING,
Log::TYPE_ERROR,
];
}
}
1 change: 1 addition & 0 deletions src/Logger/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Logger
'sentry',
'appSignal',
'logOwl',
'datadog',
];

/**
Expand Down
15 changes: 15 additions & 0 deletions tests/e2e/Adapter/DatadogTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Utopia\Tests\E2E\Adapter;

use Utopia\Logger\Adapter\Datadog;
use Utopia\Tests\E2E\AdapterBase;

class DatadogTest extends AdapterBase
{
protected function setUp(): void
{
parent::setUp();
$this->adapter = new Datadog(\getenv('TEST_DATADOG_KEY') ?: '');
}
}