Sentry-php: How to set user data in a new way?

Created on 20 Jan 2020  路  2Comments  路  Source: getsentry/sentry-php

According https://github.com/getsentry/sentry-php/pull/931 there is a depricated way to set user data over:
Sentry\configureScope(function (Sentry\State\Scope $scope) use ($user_info): void { $scope->setUser([...]); });
So, how I need to make is correctly. Now I'm getting warning like 'USER_DEPRECATED Replacing the data is deprecated since version 2.3 and will stop working from version 3.0'

Question

Most helpful comment

You just need to pass true to the second argument of the setUser method:

$scope->setUser([...], true);

Since there is no way (at the moment, at least) to remove something set on the root-level of the context, if you need to do it the best way is to create a temporary nested scope that will be popped off from the stack when it will not be needed anymore. This happens because the data you pass to the method is merged with the existing data, thus it's impossible to unset the keys or replace the data entirely

configureScope(static function (Scope $scope): void {
    $scope->setUser(['foo' => 'bar'], true);
});

// event.user = ['foo' => 'bar']

withScope(static function (Scope $scope): void {
    $scope->setUser(['foo' => 'baz'], true);
    $scope->setUser(['bar' => ['qux' => 'quux']], true);

    // event.user = ['foo' => 'baz', 'bar' => ['qux' => 'quux']]

    $scope->setUser(['bar' => ['quux' => 'quuz'], true);

    // event.user = ['foo' => 'baz', 'bar' => ['quux' => 'quuz']]
});

// event.user = ['foo' => 'bar']

All 2 comments

You just need to pass true to the second argument of the setUser method:

$scope->setUser([...], true);

Since there is no way (at the moment, at least) to remove something set on the root-level of the context, if you need to do it the best way is to create a temporary nested scope that will be popped off from the stack when it will not be needed anymore. This happens because the data you pass to the method is merged with the existing data, thus it's impossible to unset the keys or replace the data entirely

configureScope(static function (Scope $scope): void {
    $scope->setUser(['foo' => 'bar'], true);
});

// event.user = ['foo' => 'bar']

withScope(static function (Scope $scope): void {
    $scope->setUser(['foo' => 'baz'], true);
    $scope->setUser(['bar' => ['qux' => 'quux']], true);

    // event.user = ['foo' => 'baz', 'bar' => ['qux' => 'quux']]

    $scope->setUser(['bar' => ['quux' => 'quuz'], true);

    // event.user = ['foo' => 'baz', 'bar' => ['quux' => 'quuz']]
});

// event.user = ['foo' => 'bar']

You just need to pass true to the second argument of the setUser method:

oh, right! I decided that I need to change method I used. I was inattentive when I read that code

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Nix-id picture Nix-id  路  5Comments

Kleingeld picture Kleingeld  路  5Comments

iluuu1994 picture iluuu1994  路  3Comments

realtebo picture realtebo  路  5Comments

JanMikes picture JanMikes  路  4Comments