View a markdown version of this page

记录注释、元数据和用户 ID - AWS X-Ray

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

记录注释、元数据和用户 ID

注意

X-Ray SDK/Daemon 维护通知 — 2026 年 2 月 25 日 AWS X-Ray SDKs/Daemon 将进入维护模式, AWS 将限制 X-Ray SDK 和 Daemon 版本仅用于解决安全问题。有关支持时间表的更多信息,请参阅 X-Ray SDK 和 Daemon Support 时间表。我们建议迁移到 OpenTelemetry。有关迁移到仪器的更多信息 OpenTelemetry,请参阅从 X-Ray 仪器迁移到 OpenTelemetry 仪器

在游戏模型类中,每当应用程序将游戏保存在 DynamoDB 中时,都会将 Game 对象记录到元数据块中。应用程序单独将游戏 ID 记录在注释中以用于筛选表达式

src/main/java/scorekeep/GameModel.java — 注释和元数据
import com.amazonaws.xray.AWSXRay; import com.amazonaws.xray.entities.Segment; import com.amazonaws.xray.entities.Subsegment; ... public void saveGame(Game game) throws SessionNotFoundException { // wrap in subsegment Subsegment subsegment = AWSXRay.beginSubsegment("## GameModel.saveGame"); try { // check session String sessionId = game.getSession(); if (sessionModel.loadSession(sessionId) == null ) { throw new SessionNotFoundException(sessionId); } Segment segment = AWSXRay.getCurrentSegment(); subsegment.putMetadata("resources", "game", game); segment.putAnnotation("gameid", game.getId()); mapper.save(game); } catch (Exception e) { subsegment.addException(e); throw e; } finally { AWSXRay.endSubsegment(); } }

在移动控制器中,应用程序将用户 IDsetUser 一起记录。用户 ID 记录在分段的单独字段中,并且不会为其编制索引以用于搜索。

src/main/java/scorekeep/MoveController.java — 用户名
import com.amazonaws.xray.AWSXRay; ... @RequestMapping(value="/{userId}", method=RequestMethod.POST) public Move newMove(@PathVariable String sessionId, @PathVariable String gameId, @PathVariable String userId, @RequestBody String move) throws SessionNotFoundException, GameNotFoundException, StateNotFoundException, RulesException { AWSXRay.getCurrentSegment().setUser(userId); return moveFactory.newMove(sessionId, gameId, userId, move); }