Skip to content

Conversation

@cedricziel
Copy link
Contributor

@cedricziel cedricziel commented Apr 8, 2025

This PR improves the trace structure assertion mechanism by implementing a PHPUnit-style diff output for comparing expected and actual trace structures. This enhancement makes it significantly easier for developers to understand what went wrong when trace assertions fail.

Key Improvements

  1. Visual Diff Output
    Added a unified diff format similar to PHPUnit's approach
    Clear visual indicators for additions (+) and removals (-)
    Proper indentation for nested structures
    Consistent formatting for complex trace structures
  2. Human-Readable Formatting
    Span kinds are displayed as readable constants (e.g., KIND_SERVER instead of 2)
    Status codes are displayed as readable constants (e.g., STATUS_ERROR instead of 2)
    Improved formatting for status descriptions and other trace attributes
  3. Recursive Comparison
    Implemented a recursive diffing algorithm that handles nested structures
    Detailed comparison of arrays, including both indexed and associative arrays
    Special handling for trace-specific data structures

Before/After Example

Before:

Expected Trace Structure:
Root Span: "root-span"
Kind: KIND_SERVER
Child Span: "child-span"
Attribute "attribute.three": "value3"

Actual Trace Structure:
Root Span: "root-span"
Kind: KIND_SERVER
Child Span: "child-span"
Missing Attribute: "attribute.three"

After:

--- Expected Trace Structure
+++ Actual Trace Structure
@@ @@
Array (
  [0] => Array (
    ['name'] => "root-span"
    ['kind'] => KIND_SERVER
    ['children'] => Array (
      [0] => Array (
        ['name'] => "child-span"
        ['attributes'] => Array (
        - ['attribute.three'] => "value3"
        )
      )
    )
  )
)

Example with multiple root spans

--- Expected Trace Structure
+++ Actual Trace Structure
@@ @@
Array (
  [0] => Array (
    ['name'] => "root-span-1"
    ['kind'] => KIND_SERVER
    ['attributes'] => Array (
    - ['attribute.one'] => "expected-value-1"
    + ['attribute.one'] => "actual-value-1"
    )
  )
  [1] => Array (
    ['name'] => "root-span-2"
    ['kind'] => KIND_CLIENT
    ['attributes'] => Array (
    - ['attribute.two'] => 24
    + ['attribute.two'] => 42
    - ['attribute.three'] => true
    )
  )
)

@cedricziel cedricziel marked this pull request as ready for review April 8, 2025 11:22
@cedricziel cedricziel requested a review from a team as a code owner April 8, 2025 11:22
@codecov
Copy link

codecov bot commented Apr 8, 2025

Codecov Report

Attention: Patch coverage is 79.66102% with 36 lines in your changes missing coverage. Please review.

Project coverage is 81.88%. Comparing base (ea137c0) to head (22553f3).
Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
.../Test/src/Fluent/TraceAssertionFailedException.php 73.00% 27 Missing ⚠️
...rc/Utils/Test/src/TraceStructureAssertionTrait.php 88.31% 9 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff              @@
##               main     #346      +/-   ##
============================================
+ Coverage     79.45%   81.88%   +2.43%     
- Complexity     1772     1806      +34     
============================================
  Files           137      137              
  Lines          7568     7640      +72     
============================================
+ Hits           6013     6256     +243     
+ Misses         1555     1384     -171     
Flag Coverage Δ
Aws 92.24% <ø> (ø)
Context/Swoole 0.00% <ø> (ø)
Instrumentation/CakePHP 20.27% <ø> (ø)
Instrumentation/CodeIgniter 73.77% <ø> (ø)
Instrumentation/Curl 90.42% <ø> (ø)
Instrumentation/Doctrine 93.25% <ø> (ø)
Instrumentation/ExtAmqp 89.26% <ø> (ø)
Instrumentation/ExtRdKafka 86.23% <ø> (ø)
Instrumentation/Guzzle 69.51% <ø> (ø)
Instrumentation/HttpAsyncClient 78.31% <ø> (ø)
Instrumentation/IO 70.68% <ø> (ø)
Instrumentation/Laravel 64.07% <ø> (ø)
Instrumentation/MongoDB 74.28% <ø> (ø)
Instrumentation/MySqli 95.81% <ø> (ø)
Instrumentation/OpenAIPHP 87.31% <ø> (ø)
Instrumentation/PDO 90.15% <ø> (ø)
Instrumentation/Psr14 77.14% <ø> (ø)
Instrumentation/Psr15 89.41% <ø> (ø)
Instrumentation/Psr16 97.56% <ø> (ø)
Instrumentation/Psr18 77.77% <ø> (ø)
Instrumentation/Psr3 59.49% <ø> (ø)
Instrumentation/Psr6 97.67% <ø> (ø)
Instrumentation/Slim 86.30% <ø> (ø)
Instrumentation/Symfony 84.93% <ø> (ø)
Instrumentation/Yii 77.68% <ø> (ø)
Logs/Monolog 100.00% <ø> (ø)
Propagation/ServerTiming 100.00% <ø> (ø)
Propagation/TraceResponse 100.00% <ø> (ø)
ResourceDetectors/Azure 91.66% <ø> (ø)
ResourceDetectors/Container 93.02% <ø> (ø)
Sampler/RuleBased 33.51% <ø> (ø)
Shims/OpenTracing 92.45% <ø> (ø)
Symfony 87.81% <ø> (ø)
Utils/Test 88.09% <79.66%> (+17.60%) ⬆️

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

Files with missing lines Coverage Δ
...rc/Utils/Test/src/TraceStructureAssertionTrait.php 89.80% <88.31%> (-0.80%) ⬇️
.../Test/src/Fluent/TraceAssertionFailedException.php 77.85% <73.00%> (+16.20%) ⬆️

... and 3 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@cedricziel
Copy link
Contributor Author

@brettmc this one is ready as well.

@brettmc brettmc merged commit 930900c into open-telemetry:main Apr 9, 2025
131 checks passed
@cedricziel cedricziel deleted the test-utils-diff branch April 9, 2025 11:59
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.

2 participants