Skip to content

Commit

Permalink
Merge pull request #25 from facade/add-tracking-uuid
Browse files Browse the repository at this point in the history
Add tracking UUID
  • Loading branch information
freekmurze authored Sep 13, 2021
2 parents 47b639d + a38e32d commit 489c17b
Show file tree
Hide file tree
Showing 16 changed files with 83 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/php-cs-fixer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- name: Fix style
uses: docker://oskarstark/php-cs-fixer-ga
with:
args: --config=.php_cs --allow-risky=yes
args: --config=.php-cs-fixer.php --allow-risky=yes

- name: Extract branch name
shell: bash
Expand Down
1 change: 1 addition & 0 deletions .php-cs-fixer.cache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"php":"8.0.10","version":"3.1.0","indent":" ","lineEnding":"\n","rules":{"blank_line_after_opening_tag":true,"braces":{"allow_single_line_anonymous_class_with_empty_body":true},"compact_nullable_typehint":true,"declare_equal_normalize":true,"lowercase_cast":true,"lowercase_static_reference":true,"new_with_braces":true,"no_blank_lines_after_class_opening":true,"no_leading_import_slash":true,"no_whitespace_in_blank_line":true,"ordered_class_elements":{"order":["use_trait"]},"ordered_imports":{"sort_algorithm":"alpha"},"return_type_declaration":true,"short_scalar_cast":true,"single_blank_line_before_namespace":true,"single_trait_insert_per_statement":true,"ternary_operator_spaces":true,"visibility_required":true,"blank_line_after_namespace":true,"class_definition":true,"constant_case":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":{"on_multiline":"ensure_fully_multiline","keep_multiple_spaces_after_comma":true},"no_break_comment":true,"no_closing_tag":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_import_per_statement":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"encoding":true,"full_opening_tag":true,"array_syntax":{"syntax":"short"},"no_unused_imports":true,"not_operator_with_successor_space":true,"trailing_comma_in_multiline":true,"phpdoc_scalar":true,"unary_operator_spaces":true,"binary_operator_spaces":true,"blank_line_before_statement":{"statements":["break","continue","declare","return","throw","try"]},"phpdoc_single_line_var_spacing":true,"phpdoc_var_without_name":true,"class_attributes_separation":{"elements":{"method":"one"}}},"hashes":{"src\/Middleware\/AddGlows.php":2757630618,"src\/Middleware\/AnonymizeIp.php":2582237091,"src\/Middleware\/CensorRequestBodyFields.php":3157018559,"src\/Time\/Time.php":3063453905,"src\/Time\/SystemTime.php":1875330795,"src\/Solutions\/ReportSolution.php":1414311092,"src\/View.php":1758466251,"src\/Enums\/GroupingTypes.php":794823242,"src\/Enums\/MessageLevels.php":2252993314,"src\/Glows\/Glow.php":2938926460,"src\/Glows\/Recorder.php":741799100,"src\/Stacktrace\/Codesnippet.php":1758681831,"src\/Stacktrace\/File.php":2191220409,"src\/Stacktrace\/Stacktrace.php":3487330919,"src\/Stacktrace\/Frame.php":2201806026,"src\/Truncation\/ReportTrimmer.php":1411772441,"src\/Truncation\/TrimStringsStrategy.php":1149918480,"src\/Truncation\/TrimContextItemsStrategy.php":2488083767,"src\/Truncation\/TruncationStrategy.php":2876512000,"src\/Truncation\/AbstractTruncationStrategy.php":3738899546,"src\/Flare.php":615945847,"src\/Http\/Response.php":3175166434,"src\/Http\/Exceptions\/BadResponse.php":2136825932,"src\/Http\/Exceptions\/MissingParameter.php":3963873571,"src\/Http\/Exceptions\/NotFound.php":605045793,"src\/Http\/Exceptions\/BadResponseCode.php":3629899270,"src\/Http\/Exceptions\/InvalidData.php":4224780353,"src\/Http\/Client.php":3513047095,"src\/Contracts\/ProvidesFlareContext.php":3654647562,"src\/Report.php":677962539,"src\/helpers.php":878229930,"src\/Api.php":4147640268,"src\/Concerns\/UsesTime.php":2203258051,"src\/Concerns\/HasContext.php":2283577173,"src\/Context\/ContextContextDetector.php":1433590813,"src\/Context\/ConsoleContext.php":2891436865,"src\/Context\/ContextInterface.php":3556428806,"src\/Context\/RequestContext.php":916577092,"src\/Context\/ContextDetectorInterface.php":3409530978,"src\/Frame.php":1560844999,"tests\/TestClasses\/ExceptionWithContext.php":3729019575,"tests\/TestClasses\/Assert.php":151773303,"tests\/TestClasses\/DumpDriver.php":3041182929,"tests\/TestClasses\/CodeSnippetDriver.php":615657858,"tests\/TestClasses\/FakeTime.php":2977551027,"tests\/TestClasses\/ReportDriver.php":3461657491,"tests\/FlareTest.php":2320308878,"tests\/Glows\/RecorderTest.php":946753721,"tests\/Stacktrace\/FileTest.php":420603250,"tests\/Stacktrace\/__snapshots__\/StrackTraceTest__it_can_detect_application_frames__1.php":167790125,"tests\/Stacktrace\/CodesnippetTest.php":765068665,"tests\/Stacktrace\/CodeSnippetDriver.php":4244799073,"tests\/Stacktrace\/ThrowAndReturnExceptionAction.php":4198281204,"tests\/Stacktrace\/StrackTraceTest.php":757065022,"tests\/Truncation\/TrimContextItemsStrategyTest.php":112441598,"tests\/Truncation\/TrimStringsStrategyTest.php":2187306892,"tests\/TestCase.php":996592721,"tests\/Concerns\/MatchesCodeSnippetSnapshots.php":685279681,"tests\/Concerns\/MatchesDumpSnapshots.php":3419801558,"tests\/Concerns\/MatchesReportSnapshots.php":2362276842,"tests\/Mocks\/FakeClient.php":1694586070,"tests\/ReportTest.php":1195087882,"tests\/Context\/ConsoleContextTest.php":3637467343,"tests\/Context\/RequestContextTest.php":1346110003}}
16 changes: 11 additions & 5 deletions .php_cs → .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
->ignoreDotFiles(true)
->ignoreVCS(true);

return PhpCsFixer\Config::create()
return (new PhpCsFixer\Config())
->setRules([
'@PSR2' => true,
'@PSR12' => true,
'array_syntax' => ['syntax' => 'short'],
'ordered_imports' => ['sortAlgorithm' => 'alpha'],
'ordered_imports' => ['sort_algorithm' => 'alpha'],
'no_unused_imports' => true,
'not_operator_with_successor_space' => true,
'trailing_comma_in_multiline_array' => true,
'trailing_comma_in_multiline' => true,
'phpdoc_scalar' => true,
'unary_operator_spaces' => true,
'binary_operator_spaces' => true,
Expand All @@ -30,9 +30,15 @@
],
'phpdoc_single_line_var_spacing' => true,
'phpdoc_var_without_name' => true,
'class_attributes_separation' => [
'elements' => [
'method' => 'one',
],
],
'method_argument_space' => [
'on_multiline' => 'ensure_fully_multiline',
'keep_multiple_spaces_after_comma' => true,
]
],
'single_trait_insert_per_statement' => true,
])
->setFinder($finder);
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to `flare-client-php` will be documented in this file

## 1.9.0 - 2021-09-13

- add report tracking uuid

## 1.8.1 - 2021-05-31

- improve compatibility with Symfony 5.3
Expand Down
1 change: 0 additions & 1 deletion src/Context/RequestContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Facade\FlareClient\Context;

use BadMethodCallException;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
Expand Down
4 changes: 2 additions & 2 deletions src/Enums/GroupingTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/** @deprecated */
class GroupingTypes
{
const TOP_FRAME = 'topFrame';
public const TOP_FRAME = 'topFrame';

const EXCEPTION = 'exceptionClass';
public const EXCEPTION = 'exceptionClass';
}
10 changes: 5 additions & 5 deletions src/Enums/MessageLevels.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

class MessageLevels
{
const INFO = 'info';
public const INFO = 'info';

const DEBUG = 'debug';
public const DEBUG = 'debug';

const WARNING = 'warning';
public const WARNING = 'warning';

const ERROR = 'error';
public const ERROR = 'error';

const CRITICAL = 'critical';
public const CRITICAL = 'critical';
}
2 changes: 1 addition & 1 deletion src/Glows/Recorder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class Recorder
{
const GLOW_LIMIT = 30;
public const GLOW_LIMIT = 30;

private $glows = [];

Expand Down
38 changes: 37 additions & 1 deletion src/Report.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

class Report
{
use UsesTime, HasContext;
use UsesTime;
use HasContext;

/** @var \Facade\FlareClient\Stacktrace\Stacktrace */
private $stacktrace;
Expand Down Expand Up @@ -65,6 +66,12 @@ class Report
/** @var string */
private $groupBy ;

/** @var string */
private $trackingUuid;

/** @var null string|null */
public static $fakeTrackingUuid = null;

public static function createForThrowable(
Throwable $throwable,
ContextInterface $context,
Expand Down Expand Up @@ -106,6 +113,16 @@ public static function createForMessage(string $message, string $logLevel, Conte
->openFrameIndex($stacktrace->firstApplicationFrameIndex());
}

public function __construct()
{
$this->trackingUuid = self::$fakeTrackingUuid ?? $this->generateUuid();
}

public function trackingUuid(): string
{
return $this->trackingUuid;
}

public function exceptionClass(string $exceptionClass)
{
$this->exceptionClass = $exceptionClass;
Expand Down Expand Up @@ -294,6 +311,25 @@ public function toArray()
'open_frame_index' => $this->openFrameIndex,
'application_path' => $this->applicationPath,
'application_version' => $this->applicationVersion,
'tracking_uuid' => $this->trackingUuid,
];
}

/*
* Found on https://stackoverflow.com/questions/2040240/php-function-to-generate-v4-uuid/15875555#15875555
*/
private function generateUuid(): string
{
// Generate 16 bytes (128 bits) of random data or use the data passed into the function.
$data = $data ?? random_bytes(16);
assert(strlen($data) == 16);

// Set version to 0100
$data[6] = chr(ord($data[6]) & 0x0f | 0x40);
// Set bits 6-7 to 10
$data[8] = chr(ord($data[8]) & 0x3f | 0x80);

// Output the 36 character UUID.
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}
}
1 change: 0 additions & 1 deletion tests/FlareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ protected function reportError(int $code)
$this->flare->report($throwable);
}


/** @test */
public function it_can_report_an_exception()
{
Expand Down
10 changes: 10 additions & 0 deletions tests/ReportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,14 @@ public function it_can_create_a_report_with_meta_data()

$this->assertEquals($metadata, $report->toArray()['context']['meta']);
}

/** @test */
public function it_will_generate_a_uuid()
{
$report = Report::createForThrowable(new Exception('this is an exception'), new ConsoleContext());

$this->assertIsString($report->trackingUuid());

$this->assertIsString($report->toArray()['tracking_uuid']);
}
}
7 changes: 7 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@

class TestCase extends BaseTestCase
{
public function setUp()
{
parent::setUp();

Report::$fakeTrackingUuid = 'fake-uuid';
}

public static function makePathsRelative(string $text): string
{
return str_replace(dirname(__DIR__, 1), '', $text);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ message_level: null
open_frame_index: null
application_path: null
application_version: null
tracking_uuid: fake-uuid
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ message_level: null
open_frame_index: null
application_path: null
application_version: null
tracking_uuid: fake-uuid
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ message_level: null
open_frame_index: 0
application_path: null
application_version: null
tracking_uuid: fake-uuid
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ message_level: null
open_frame_index: null
application_path: null
application_version: null
tracking_uuid: fake-uuid

0 comments on commit 489c17b

Please sign in to comment.