Skip to content
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

Closed
styfle opened this issue Feb 4, 2019 · 9 comments

Comments

@styfle
Copy link

styfle commented Feb 4, 2019

I created a repository so you can reproduce the issue here:

https://github.com/styfle/aws-xray-bug

Related to #60

/cc @chrisradek

@chrisradek
Copy link
Contributor

@styfle
Are you seeing this issue everytime you attempt to trace your micro app? One thing I noticed is your package.json uses [email protected], but for async support, you'd want to be using [email protected]

When I ran your example with this change, I was able to see multiple traces when calling the endpoint with GET.

@styfle
Copy link
Author

styfle commented Feb 10, 2019

@chrisradek Thanks for the response!

I ended up changing the code to use [email protected] but I still needed to access private APIs to get it to log HTTP Request and HTTP Response.

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 🤷‍♂️

@styfle
Copy link
Author

styfle commented Feb 12, 2019

@chrisradek I think I found another bug.

I can't seem to use an annotation key that contains a hyphen.

For example segment.addAnnotation('x-now-id', 'abc123') does not work but segment.addAnnotation('xNowId', 'abc123') works as expected.

Is this a known limitation or a bug?

@chrisradek
Copy link
Contributor

@styfle
Annotation keys can be up to 500 alphanumeric characters. No spaces or symbols except underscores. Values can be up to 1000 unicode characters.

@styfle
Copy link
Author

styfle commented Feb 12, 2019

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.

@styfle
Copy link
Author

styfle commented Feb 13, 2019

For some strange reason, there is no response logged around 30% the time (even with my res.on('finish') hack above.

image

@styfle
Copy link
Author

styfle commented Feb 19, 2019

@chrisradek The experimental tag does not work with Node 11.

error [email protected]: The engine "node" is incompatible with this module. Expected version ">= 4.x <= 10.x". Got "11.10.0"

Is there a reason why the package.json specifies <= 10.x?


Update: Looks like it was fixed in #89

Can you publish a new experimental tag to npm?

@manchuck
Copy link

@styfle The reason for working on node <= 10.x is due to the lambda execution environment. Lambada currently executes node @ 8.10

@chrisradek
Copy link
Contributor

Closing this since we started talking about multiple issues. If you are still encountering issues after installing the experimental tag of the SDK, please feel free to open a new issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants