-
Notifications
You must be signed in to change notification settings - Fork 155
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
xray middleware for micro fails with Error: Failed to get the current sub/segment from the context #95
Comments
@styfle When I ran your example with this change, I was able to see multiple traces when calling the endpoint with GET. |
@chrisradek Thanks for the response! I ended up changing the code to use Here's what I ended up with. const AWSXRay = require('aws-xray-sdk');
const { IncomingRequestData } = AWSXRay.middleware;
console.log('Patching http global');
AWSXRay.captureHTTPsGlobal(require('http'));
function trace(handler) => {
console.log('start tracing micro');
return async function traceAsync(req, res) {
const namespace = AWSXRay.getNamespace();
const segment = new AWSXRay.Segment('some-segment-name-here');
segment.http = new IncomingRequestData(req);
res.on('finish', function finish() {
console.log('end tracing micro');
segment.http.close(res);
segment.http.status = res.statusCode || '';
if (res.statusCode === 429) {
segment.throttle = true;
}
if (res.headers && res.headers['content-length']) {
segment.http['content_length'] = res.headers['content-length'];
}
segment.close();
});
namespace.bindEmitter(req);
namespace.bindEmitter(res);
const result = await namespace.runAndReturn(async function run() {
AWSXRay.setSegment(segment);
const handlerResponse = await handler(req, res);
segment.close();
return handlerResponse;
});
return result;
};
}
function captureAsync(func, args) {
const { name = 'anonymous' } = func;
console.log('captureAsync ', name);
return new Promise((resolve, reject) => {
AWSXRay.captureAsyncFunc(name, async (subsegment) => {
try {
const p = func.apply(this, args);
let result;
if (p instanceof Promise) {
result = await p;
} else {
result = p;
}
subsegment.close();
resolve(result);
} catch (error) {
subsegment.close(error);
reject(error);
}
});
});
}
module.exports = { trace, captureAsync }; Even with these private APIs, I still sometimes does not log the HTTP Response 🤷♂️ |
@chrisradek I think I found another bug. I can't seem to use an annotation key that contains a hyphen. For example Is this a known limitation or a bug? |
I see now. It was silently dropping that data even with the following flags: - AWS_XRAY_DEBUG_MODE=TRUE
- AWS_XRAY_CONTEXT_MISSING=LOG_ERROR I would expect to see some sort of logging error when a segment does not meet this criteria. |
@chrisradek The
Is there a reason why the package.json specifies Update: Looks like it was fixed in #89 Can you publish a new |
@styfle The reason for working on node |
Closing this since we started talking about multiple issues. If you are still encountering issues after installing the |
I created a repository so you can reproduce the issue here:
https://github.com/styfle/aws-xray-bug
Related to #60
/cc @chrisradek
The text was updated successfully, but these errors were encountered: