问题描述
这是我所做的,
- 创建了一个 Cloud9 实例,启动了一个 maven 应用程序,添加了 aws sdk java、x-ray 核心、x-ray 检测器、x-ray sdk 依赖项,创建了 DynamoDB 客户端运行了应用程序,插入了数据但未找到错误子段。手动添加的段,错误消失但没有痕迹。
- 创建 Spring Boot 应用程序,添加相同的依赖项,添加 Xray servlet 过滤器,添加开始段,开始子段,没有错误但没有痕迹。
我也有更多方法,但这些方法似乎非常接近。我也没有安装任何代理或守护进程。谁能告诉我哪里出错了?
我正在尝试创建一个简单的 Java 应用程序,甚至是一个页面来在 DynamoDB 中插入数据并获取跟踪记录。
解决方法
我没有在此处共享 Java 的经验,Node JS 示例希望对您有所帮助。对此进行了测试:https://github.com/aws-samples/aws-xray-sdk-node-sample
const AWSXRay = require('aws-xray-sdk');
const XRayExpress = AWSXRay.express;
const express = require('express');
// Capture all AWS clients we create
const AWS = AWSXRay.captureAWS(require('aws-sdk'));
AWS.config.update({region: process.env.DEFAULT_AWS_REGION || 'us-west-2'});
// Capture all outgoing https requests
AWSXRay.captureHTTPsGlobal(require('https'));
const https = require('https');
// Capture MySQL queries
const mysql = AWSXRay.captureMySQL(require('mysql'));
const app = express();
const port = 3000;
app.use(XRayExpress.openSegment('SampleSite'));
app.get('/',(req,res) => {
const seg = AWSXRay.getSegment();
const sub = seg.addNewSubsegment('customSubsegment');
setTimeout(() => {
sub.close();
res.sendFile(`${process.cwd()}/index.html`);
},500);
});
app.get('/aws-sdk/',res) => {
const ddb = new AWS.DynamoDB();
const ddbPromise = ddb.listTables().promise();
ddbPromise.then(function(data) {
res.send(`ListTables result:\n ${JSON.stringify(data)}`);
}).catch(function(err) {
res.send(`Encountered error while calling ListTables: ${err}`);
});
});
app.get('/http-request/',res) => {
const endpoint = 'https://amazon.com/';
https.get(endpoint,(response) => {
response.on('data',() => {});
response.on('error',(err) => {
res.send(`Encountered error while making HTTPS request: ${err}`);
});
response.on('end',() => {
res.send(`Successfully reached ${endpoint}.`);
});
});
});
app.get('/mysql/',res) => {
const mysqlConfig = require('./mysql-config.json');
const config = mysqlConfig.config;
const table = mysqlConfig.table;
if (!config.user || !config.database || !config.password || !config.host || !table) {
res.send('Please correctly populate mysql-config.json');
return;
}
const connection = mysql.createConnection(config);
connection.query(`SELECT * FROM ${table}`,(err,results,fields) => {
if (err) {
res.send(`Encountered error while querying ${table}: ${err}`);
return;
}
res.send(`Retrieved the following results from ${table}:\n${results}`);
});
connection.end();
});
app.use(XRayExpress.closeSegment());
app.listen(port,() => console.log(`Example app listening on port ${port}!`));