Skip to content

Commit c6fd2c9

Browse files
Revert "Enable context-flattening to support context field searching"
1 parent e6c0322 commit c6fd2c9

3 files changed

Lines changed: 5 additions & 375 deletions

File tree

src/Instrumentation/Laravel/README.md

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,3 @@ The extension can be disabled via [runtime configuration](https://opentelemetry.
2222
```shell
2323
OTEL_PHP_DISABLED_INSTRUMENTATIONS=laravel
2424
```
25-
26-
### Log Context Attributes
27-
28-
By default, log context is JSON-encoded into a single `context` attribute. This can make it difficult to search for specific context values in observability backends like SigNoz.
29-
30-
To flatten log context into individual, searchable attributes, enable:
31-
32-
```shell
33-
OTEL_PHP_LARAVEL_LOG_ATTRIBUTES_FLATTEN=true
34-
```
35-
36-
**Default behavior (off):**
37-
```
38-
context: {"http":{"method":"GET","path":"/users"},"user_id":"123"}
39-
```
40-
41-
**With flattening enabled:**
42-
```
43-
http.method: GET
44-
http.path: /users
45-
user_id: 123
46-
```
47-
48-
Nested arrays are flattened using dot notation, making each value individually searchable in your observability backend.

src/Instrumentation/Laravel/src/Watchers/LogWatcher.php

Lines changed: 5 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,17 @@
77
use Illuminate\Contracts\Foundation\Application;
88
use Illuminate\Log\Events\MessageLogged;
99
use Illuminate\Log\LogManager;
10-
use Illuminate\Support\Arr;
1110
use OpenTelemetry\API\Instrumentation\CachedInstrumentation;
12-
use OpenTelemetry\API\Instrumentation\ConfigurationResolver;
1311
use OpenTelemetry\API\Logs\Severity;
14-
use Stringable;
1512
use Throwable;
1613
use TypeError;
1714

1815
class LogWatcher extends Watcher
1916
{
20-
/**
21-
* When enabled, log context attributes are spread as individual OTLP attributes
22-
* instead of being JSON-encoded into a single 'context' attribute.
23-
* This improves searchability in observability backends like SigNoz.
24-
*/
25-
public const OTEL_PHP_LARAVEL_LOG_ATTRIBUTES_FLATTEN = 'OTEL_PHP_LARAVEL_LOG_ATTRIBUTES_FLATTEN';
26-
2717
private LogManager $logger;
28-
private bool $flattenAttributes;
29-
3018
public function __construct(
3119
private CachedInstrumentation $instrumentation,
3220
) {
33-
$this->flattenAttributes = $this->shouldFlattenAttributes();
3421
}
3522

3623
/** @psalm-suppress UndefinedInterfaceMethod */
@@ -69,28 +56,20 @@ public function recordLog(MessageLogged $log): void
6956
->logger()
7057
->logRecordBuilder();
7158

72-
$contextToProcess = array_filter($log->context, static fn ($value) => $value !== null);
59+
$context = array_filter($log->context, static fn ($value) => $value !== null);
7360
$exception = $this->getExceptionFromContext($log->context);
7461

7562
if ($exception !== null) {
7663
$logBuilder->setException($exception);
7764

78-
unset($contextToProcess['exception']);
65+
unset($context['exception']);
7966
}
8067

8168
$logBuilder->setBody($log->message)
8269
->setSeverityText($log->level)
83-
->setSeverityNumber(Severity::fromPsr3($log->level));
84-
85-
if ($this->flattenAttributes) {
86-
foreach ($this->buildFlattenedAttributes($contextToProcess) as $key => $value) {
87-
$logBuilder->setAttribute($key, $value);
88-
}
89-
} else {
90-
$logBuilder->setAttribute('context', json_encode($contextToProcess) ?: '{}');
91-
}
92-
93-
$logBuilder->emit();
70+
->setSeverityNumber(Severity::fromPsr3($log->level))
71+
->setAttribute('context', $context)
72+
->emit();
9473
}
9574

9675
private function getExceptionFromContext(array $context): ?Throwable
@@ -104,41 +83,4 @@ private function getExceptionFromContext(array $context): ?Throwable
10483

10584
return $context['exception'];
10685
}
107-
108-
private function shouldFlattenAttributes(): bool
109-
{
110-
$resolver = new ConfigurationResolver();
111-
112-
return $resolver->has(self::OTEL_PHP_LARAVEL_LOG_ATTRIBUTES_FLATTEN)
113-
&& $resolver->getBoolean(self::OTEL_PHP_LARAVEL_LOG_ATTRIBUTES_FLATTEN);
114-
}
115-
116-
/**
117-
* Build flattened attributes from context array.
118-
* Nested arrays are flattened with dot notation for better searchability.
119-
*
120-
* @return array<string, mixed>
121-
*/
122-
private function buildFlattenedAttributes(array $context): array
123-
{
124-
return array_map(fn ($value) => $this->normalizeValue($value), Arr::dot($context));
125-
}
126-
127-
/**
128-
* Normalize a value for OTLP attributes.
129-
* OTLP attributes support: string, bool, int, float, and arrays of these.
130-
*/
131-
private function normalizeValue(mixed $value): string|bool|int|float|null
132-
{
133-
if ($value === null || is_scalar($value)) {
134-
return $value;
135-
}
136-
137-
if ($value instanceof Stringable) {
138-
return (string) $value;
139-
}
140-
141-
// For objects that can't be stringified, JSON encode them
142-
return json_encode($value) ?: null;
143-
}
14486
}

0 commit comments

Comments
 (0)