Skip to content

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

@nickmarden

Description

@nickmarden

This feature is specific to the Laravel auto-instrumentation package and how it serializes PSR-3 log context for OTLP export. It does not require specification changes.

Is your feature request related to a problem?

The Laravel LogWatcher 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 difficult or impossible to search/filter logs by specific context values in observability backends like SigNoz, Jaeger, or Grafana. Users cannot query for http.method = "GET" or user_id = "123" because these values are buried inside an opaque JSON string rather than being individual, indexed attributes.

Describe the solution you'd like

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

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

Default behavior (off) would remain unchanged for backward compatibility. An argument could be made for making this behavior on by default in future versions, but for now simply having the option could be very useful for users.

Describe alternatives you've considered

  1. Using PSR-3 instrumentation's MODE_EXPORT: This already spreads context as individual attributes, but it causes duplicate log records when used alongside Laravel's LogWatcher since both emit logs independently with no coordination between them.
  2. Post-processing with a LogRecordProcessor: Not viable because attributes are immutable by the time processors receive the record.
  3. Parsing the JSON in the observability backend: Possible in some backends but defeats the purpose of structured logging and varies by vendor.

Additional context

I have a working implementation ready as a PR that:

  • Uses ConfigurationResolver consistent with other OTEL PHP configuration patterns
  • Recursively flattens nested arrays with dot notation
  • Handles Stringable objects, scalars, and edge cases appropriately
  • Preserves existing exception attribute handling
  • Includes comprehensive unit tests
  • Is fully backward compatible (off by default)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions