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

Add support for contexts #839

Closed
wants to merge 0 commits into from
Closed

Add support for contexts #839

wants to merge 0 commits into from

Conversation

@williamdes
Copy link
Contributor Author

Maybe my works is not perfect, I can improve it though.

@williamdes
Copy link
Contributor Author

williamdes commented Jun 25, 2019

image

"contexts":{"akey":{"0":"avalue !!","type":"akey"},"keyscope":{"0":"efniurnuenrgergn","type":"keyscope"},"os":{"type":"os","name":"Linux"},"akey2":{"3":"ok","type":"akey2","4":{"rf0":"ok","key6":{"a":"b"},"key5":["ok"],"key4":"value"}}

The search builder in Sentry GUI

image

Copy link
Collaborator

@Jean85 Jean85 left a 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.

src/State/Scope.php Outdated Show resolved Hide resolved
@williamdes
Copy link
Contributor Author

@HazAT @Jean85 Can you please review ?

src/State/Scope.php Outdated Show resolved Hide resolved
src/State/Scope.php Outdated Show resolved Hide resolved
src/Event.php Outdated Show resolved Hide resolved
@HazAT
Copy link
Member

HazAT commented Jul 1, 2019

Thanks for the PR, good job, we definitely want to have setContext. I left a few notes.

@williamdes
Copy link
Contributor Author

@HazAT I did the changes you suggested, please review one last time :)

Copy link
Member

@HazAT HazAT left a 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

@williamdes
Copy link
Contributor Author

@HazAT While reviewing I found out that I had forgot to add some lines (b60017b)

Changelog added by ac56ad2

@ste93cry ste93cry changed the base branch from master to develop July 1, 2019 16:59
src/Context/Context.php Outdated Show resolved Hide resolved
src/State/Scope.php Outdated Show resolved Hide resolved
tests/State/ScopeTest.php Outdated Show resolved Hide resolved
src/State/Scope.php Outdated Show resolved Hide resolved
src/State/Scope.php Outdated Show resolved Hide resolved
src/State/Scope.php Outdated Show resolved Hide resolved
public function setContext(string $name, array $value): self
{
if (!isset($this->contexts[$name])) {
$this->contexts[$name] = new \stdClass();
Copy link
Collaborator

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?

Copy link
Contributor Author

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

Copy link
Collaborator

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.

Copy link
Collaborator

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

Copy link
Contributor Author

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 ?

Copy link

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.

Copy link
Collaborator

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

Copy link
Contributor Author

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

Copy link
Collaborator

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

Copy link
Contributor Author

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

@williamdes
Copy link
Contributor Author

@ste93cry I made the changes you requested :)

Copy link
Collaborator

@ste93cry ste93cry left a 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)

src/State/Scope.php Outdated Show resolved Hide resolved
src/Context/Context.php Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
src/Event.php Outdated Show resolved Hide resolved
src/Event.php Outdated Show resolved Hide resolved
@williamdes
Copy link
Contributor Author

@ste93cry I made more changes as you requested.


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.

Seems okay for me, you control the data you send to Sentry, no ?

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)

Same thing ?
They are not in the same place in the Sentry GUI and in the request
Did I misunderstand ?

@ste93cry
Copy link
Collaborator

ste93cry commented Jul 11, 2019

They are not in the same place in the Sentry GUI and in the request

What I meant is that some data like the information on the OS of the server or the HTTP request is stored under the $contexts key, which with this feature can be completely overridden by the user without any sort of validation. This means that before the data sent to Sentry was always filled correctly and strictly validated (if the server did not expect a string you could not send a string) while now we are going to lose all of these stuffs because we are just going to blindly replace all data with something else given by the user, and since some of the default contexts are named with common words (server, request, etc) it will be really easy to lose useful data by mistake

@williamdes
Copy link
Contributor Author

default contexts are named with common words (server, request, etc) it will be really easy to lose useful data by mistake

I agree

@ste93cry
Copy link
Collaborator

To clarify some more things, when I talked about strict validation I meant that the data of the contexts key of the event's payload are handled by specialized classes (ServerOsContext, RuntimeContext, etc) that validate the data (both types and allowed values). By just replacing its value with random data from the user we're losing such feature. Also, supposing you're not passing all required attributes for those special default contexts or you are passing more attributes than what the server expects we are gonna break the logging of events without the user be ever notified of this because we don't log the errors during the sending. In this regards, the classes were introduced to have both a nice API to manage each context and to prevent such issues by validating and restricting what the user could do on them. This is why I undoubtedly see the value of having such feature but I also see that it's going to introduce a lot of pitfalls that won't be easily solved once we merge it as is because we won't be able to rollback without breaking the behavior, be it user or technical. If there was some restriction, for example in disallowing/ignoring the override of the default contexts, I would be totally fine. Of course, if everyone agree anyway with this PR I'm ok to merge it

@williamdes
Copy link
Contributor Author

If there was some restriction, for example in disallowing/ignoring the override of the default contexts, I would be totally fine. Of course, if everyone agree anyway with this PR I'm ok to merge it

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

@williamdes
Copy link
Contributor Author

rebased.

@williamdes
Copy link
Contributor Author

rebased another time..

@HazAT @ste93cry @Jean85 Can you merge this feature or tell me what to change please ?

2 months later ..

@ste93cry
Copy link
Collaborator

This feature will be scheduled for 2.3 since there are some more general changes regarding how we manage contexts that may affect this feature and how it’s coded. I’m sorry if this is taking more time than you expected, in the meanwhile you can use the beforeSend option to edit the raw event data before it’s sent

@ste93cry ste93cry modified the milestones: 2.2, 2.3 Aug 30, 2019
@williamdes
Copy link
Contributor Author

I have no idea why psalm complains about, I did not change this file..

ERROR: InvalidOperand - src/Stacktrace.php:314:36 - Cannot perform a numeric operation with a non-numeric type array-key
                $result['param' . ($index + 1)] = $this->serializeArgument($argument);

@williamdes
Copy link
Contributor Author

@ste93cry I checked and the error shows on develop branch so I will not fix it.

* Sets context data with the given name.
*
* @param string $name The name that uniquely identifies the context
* @param array<string, mixed> $value The value
Copy link
Collaborator

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>>?

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 will try another time but I did have to revert this because psalm says it looks like an object

/**
* @var Context The list of contexts associated to this scope
*/
private $contexts;
Copy link
Collaborator

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
Copy link
Collaborator

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;
Copy link
Collaborator

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

@@ -274,6 +362,25 @@ public function testGetExtraContext(): void
$this->assertInstanceOf(Context::class, $event->getExtraContext());
}

public function testGetContexts(): void
Copy link
Collaborator

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

@@ -119,6 +119,94 @@ public function testToArray(): void
$this->assertEquals($expected, $event->toArray());
}

public function testToArrayMergeUserContexts(): void
Copy link
Collaborator

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?

@ste93cry ste93cry closed this Dec 22, 2019
ste93cry pushed a commit to ste93cry/sentry-php that referenced this pull request Dec 22, 2019
Pull-request: getsentry#839

Signed-off-by: William Desportes <williamdes@wdes.fr>
ste93cry pushed a commit to ste93cry/sentry-php that referenced this pull request Dec 22, 2019
Pull-request: getsentry#839

Signed-off-by: William Desportes <williamdes@wdes.fr>
ste93cry pushed a commit to ste93cry/sentry-php that referenced this pull request Dec 22, 2019
Pull-request: getsentry#839

Signed-off-by: William Desportes <williamdes@wdes.fr>
ste93cry pushed a commit to ste93cry/sentry-php that referenced this pull request Dec 22, 2019
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
@williamdes
Copy link
Contributor Author

@ste93cry did my work get merged?

@ste93cry
Copy link
Collaborator

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

@williamdes
Copy link
Contributor Author

Thank you for your nice reply, keep me updated on the progress of this PR

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

I totally understand, I have the same challenges on the phpMyAdmin project with all the contributions and issues

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.

None yet

5 participants