Интегрирован сбор метрик в sessionserver

This commit is contained in:
ErickSkrauch 2017-11-19 15:36:51 +03:00
parent 72f546c827
commit 236f0e7d50
2 changed files with 16 additions and 18 deletions

View File

@ -6,6 +6,7 @@ use api\modules\session\exceptions\IllegalArgumentException;
use api\modules\session\models\protocols\HasJoinedInterface; use api\modules\session\models\protocols\HasJoinedInterface;
use api\modules\session\Module as Session; use api\modules\session\Module as Session;
use common\models\Account; use common\models\Account;
use Yii;
use yii\base\ErrorException; use yii\base\ErrorException;
use yii\base\Model; use yii\base\Model;
@ -19,6 +20,7 @@ class HasJoinedForm extends Model {
} }
public function hasJoined(): Account { public function hasJoined(): Account {
Yii::$app->statsd->inc('sessionserver.hasJoined.attempt');
if (!$this->protocol->validate()) { if (!$this->protocol->validate()) {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
@ -26,13 +28,12 @@ class HasJoinedForm extends Model {
$serverId = $this->protocol->getServerId(); $serverId = $this->protocol->getServerId();
$username = $this->protocol->getUsername(); $username = $this->protocol->getUsername();
Session::info( Session::info("Server with server_id = '{$serverId}' trying to verify has joined user with username = '{$username}'.");
"Server with server_id = '{$serverId}' trying to verify has joined user with username = '{$username}'."
);
$joinModel = SessionModel::find($username, $serverId); $joinModel = SessionModel::find($username, $serverId);
if ($joinModel === null) { if ($joinModel === null) {
Session::error("Not found join operation for username = '{$username}'."); Session::error("Not found join operation for username = '{$username}'.");
Yii::$app->statsd->inc('sessionserver.hasJoined.fail_no_join');
throw new ForbiddenOperationException('Invalid token.'); throw new ForbiddenOperationException('Invalid token.');
} }
@ -42,9 +43,8 @@ class HasJoinedForm extends Model {
throw new ErrorException('Account must exists'); throw new ErrorException('Account must exists');
} }
Session::info( Session::info("User with username = '{$username}' successfully verified by server with server_id = '{$serverId}'.");
"User with username = '{$username}' successfully verified by server with server_id = '{$serverId}'." Yii::$app->statsd->inc('sessionserver.hasJoined.success');
);
return $account; return $account;
} }

View File

@ -53,6 +53,7 @@ class JoinForm extends Model {
$serverId = $this->serverId; $serverId = $this->serverId;
$accessToken = $this->accessToken; $accessToken = $this->accessToken;
Session::info("User with access_token = '{$accessToken}' trying join to server with server_id = '{$serverId}'."); Session::info("User with access_token = '{$accessToken}' trying join to server with server_id = '{$serverId}'.");
Yii::$app->statsd->inc('sessionserver.join.attempts');
if (!$this->validate()) { if (!$this->validate()) {
return false; return false;
} }
@ -63,10 +64,8 @@ class JoinForm extends Model {
throw new ErrorException('Cannot save join session model'); throw new ErrorException('Cannot save join session model');
} }
Session::info( Session::info("User with access_token = '{$accessToken}' and nickname = '{$account->username}' successfully joined to server_id = '{$serverId}'.");
"User with access_token = '{$accessToken}' and nickname = '{$account->username}' successfully joined to " . Yii::$app->statsd->inc('sessionserver.join.success');
"server_id = '{$serverId}'."
);
return true; return true;
} }
@ -100,6 +99,7 @@ class JoinForm extends Model {
/** @var MinecraftAccessKey|\api\components\OAuth2\Entities\AccessTokenEntity $accessModel */ /** @var MinecraftAccessKey|\api\components\OAuth2\Entities\AccessTokenEntity $accessModel */
if ($accessModel->isExpired()) { if ($accessModel->isExpired()) {
Session::error("User with access_token = '{$accessToken}' failed join by expired access_token."); Session::error("User with access_token = '{$accessToken}' failed join by expired access_token.");
Yii::$app->statsd->inc('sessionserver.join.fail_token_expired');
throw new ForbiddenOperationException('Expired access_token.'); throw new ForbiddenOperationException('Expired access_token.');
} }
@ -113,11 +113,13 @@ class JoinForm extends Model {
if ($identity === null) { if ($identity === null) {
Session::error("User with access_token = '{$accessToken}' failed join by wrong access_token."); Session::error("User with access_token = '{$accessToken}' failed join by wrong access_token.");
Yii::$app->statsd->inc('sessionserver.join.fail_wrong_token');
throw new ForbiddenOperationException('Invalid access_token.'); throw new ForbiddenOperationException('Invalid access_token.');
} }
if (!Yii::$app->user->can(P::MINECRAFT_SERVER_SESSION)) { if (!Yii::$app->user->can(P::MINECRAFT_SERVER_SESSION)) {
Session::error("User with access_token = '{$accessToken}' doesn't have enough scopes to make join."); Session::error("User with access_token = '{$accessToken}' doesn't have enough scopes to make join.");
Yii::$app->statsd->inc('sessionserver.join.fail_not_enough_scopes');
throw new ForbiddenOperationException('The token does not have required scope.'); throw new ForbiddenOperationException('The token does not have required scope.');
} }
@ -127,18 +129,14 @@ class JoinForm extends Model {
$selectedProfile = $this->selectedProfile; $selectedProfile = $this->selectedProfile;
$isUuid = StringHelper::isUuid($selectedProfile); $isUuid = StringHelper::isUuid($selectedProfile);
if ($isUuid && $account->uuid !== $this->normalizeUUID($selectedProfile)) { if ($isUuid && $account->uuid !== $this->normalizeUUID($selectedProfile)) {
Session::error( Session::error("User with access_token = '{$accessToken}' trying to join with identity = '{$selectedProfile}', but access_token issued to account with id = '{$account->uuid}'.");
"User with access_token = '{$accessToken}' trying to join with identity = '{$selectedProfile}'," . Yii::$app->statsd->inc('sessionserver.join.fail_uuid_mismatch');
" but access_token issued to account with id = '{$account->uuid}'."
);
throw new ForbiddenOperationException('Wrong selected_profile.'); throw new ForbiddenOperationException('Wrong selected_profile.');
} }
if (!$isUuid && mb_strtolower($account->username) !== mb_strtolower($selectedProfile)) { if (!$isUuid && mb_strtolower($account->username) !== mb_strtolower($selectedProfile)) {
Session::error( Session::error("User with access_token = '{$accessToken}' trying to join with identity = '{$selectedProfile}', but access_token issued to account with username = '{$account->username}'.");
"User with access_token = '{$accessToken}' trying to join with identity = '{$selectedProfile}'," . Yii::$app->statsd->inc('sessionserver.join.fail_username_mismatch');
" but access_token issued to account with username = '{$account->username}'."
);
throw new ForbiddenOperationException('Invalid credentials'); throw new ForbiddenOperationException('Invalid credentials');
} }