Skip to content

[Notifier] Add debugData property to SentMessage class #51746

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

Closed

Conversation

ahmedghanem00
Copy link
Contributor

@ahmedghanem00 ahmedghanem00 commented Sep 25, 2023

Q A
Branch? 6.4
Bug fix? no
New feature? yes
Deprecations? no
Tickets Fix #49793
License MIT
Doc PR

As stated in the title, adding such a property would allow transports to include additional information in the returned SentMessage object (e.g. http response)

@OskarStark
Copy link
Contributor

Not sure about this, I mean it makes sense to have more debug data available, but not sure about the method name and signature.

WDYT @fabpot ?

@smnandre
Copy link
Member

The response is already logged in the HttpClient i believe, no ?

I see how this could be used, but maybe add a toggle/config to enable it only in debug mode ?

Maybe i'm wrong, but i'm feeling a bit "afraid" of the added "weight" (messenger / serialization ..)

@nicolas-grekas
Copy link
Member

Looking at the linked issue, this is not about debugging but rather about providing extra info.
Maybe a generic getInfo() could work - and we'd ensure this contains the response header (and the json-decoded body)?

return $this->debugData;
}

public function setDebugData(array $data): void
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should instead be a new constructor argument

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I chose the setter method initially because some transporters may work differently and not have http-response or extra-data to include?

So, it would make sense to make it an optional same as setMessageId()?

Not sure but just clarifying

@ahmedghanem00
Copy link
Contributor Author

ahmedghanem00 commented Sep 29, 2023

Looking at the linked issue, this is not about debugging but rather about providing extra info. Maybe a generic getInfo() could work - and we'd ensure this contains the response header (and the json-decoded body)?

@nicolas-grekas Nice, We can then ensure this via adding the whole response object?

@fabpot
Copy link
Member

fabpot commented Sep 30, 2023

Having a free-form array where each transport can add anything looks like a bad idea.

@nicolas-grekas nicolas-grekas modified the milestones: 6.4, 7.1 Nov 15, 2023
@OskarStark
Copy link
Contributor

@nicolas-grekas what about something like this:

diff --git a/src/Symfony/Component/Notifier/Message/SentMessage.php b/src/Symfony/Component/Notifier/Message/SentMessage.php
index 6febf9ebc9..bd662f68aa 100644
--- a/src/Symfony/Component/Notifier/Message/SentMessage.php
+++ b/src/Symfony/Component/Notifier/Message/SentMessage.php
@@ -19,6 +19,7 @@ class SentMessage
     private MessageInterface $original;
     private string $transport;
     private ?string $messageId = null;
+    private array $info = [];

     public function __construct(MessageInterface $original, string $transport)
     {
@@ -45,4 +46,23 @@ class SentMessage
     {
         return $this->messageId;
     }
+
+    public function setInfo(int $responseStatusCode, array $responseHeaders = [], array $body = []): void
+    {
+        $this->info = [
+            'response' => [
+                'status_code' => $responseStatusCode,
+                'headers' => $responseHeaders,
+                'body' => $body,
+            ]
+        ];
+    }
+
+    /**
+     * @return array{response: array{status_code: int, headers: array, body: array}
+     */
+    public function getInfo(): array
+    {
+        return $this->info;
+    }
 }

@smnandre
Copy link
Member

[...]

With some #[\SensitiveParameter] ? I'm not still sure when to add this attribute or not

@nicolas-grekas
Copy link
Member

Having a free-form array where each transport can add anything looks like a bad idea.

In HttpClient we documented the standard info keys, and decorators can add their own things there alsp (e.g. the number of retries for the RetryableHttpClient). This works well. What's the alternative?

@xabbuh xabbuh modified the milestones: 7.1, 7.2 May 15, 2024
@chaours
Copy link

chaours commented Sep 27, 2024

How can we move forward on that PR ? I think the @OskarStark solution is better than original. We don't want to put debug info but more info from return response if provided. Do you want me to make another PR with your solution directly ? Or could we update this one ? We've been waiting for this improvement for a long time now to pick the fees natively. Is the header is really necessary inside info (too much as @smnandre said) ?

@fabpot fabpot modified the milestones: 7.2, 7.3 Nov 20, 2024
@fabpot
Copy link
Member

fabpot commented Jan 5, 2025

Closing as this PR won't be fixed as is. If someone wants to continue working on this, please open a new PR, maybe based on @OskarStark idea.

@fabpot fabpot closed this Jan 5, 2025
fabpot added a commit that referenced this pull request Jan 25, 2025
This PR was merged into the 7.3 branch.

Discussion
----------

[Notifier] Add SentMessage additional info

| Q             | A
| ------------- | ---
| Branch?       | 7.3
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Issues        | Fix #49793
| License       | MIT

This PR allows adding additional data into [Notifier/Message/SentMessage.php](https://github.com/symfony/symfony/blob/7.3/src/Symfony/Component/Notifier/Message/SentMessage.php), based on (and using) [HttpClient Response's info](https://github.com/symfony/symfony/blob/7.3/src/Symfony/Component/HttpClient/Response/AsyncContext.php#L118-L145). The initial need behind this feature is to be able to access to some data of the Transport's response (e.g.: [`nbSms` and `cost` for allmysms](https://doc.allmysms.com/api/fr/#api-SMS-sendsimple), or [`totalCreditsRemoved` for OVH Cloud](https://eu.api.ovh.com/console/?section=%2Fsms&branch=v1#post-/sms/-serviceName-/jobs)).

Original (closed) PR: #51746

Some points to discuss:
- It would be a lot easier to inject the eventual `ResponseInterface` object into the constructor as a readonly property, but not all Transports are using HTTP
- [Having a free-form array where each transport can add anything looks like a bad idea](#51746 (comment)), but [In HttpClient you documented the standard info keys, and decorators can add their own things there](#51746 (comment))

Commits
-------

a50f38c feat(notifier): add SentMessage additional info
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Notifier] SentMessage add debug property
8 participants