Skip to content

Comments

Enable context-flattening to support context field searching#491

Open
nickmarden wants to merge 4 commits intoopen-telemetry:mainfrom
nickmarden:feat/first-class-context-fields
Open

Enable context-flattening to support context field searching#491
nickmarden wants to merge 4 commits intoopen-telemetry:mainfrom
nickmarden:feat/first-class-context-fields

Conversation

@nickmarden
Copy link

@nickmarden nickmarden commented Dec 17, 2025

Description

Resolves open-telemetry/opentelemetry-php#1837

This PR adds an optional configuration to flatten log context attributes into individual OTLP attributes, improving searchability in observability backends.

Problem

The Laravel LogWatcher currently JSON-encodes the entire log context into a single context attribute:

context: {"http":{"method":"GET","path":"/users","status":200},"user_id":"123"}

This makes it impossible to search/filter by specific values (e.g., http.method = "GET") in backends like SigNoz, Jaeger, or Grafana.

Solution

A new configuration option OTEL_PHP_LARAVEL_LOG_ATTRIBUTES_FLATTEN that, when enabled, spreads context as individual attributes with dot-notation for nested arrays:

http.method: GET
http.path: /users
http.status: 200
user_id: 123

Changes

  • src/Watchers/LogWatcher.php: Added flattening logic with ConfigurationResolver, dot-notation for nested arrays, and proper handling of Stringable objects
  • README.md: Documented the new configuration option with examples
  • tests/Unit/Watchers/LogWatcherTest.php: Added 11 unit tests covering default behavior, flattened mode, nested arrays, exceptions, and edge cases

Backward Compatibility

  • Default behavior unchanged (off by default)
  • Existing tests continue to pass

Testing

./vendor/bin/phpunit --testsuite=unit # 18 tests, 42 assertions
./vendor/bin/phpunit --testsuite=integration # 21 tests, 120 assertions

Screenshot

Screenshot 2025-12-17 at 6 56 35 PM

@nickmarden nickmarden requested a review from a team as a code owner December 17, 2025 23:10
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Dec 17, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

@codecov
Copy link

codecov bot commented Dec 19, 2025

Codecov Report

❌ Patch coverage is 94.73684% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.48%. Comparing base (a3c02c8) to head (2e340d8).

Files with missing lines Patch % Lines
...nstrumentation/Laravel/src/Watchers/LogWatcher.php 94.73% 2 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff              @@
##               main     #491      +/-   ##
============================================
+ Coverage     83.46%   83.48%   +0.02%     
- Complexity     2330     2344      +14     
============================================
  Files           149      149              
  Lines          8791     8816      +25     
============================================
+ Hits           7337     7360      +23     
- Misses         1454     1456       +2     
Flag Coverage Δ
Aws 93.37% <ø> (ø)
Context/Swoole 0.00% <ø> (ø)
Exporter/Instana 49.80% <ø> (ø)
Instrumentation/AwsSdk 82.00% <ø> (ø)
Instrumentation/CakePHP 20.42% <ø> (ø)
Instrumentation/CodeIgniter 79.31% <ø> (ø)
Instrumentation/Curl 86.88% <ø> (-0.28%) ⬇️
Instrumentation/Doctrine 92.82% <ø> (ø)
Instrumentation/ExtAmqp 88.80% <ø> (ø)
Instrumentation/ExtRdKafka 86.13% <ø> (ø)
Instrumentation/Guzzle 76.25% <ø> (ø)
Instrumentation/HttpAsyncClient 78.94% <ø> (ø)
Instrumentation/IO 0.00% <ø> (ø)
Instrumentation/Laravel 72.38% <94.73%> (+0.73%) ⬆️
Instrumentation/MongoDB 76.84% <ø> (ø)
Instrumentation/MySqli 93.39% <ø> (ø)
Instrumentation/OpenAIPHP 86.71% <ø> (ø)
Instrumentation/PDO 85.67% <ø> (ø)
Instrumentation/PostgreSql 91.36% <ø> (ø)
Instrumentation/Psr14 77.41% <ø> (ø)
Instrumentation/Psr15 89.74% <ø> (ø)
Instrumentation/Psr16 97.43% <ø> (ø)
Instrumentation/Psr18 79.41% <ø> (ø)
Instrumentation/Psr3 67.70% <ø> (ø)
Instrumentation/Psr6 97.56% <ø> (ø)
Instrumentation/ReactPHP 99.41% <ø> (ø)
Instrumentation/Session 94.28% <ø> (ø)
Instrumentation/Slim 84.21% <ø> (ø)
Instrumentation/Yii 83.33% <ø> (ø)
Logs/Monolog 100.00% <ø> (ø)
Propagation/CloudTrace 90.69% <ø> (ø)
Propagation/Instana 98.07% <ø> (ø)
Propagation/ServerTiming 94.73% <ø> (ø)
Propagation/TraceResponse 94.73% <ø> (ø)
ResourceDetectors/Azure 91.66% <ø> (ø)
ResourceDetectors/Container 93.02% <ø> (ø)
ResourceDetectors/DigitalOcean 100.00% <ø> (ø)
Sampler/Xray 78.38% <ø> (ø)
Shims/OpenTracing 92.99% <ø> (ø)
SqlCommenter 95.58% <ø> (ø)
Symfony 88.14% <ø> (ø)
Utils/Test 87.79% <ø> (ø)

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

Files with missing lines Coverage Δ
...nstrumentation/Laravel/src/Watchers/LogWatcher.php 96.42% <94.73%> (-0.35%) ⬇️

... and 1 file 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 a3c02c8...2e340d8. Read the comment docs.

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

@nickmarden nickmarden force-pushed the feat/first-class-context-fields branch from c13169b to 2e340d8 Compare December 19, 2025 20:38
@nickmarden
Copy link
Author

@brettmc I'm unclear how to trigger another CI run. I believe I've fixed all but one of the previously-failing CI tests, but I can't be sure. Is this something you need to trigger, or can I?

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.

[Laravel] Add option to flatten log context attributes for better searchability

2 participants