sentry-symfony icon indicating copy to clipboard operation
sentry-symfony copied to clipboard

Support for using own Representation Serializer

Open annuh opened this issue 2 years ago • 2 comments

I would like to support something similar as discussed in this issue: https://github.com/getsentry/sentry-php/issues/889

image
  • Old: Object: App\Entity\SomeEntity
  • New: Object: App\Entity\SomeEntity(#123)

A solution for this would be this:

use Sentry\Serializer\Serializable;

class ExampleObject implements Serializable
{
    private $id = 123;

    public function toSentry(): array
    {
        return [
            'internal_state' => 'Object: ExampleObject(#' . $this->id . ')' ,
        ];
    }
} 

But I would rather have a more re-usable solution, via a custom Serializer:

# sentry.yaml
sentry:
  representation_serializer: App\Sentry\RepresentationSerializer
# App\Sentry\RepresentationSerializer.php

class RepresentationSerializer extends AbstractSerializer implements RepresentationSerializerInterface
{
    public function representationSerialize($value)
    {
        if (\is_object($value)) {
            return 'Object ' . \get_class($value) . '(#' . $value->getId() . ')';
        }
    }
}

For this we need to be able to pass a custom Serializer to this bundle. Would it be possible to support using a custom Serializer with the sentry-symfony bundle? https://github.com/getsentry/sentry-symfony/blob/master/src/DependencyInjection/SentryExtension.php#L123-L125

annuh avatar Nov 09 '22 13:11 annuh

IMHO this should be handled using custom class serializers, which is documented in the readme: https://github.com/getsentry/sentry-symfony#custom-serializers

Adding an interface that has getId to intended classes should make the trick.

Jean85 avatar Nov 09 '22 21:11 Jean85

@Jean85, this was not mentioned here but there already has been some contact around this and for the actual use case (not this example) that won't do (many classes without any base class or interface) and currently the only way around it is implementing a custom Serializer. Using the class serializers would be ideal but not possible/feasible here.

stayallive avatar Nov 09 '22 22:11 stayallive