Skip to content

Commit

Permalink
Merge pull request #99 from stickeeuk/fix/context
Browse files Browse the repository at this point in the history
Fix for non-string context
  • Loading branch information
stickeegreg authored Nov 25, 2024
2 parents c439f0c + d7dd55d commit 5936ecd
Showing 1 changed file with 30 additions and 9 deletions.
39 changes: 30 additions & 9 deletions src/Laravel/Providers/InstrumentationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Stickee\Instrumentation\Laravel\Providers;

use Exception;
use function OpenTelemetry\Instrumentation\hook;
use Illuminate\Console\Events\CommandFinished;
use Illuminate\Contracts\Http\Kernel as KernelInterface;
use Illuminate\Foundation\Application;
Expand Down Expand Up @@ -38,9 +39,9 @@
use Stickee\Instrumentation\Queue\Connectors\SyncConnector;
use Stickee\Instrumentation\Utils\SemConv;
use Stickee\Instrumentation\Watchers\MemoryWatcher;
use Stickee\Instrumentation\Watchers\QueryCountWatcher;

use function OpenTelemetry\Instrumentation\hook;
use Stickee\Instrumentation\Watchers\QueryCountWatcher;
use Throwable;

/**
* Instrumentation service provider
Expand Down Expand Up @@ -175,13 +176,33 @@ private function hook(): void
$scrubber = app(DataScrubberInterface::class);
}

$maxLength = config('instrumentation.scrubbing.max_length');
$message = $params[0];

$message->message = $scrubber->scrub('', mb_substr((string) $message->message, 0, $maxLength));

foreach ($message->context as $key => $value) {
$message->context[$key] = $scrubber->scrub((string) $key, mb_substr((string) $value, 0, $maxLength));
try {
$maxLength = (int) config('instrumentation.scrubbing.max_length', 10240);
$message = $params[0];

try {
$messageString = (string) $message->message;
} catch (Throwable) {
$messageString = 'Non-string error message';
}

$message->message = $scrubber->scrub('', mb_substr($messageString, 0, $maxLength));

foreach ($message->context as $key => $value) {
try {
$stringValue = (string) $value;
} catch (Throwable) {
try {
$stringValue = json_encode($value);
} catch (Throwable) {
$stringValue = 'Non-stringable error context value';
}
}

$message->context[$key] = $scrubber->scrub((string) $key, mb_substr($stringValue, 0, $maxLength));
}
} catch (Throwable) {
// Ignore errors
}

return $params;
Expand Down

0 comments on commit 5936ecd

Please sign in to comment.