Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for using own Representation Serializer #671

Open
annuh opened this issue Nov 9, 2022 · 2 comments
Open

Support for using own Representation Serializer #671

annuh opened this issue Nov 9, 2022 · 2 comments

Comments

@annuh
Copy link
Contributor

annuh commented Nov 9, 2022

I would like to support something similar as discussed in this issue: getsentry/sentry-php#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 annuh changed the title Support for using own Serializer Support for using own Representation Serializer Nov 9, 2022
@Jean85
Copy link
Collaborator

Jean85 commented Nov 9, 2022

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.

@stayallive
Copy link
Collaborator

stayallive commented Nov 9, 2022

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants