-
-
Notifications
You must be signed in to change notification settings - Fork 450
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
Add support for contexts #839
Conversation
Maybe my works is not perfect, I can improve it though. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure about the usefulness of this feature; I would require @HazAT to chime in to understand how this feature should works in regard to Sentry server side.
Thanks for the PR, good job, we definitely want to have |
@HazAT I did the changes you suggested, please review one last time :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks again 👍
Maybe another review of @stayallive @Jean85 @ste93cry (sorry for the multi ping, one review should be enough)
@williamdes Please one last thing, add a changelog entry
src/State/Scope.php
Outdated
public function setContext(string $name, array $value): self | ||
{ | ||
if (!isset($this->contexts[$name])) { | ||
$this->contexts[$name] = new \stdClass(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really like to have to use stdClass
, what's the point of doing it instead of using $value
directly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sentry only accepts objects for contexts
, any other value will make an error..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is reasonable only if the user is using numeric-only keys, so json_encode
will mistake it for an array.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@wedamija you're right, but there is only one case that cames to my mind that could cause PHP to JSON-encode in the wrong format the array: when it represents a pure list (so 0-based index). In all other cases it should work, and I expect people to use the context more as a map than a list. Of course this cannot be strictly enforced, we could add a check like in the TagsContext
that the key is a string until PHP will have true generics that will allow typehinting the arrays
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ste93cry What should I do ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we could add a check like in the TagsContext that the key is a string
I'd be in favor of doing so as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This won't work as PHP will convert a string containing a number into a number if it's used as key of an array 💩 Imho let's not complicate the code to try handle such cases, make use of a simple array
instead of stdClass
and it will work fine as long as someone does not really pass a list instead of a map
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ste93cry do I need to make a change here ?
as I said "Sentry only accepts objects for contexts, any other value will make an error."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do I need to make a change here ?
Yes please, let's start with array
and let's see what happens and how the situation evolves in the future. Also please update the code to use a plain array for the contexts
variable and add the setContexts
method to the Event
class.
"Sentry only accepts objects for contexts, any other value will make an error."
You're right, however it's unlikely that someone will put a list as value of a context and it's unlikely that someone will use string
keys containing numbers knowing that they will be casted
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, made the required changes (rebase, amend fix)
See 706fd14
@ste93cry I made the changes you requested :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ping @HazAT one thing that doesn't convince me of this feature is that it will allow people to just override whatever is in the contexts
key, even the "standard" ones like server OS or request that cames from the interfaces. Right now the SDK interfaces are well defined and invalid data cannot be set while with this change the whole validation will be unuseful. Also I find really confusing that there are multiple ways to configure the same thing (the particular contexts and the contexts as a whole thing)
@ste93cry I made more changes as you requested.
Seems okay for me, you control the data you send to Sentry, no ?
Same thing ? |
What I meant is that some data like the information on the OS of the server or the HTTP request is stored under the |
I agree |
To clarify some more things, when I talked about strict validation I meant that the data of the |
Okay, I someone else requests disallowing the override, I will invert the array merge (https://github.com/getsentry/sentry-php/pull/839/files#diff-a8ad00caade65c3a6d9d157e7e4b8708R656). |
rebased. |
This feature will be scheduled for |
I have no idea why psalm complains about, I did not change this file..
|
@ste93cry I checked and the error shows on |
src/State/Scope.php
Outdated
* Sets context data with the given name. | ||
* | ||
* @param string $name The name that uniquely identifies the context | ||
* @param array<string, mixed> $value The value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be array<string, array<string, mixed>>
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will try another time but I did have to revert this because psalm says it looks like an object
src/State/Scope.php
Outdated
/** | ||
* @var Context The list of contexts associated to this scope | ||
*/ | ||
private $contexts; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For simplicity let's use an array<string, array<string, mixed>>
too here. I don't think that there is any good reason to use the Context
object which we may remove in 3.0
given that `contexts is able to override any data of the interfaces, thus effectively bypassing the validation provided by these objects
src/Event.php
Outdated
* Sets context data with the given name. | ||
* | ||
* @param string $name The name that uniquely identifies the context | ||
* @param array<string, mixed> $value The value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The typehint should be array<string, array<string, mixed>>
src/Event.php
Outdated
/** | ||
* @var Context An arbitrary mapping of additional contexts associated to this event | ||
*/ | ||
private $contexts; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please switch to array<string, array<string, mixed>>
, see similar comment below for the explanation of why I'm requesting this change
tests/EventTest.php
Outdated
@@ -274,6 +362,25 @@ public function testGetExtraContext(): void | |||
$this->assertInstanceOf(Context::class, $event->getExtraContext()); | |||
} | |||
|
|||
public function testGetContexts(): void |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that this test is useful because using typehints we are guaranteed that the returned value is of the expected type
tests/EventTest.php
Outdated
@@ -119,6 +119,94 @@ public function testToArray(): void | |||
$this->assertEquals($expected, $event->toArray()); | |||
} | |||
|
|||
public function testToArrayMergeUserContexts(): void |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about merging this test and the one below using a data provider?
Pull-request: getsentry#839 Signed-off-by: William Desportes <williamdes@wdes.fr>
Pull-request: getsentry#839 Signed-off-by: William Desportes <williamdes@wdes.fr>
Pull-request: getsentry#839 Signed-off-by: William Desportes <williamdes@wdes.fr>
Pull-request: getsentry#839 Signed-off-by: William Desportes <williamdes@wdes.fr> Add unit tests for Contexts Pull-request: getsentry#839 Signed-off-by: William Desportes <williamdes@wdes.fr> Fix CR issues
@ste93cry did my work get merged? |
Not yet, I wanted to rebase and change some things before requesting a final review and merging but somehow I messed your branch or whatever it happened (I honestly don't know) and GitHub closed the PR because it though there were no more commits... I will open a new PR as followup of this one with all your changes plus mines merged together. I feel very sorry for how I personally handled your contribution, how long it took to get my attention and how long it's taking to get merged |
Thank you for your nice reply, keep me updated on the progress of this PR
I totally understand, I have the same challenges on the phpMyAdmin project with all the contributions and issues |
https://github.com/getsentry/sentry-javascript/blob/faf038bef8d3812982f258e8630d7d3521a9ff02/packages/integrations/src/vue.ts#L93
https://github.com/getsentry/sentry-javascript/blob/c3b794026591ad40325f38250b852070f21c2982/packages/browser/test/index.test.ts#L45
setContext
is missingContexts are very useful, the javascript sdk implements it correctly.