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

#632 : Move stack trace formatting out of Span class #694

Merged
merged 3 commits into from
Jun 3, 2022

Conversation

amber0612
Copy link
Contributor

@amber0612 amber0612 commented Jun 1, 2022

closes #632

@welcome
Copy link

welcome bot commented Jun 1, 2022

Thanks for opening your first pull request! If you haven't yet signed our Contributor License Agreement (CLA), then please do so that we can accept your contribution. A link should appear shortly in this PR if you have not already signed one.

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Jun 1, 2022

CLA Signed

The committers listed above are authorized under a signed CLA.

@brettmc
Copy link
Collaborator

brettmc commented Jun 2, 2022

@amber0612 - can you please fix up linting errors with this PR? (make style will do it, and make all to confirm locally that everything is happy)

@codecov
Copy link

codecov bot commented Jun 2, 2022

Codecov Report

Merging #694 (c8365d1) into main (85282b5) will decrease coverage by 2.25%.
The diff coverage is 2.63%.

Impacted file tree graph

@@             Coverage Diff              @@
##               main     #694      +/-   ##
============================================
- Coverage     86.07%   83.82%   -2.26%     
- Complexity     1151     1233      +82     
============================================
  Files           128      138      +10     
  Lines          2794     2980     +186     
============================================
+ Hits           2405     2498      +93     
- Misses          389      482      +93     
Flag Coverage Δ
7.4 83.82% <2.63%> (-2.22%) ⬇️
8.0 83.87% <2.63%> (-2.21%) ⬇️
8.1 83.87% <2.63%> (-2.21%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/SDK/Common/Util/TracingUtil.php 0.00% <0.00%> (ø)
src/SDK/Trace/Span.php 93.69% <100.00%> (-0.23%) ⬇️
src/Context/Context.php 82.92% <0.00%> (ø)
src/Contrib/Prometheus/PrometheusExporter.php 93.75% <0.00%> (ø)
src/Context/FiberNotSupportedContextStorage.php
src/Context/ScopeBound/ScopeBoundPromise.php 96.29% <0.00%> (ø)
src/Context/FiberBoundContextStorageScope.php 0.00% <0.00%> (ø)
src/Context/ScopeBound/ContextHolder.php 100.00% <0.00%> (ø)
src/API/Trace/Propagation/B3MultiPropagator.php 100.00% <0.00%> (ø)
... and 9 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 85282b5...c8365d1. Read the comment docs.

@brettmc
Copy link
Collaborator

brettmc commented Jun 2, 2022

Can you sort out signing a CLA please? (We can't accept your contribution without this) And also try to write a unit test for the newly-created class, since code coverage has dropped with this refactor.

@amber0612
Copy link
Contributor Author

Can you sort out signing a CLA please? (We can't accept your contribution without this) And also try to write a unit test for the newly-created class, since code coverage has dropped with this refactor.

Hi Bret, Unit test for formatStackTrace was not written previously. Can u help me with what am i supposed to assert equal for Unit test of the formatStackTrace function. As the function returns String which is stack Trace and string returned will be complex with Line numbers too. Do u have ideas on how to handle unit test for this...

@Nevay
Copy link
Contributor

Nevay commented Jun 3, 2022

I would write them as .phpt tests to have a clean stacktrace. There is an open issue with PHPUnit adding an additional frame; we can use run-tests.php instead (at least for now).

--TEST--
Basic stacktrace format
--FILE--
<?php
use OpenTelemetry\SDK\Common\Util\TracingUtil;

require_once 'vendor/autoload.php';

echo TracingUtil::formatStackTrace(new Exception('message')), "\n";
?>
--EXPECT--
Exception: message
 at main(test_stacktrace_basic.php:6)

(I plan on creating a follow-up PR to resolve some issues with the current implementation and could take care of the tests if necessary.)


use Throwable;

class TracingUtil
Copy link
Member

@tidal tidal Jun 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please rename the class to OpenTelemetry\SDK\Common\Exception\StackTraceFormatter and the method to just format, please?

*/
public static function formatStackTrace(Throwable $e, array &$seen = null): string
{
$starter = $seen ? 'Caused by: ' : '';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you make Caused by: a constant, please?

{
$starter = $seen ? 'Caused by: ' : '';
$result = [];
if (!$seen) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please check for a null value here, please?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tidal I plan on replacing the implementation with a reduced version of this after this PR is merged, which would make most of these changes obsolete; can we postpone implementation specific changes and keep this PR about moving the implementation out of the Span class?

See below for example output that highlights some problems, changes:

  • Fix "... n more" to fold only identical frames, currently we might lose information (folding on first seen frame instead of folding last n identical frames as described by Java)
  • Fix exception class names not being converted to dotted format
  • Fix functions being shown as main
  • Fix out-of-memory on circular exception
# new
Abc.Def.Test: bar
	at Abc.Def.Test.create(Test.php:10)
	at Abc.Def.run(example.php:8)
	at {main}(example.php:12)
Caused by: Abc.Def.Test: foo
	at Abc.Def.Test.create(Test.php:10)
	at Abc.Def.run(example.php:7)
	... 1 more
# current
Abc\Def\Test: bar
 at Abc.Def.Test.create(Test.php:10)
 at main(example.php:8)
 at main(example.php:12)
Caused by: Abc\Def\Test: foo
 ... 3 more

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

$file = $e->getFile();
$line = $e->getLine();
while (true) {
$current = "$file:$line";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you make this a sprintf as above, please?


$traceHasKey = array_key_exists(0, $trace);
$traceKeyHasClass = $traceHasKey && array_key_exists('class', $trace[0]);
$traceKeyHasFunction = $traceKeyHasClass && array_key_exists('function', $trace[0]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please male classand function class constants, please?

$line === null ? '' : ':',
$line ?? ''
);
$seen[] = "$file:$line";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you make this a sprintf call, please?

$line ?? ''
);
$seen[] = "$file:$line";
if (!count($trace)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please check for a numeric value here, please?

if (!count($trace)) {
break;
}
$file = array_key_exists('file', $trace[0]) ? $trace[0]['file'] : 'Unknown Source';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you male Unknown Source a class constant, please?

break;
}
$file = array_key_exists('file', $trace[0]) ? $trace[0]['file'] : 'Unknown Source';
$line = array_key_exists('file', $trace[0]) && array_key_exists('line', $trace[0]) && $trace[0]['line'] ? $trace[0]['line'] : null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you make line a class constant, please?

array_shift($trace);
}
$result = implode("\n", $result);
if ($prev) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you check for a type here, please?

@tidal
Copy link
Member

tidal commented Jun 3, 2022

@brettmc
We can take care of a test later,
so I'm merging this for now.

@tidal tidal merged commit e728c53 into open-telemetry:main Jun 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Move stack trace formatting out of Span class
4 participants