-
Notifications
You must be signed in to change notification settings - Fork 18
Added Dynatrace Adaptor #21
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
Open
michizhou
wants to merge
3
commits into
utopia-php:main
Choose a base branch
from
michizhou:feat-dynatrace-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.
Open
Changes from all commits
Commits
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 hidden or 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,182 @@ | ||
| <?php | ||
|
|
||
| namespace Utopia\Logger\Adapter; | ||
|
|
||
| use Exception; | ||
| use Utopia\Logger\Adapter; | ||
| use Utopia\Logger\Log; | ||
| use Utopia\Logger\Logger; | ||
|
|
||
| // Reference Material | ||
| // https://www.dynatrace.com/support/help/dynatrace-api/environment-api/events-v2 | ||
|
|
||
| class Dynatrace extends Adapter | ||
| { | ||
| /** | ||
| * @var string (required, can be found in Dynatrace -> Access tokens) | ||
| */ | ||
| protected string $apiKey; | ||
|
|
||
| /** | ||
| * @var string (required, this is part of Dynatrace endpoint: 'https://{{THIS_PART}}.live.dynatrace.com/api/v2/events/ingest') | ||
| */ | ||
| protected string $environmentId; | ||
|
|
||
| /** | ||
| * Return unique adapter name | ||
| * | ||
| * @return string | ||
| */ | ||
| public static function getName(): string | ||
| { | ||
| return "dynatrace"; | ||
| } | ||
|
|
||
| /** | ||
| * Push log to external provider | ||
| * | ||
| * @param Log $log | ||
| * @return int | ||
| * @throws Exception | ||
| */ | ||
| public function push(Log $log): int | ||
| { | ||
| $id = empty($log->getUser()) ? null : $log->getUser()->getId(); | ||
| $email = empty($log->getUser()) ? null : $log->getUser()->getEmail(); | ||
| $username = empty($log->getUser()) ? null : $log->getUser()->getUsername(); | ||
|
|
||
| $breadcrumbsObject = $log->getBreadcrumbs(); | ||
| $breadcrumbsArray = []; | ||
|
|
||
| foreach ($breadcrumbsObject as $breadcrumb) { | ||
| \array_push($breadcrumbsArray, [ | ||
| 'type' => $breadcrumb->getType(), | ||
| 'category' => $breadcrumb->getCategory(), | ||
| 'message' => $breadcrumb->getMessage(), | ||
| 'timestamp' => \intval($breadcrumb->getTimestamp()) | ||
| ]); | ||
| } | ||
|
|
||
| $stackFrames = []; | ||
|
|
||
| if (isset($log->getExtra()['detailedTrace'])) { | ||
| foreach ($log->getExtra()['detailedTrace'] as $trace) { | ||
| \array_push($stackFrames, [ | ||
| 'filename' => $trace['file'], | ||
| 'lineno' => $trace['line'], | ||
| 'function' => $trace['function'], | ||
| ]); | ||
| } | ||
| } | ||
|
|
||
| $tags = array(); | ||
|
|
||
| foreach ($log->getTags() as $tagKey => $tagValue) { | ||
| $tags[$tagKey] = $tagValue; | ||
| } | ||
|
|
||
| if (!empty($log->getType())) { | ||
| $tags['type'] = $log->getType(); | ||
| } | ||
| if (!empty($log->getVersion())) { | ||
| $tags['version'] = $log->getVersion(); | ||
| } | ||
| if (!empty($log->getEnvironment())) { | ||
| $tags['environment'] = $log->getEnvironment(); | ||
| } | ||
| if (!empty($log->getAction())) { | ||
| $tags['action'] = $log->getAction(); | ||
| } | ||
| if (!empty($log->getNamespace())) { | ||
| $tags['namespace'] = $log->getNamespace(); | ||
| } | ||
| if (!empty($log->getServer())) { | ||
| $tags['server'] = $log->getServer(); | ||
| } | ||
|
|
||
| $tags['userId'] = $id; | ||
| $tags['userEmail'] = $email; | ||
| $tags['userName'] = $username; | ||
| $tags['stacktrace'] = $stackFrames; | ||
| $tags['breadcrumbs'] = $breadcrumbsArray; | ||
|
|
||
| // prepare log (request body) | ||
| $requestBody = [ | ||
| 'eventType' => 'ERROR_EVENT', | ||
| 'title' => $log->getMessage(), | ||
| 'startTime' => \intval($log->getTimestamp()), | ||
| 'endTime' => \intval($log->getTimestamp()), | ||
| 'properties' => $tags | ||
| ]; | ||
|
|
||
| // init curl object | ||
| $ch = \curl_init(); | ||
|
|
||
| // define options | ||
| $optArray = array( | ||
| CURLOPT_URL => 'https://' . $this->environmentId . '.live.dynatrace.com/api/v2/events/ingest', | ||
| CURLOPT_RETURNTRANSFER => true, | ||
| CURLOPT_POST => true, | ||
| CURLOPT_POSTFIELDS => \json_encode($requestBody), | ||
| CURLOPT_HEADEROPT => \CURLHEADER_UNIFIED, | ||
| CURLOPT_HTTPHEADER => array('Content-Type: application/json', 'Authorization: Api-Token ' . $this->apiKey) | ||
| ); | ||
|
|
||
| // 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; | ||
| } | ||
|
|
||
| /** | ||
| * Dynatrace constructor. | ||
| * | ||
| * @param string $configKey | ||
| */ | ||
| public function __construct(string $configKey) | ||
| { | ||
| $configChunks = \explode(";", $configKey); | ||
| $this->apiKey = $configChunks[0]; | ||
| $this->environmentId = $configChunks[1]; | ||
| } | ||
|
|
||
| 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 hidden or 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 |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ class Logger | |
| 'sentry', | ||
| 'appSignal', | ||
| 'logOwl', | ||
| 'dynatrace', | ||
| ]; | ||
|
|
||
| /** | ||
|
|
||
This file contains hidden or 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 ran the test, but it failed. After outputting the response from dynatrace, I got:
{"error":{"code":400,"message":"Could not map JSON at \'properties.stacktrace\' near line 1 column 453"}}Were you able to successfully push an event?
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.
@stnguyen90 I don't recall pushing any events to Dynatrace, which is probably why I didn't catch this error. Do you know how we can resolve this issue? I'm willing to help in any way I can.
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.
@michizhou, please refer to Dynatrace's docs and see how to properly pass the stacktrace.